java树的代码编写 java实现树结构的输出
如何在java构造函数中创建一棵树
import java.util.Stack;//导入栈包
河东ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
public class newtree {
private newtree lchild;// 声明数据成员
private newtree rchild;
private char data;
private newtree root;
public newtree(newtree l, newtree r, char data) {// 有参构造函数进行成员赋值
lchild = l;
rchild = r;
this.data = data;
}
public newtree() {// 无参构造函数创建树
newtree f = new newtree(null, null, 'f');
newtree g = new newtree(null, null, 'g');
newtree d = new newtree(null, null, 'd');
newtree e = new newtree(null, null, 'e');
newtree b = new newtree(d, e, 'b');
newtree c = new newtree(f, g, 'c');
newtree a = new newtree(b, c, 'a');
this.root=a;
}
public void visit(newtree p) {/* 输出数据 */
System.out.print(p.data);// 访问结点
}
@SuppressWarnings("unchecked")
public void InOrder() {/* 输入数据 */
newtree p=this.root;//你建了一棵树要把根节点赋值进去啊
Stack s = new Stack();
while (p != null || !s.isEmpty()) /* 处理数据:进行中序遍历 */
{
if (p != null) {
s.push(p);
p = p.lchild;
} else {
p = (newtree) s.pop();
p.visit(p);//this指的是当前的类对象
p = p.rchild;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
newtree h = new newtree();// 声明变量,变量赋值
h.InOrder();
}
}
//根据你的代码改了一个
import java.util.Stack;//导入栈包
public class newtree {
public Tree createTree() {// 无参构造函数创建树
Tree f = new Tree(null, null, 'f');
Tree g = new Tree(null, null, 'g');
Tree d = new Tree(null, null, 'd');
Tree e = new Tree(null, null, 'e');
Tree b = new Tree(d, e, 'b');
Tree c = new Tree(f, g, 'c');
Tree a = new Tree(b, c, 'a');
return a;
}
public void InOrder(Tree p) {/* 输入数据 */
StackTree s = new StackTree();
while (p != null || !s.isEmpty()) { /* 处理数据:进行中序遍历 */
if (p != null) {
s.push(p);
p = p.lchild;
} else {
p = s.pop();
System.out.print(p.data);
p = p.rchild;
}
}
}
public void inOrder1(Tree p) {
if (p == null)
return;
inOrder1(p.lchild);
System.out.print(p.data);
inOrder1(p.rchild);
}
public static void main(String[] args) {
newtree h = new newtree();// 声明变量,变量赋值
h.InOrder(h.createTree());
System.out.println();
h.inOrder1(h.createTree());
}
}
class Tree {
Tree lchild;// 声明数据成员
Tree rchild;
char data;
Tree(Tree lchild, Tree rchild, char data) {
this.lchild = lchild;
this.rchild = rchild;
this.data = data;
}
}
建立一个二叉树,附带查询代码,JAVA代码
import java.util.ArrayList;
// 树的一个节点
class TreeNode {
Object _value = null; // 他的值
TreeNode _parent = null; // 他的父节点,根节点没有PARENT
ArrayList _childList = new ArrayList(); // 他的孩子节点
public TreeNode( Object value, TreeNode parent ){
this._parent = parent;
this._value = value;
}
public TreeNode getParent(){
return _parent;
}
public String toString() {
return _value.toString();
}
}
public class Tree {
// 给出宽度优先遍历的值数组,构建出一棵多叉树
// null 值表示一个层次的结束
// "|" 表示一个层次中一个父亲节点的孩子输入结束
// 如:给定下面的值数组:
// { "root", null, "left", "right", null }
// 则构建出一个根节点,带有两个孩子("left","right")的树
public Tree( Object[] values ){
// 创建根
_root = new TreeNode( values[0], null );
// 创建下面的子节点
TreeNode currentParent = _root; // 用于待创建节点的父亲
//TreeNode nextParent = null;
int currentChildIndex = 0; // 表示 currentParent 是他的父亲的第几个儿子
//TreeNode lastNode = null; // 最后一个创建出来的TreeNode,用于找到他的父亲
for ( int i = 2; i values.length; i++ ){
// 如果null ,表示下一个节点的父亲是当前节点的父亲的第一个孩子节点
if ( values[i] == null ){
currentParent = (TreeNode)currentParent._childList.get(0);
currentChildIndex = 0;
continue;
}
// 表示一个父节点的所有孩子输入完毕
if ( values[i].equals("|") ){
if ( currentChildIndex+1 currentParent._childList.size() ){
currentChildIndex++;
currentParent = (TreeNode)currentParent._parent._childList.get(currentChildIndex);
}
continue;
}
TreeNode child = createChildNode( currentParent, values[i] );
}
}
TreeNode _root = null;
public TreeNode getRoot(){
return _root;
}
/**
// 按宽度优先遍历,打印出parent子树所有的节点
private void printSteps( TreeNode parent, int currentDepth ){
for ( int i = 0; i parent._childList.size(); i++ ){
TreeNode child = (TreeNode)parent._childList.get(i);
System.out.println(currentDepth+":"+child);
}
if ( parent._childList.size() != 0 ) System.out.println(""+null);// 为了避免叶子节点也会打印null
//打印 parent 同层的节点的孩子
if ( parent._parent != null ){ // 不是root
int i = 1;
while ( i parent._parent._childList.size() ){// parent 的父亲还有孩子
TreeNode current = (TreeNode)parent._parent._childList.get(i);
printSteps( current, currentDepth );
i++;
}
}
// 递归调用,打印所有节点
for ( int i = 0; i parent._childList.size(); i++ ){
TreeNode child = (TreeNode)parent._childList.get(i);
printSteps( child, currentDepth+1 );
}
}
// 按宽度优先遍历,打印出parent子树所有的节点
public void printSteps(){
System.out.println(""+_root);
System.out.println(""+null);
printSteps(_root, 1 );
}**/
// 将给定的值做为 parent 的孩子,构建节点
private TreeNode createChildNode( TreeNode parent, Object value ){
TreeNode child = new TreeNode( value , parent );
parent._childList.add( child );
return child;
}
public static void main(String[] args) {
Tree tree = new Tree( new Object[]{ "root", null,
"left", "right", null,
"l1","l2","l3", "|", "r1","r2",null } );
//tree.printSteps();
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(0) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(1) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(2) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(0) );
System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(1) );
}
}
java:二叉树添加和查询方法
package arrays.myArray;
public class BinaryTree {
private Node root;
// 添加数据
public void add(int data) {
// 递归调用
if (null == root)
root = new Node(data, null, null);
else
addTree(root, data);
}
private void addTree(Node rootNode, int data) {
// 添加到左边
if (rootNode.data data) {
if (rootNode.left == null)
rootNode.left = new Node(data, null, null);
else
addTree(rootNode.left, data);
} else {
// 添加到右边
if (rootNode.right == null)
rootNode.right = new Node(data, null, null);
else
addTree(rootNode.right, data);
}
}
// 查询数据
public void show() {
showTree(root);
}
private void showTree(Node node) {
if (node.left != null) {
showTree(node.left);
}
System.out.println(node.data);
if (node.right != null) {
showTree(node.right);
}
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
用java怎么构造一个二叉树?
二叉树的相关操作,包括创建,中序、先序、后序(递归和非递归),其中重点的是java在先序创建二叉树和后序非递归遍历的的实现。
package com.algorithm.tree;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;
public class Tree {
private Node root;
public Tree() {
}
public Tree(Node root) {
this.root = root;
}
//创建二叉树
public void buildTree() {
Scanner scn = null;
try {
scn = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
root = createTree(root,scn);
}
//先序遍历创建二叉树
private Node createTree(Node node,Scanner scn) {
String temp = scn.next();
if (temp.trim().equals("#")) {
return null;
} else {
node = new Node((T)temp);
node.setLeft(createTree(node.getLeft(), scn));
node.setRight(createTree(node.getRight(), scn));
return node;
}
}
//中序遍历(递归)
public void inOrderTraverse() {
inOrderTraverse(root);
}
public void inOrderTraverse(Node node) {
if (node != null) {
inOrderTraverse(node.getLeft());
System.out.println(node.getValue());
inOrderTraverse(node.getRight());
}
}
//中序遍历(非递归)
public void nrInOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
node = stack.pop();
System.out.println(node.getValue());
node = node.getRight();
}
}
//先序遍历(递归)
public void preOrderTraverse() {
preOrderTraverse(root);
}
public void preOrderTraverse(Node node) {
if (node != null) {
System.out.println(node.getValue());
preOrderTraverse(node.getLeft());
preOrderTraverse(node.getRight());
}
}
//先序遍历(非递归)
public void nrPreOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
System.out.println(node.getValue());
stack.push(node);
node = node.getLeft();
}
node = stack.pop();
node = node.getRight();
}
}
//后序遍历(递归)
public void postOrderTraverse() {
postOrderTraverse(root);
}
public void postOrderTraverse(Node node) {
if (node != null) {
postOrderTraverse(node.getLeft());
postOrderTraverse(node.getRight());
System.out.println(node.getValue());
}
}
//后续遍历(非递归)
public void nrPostOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
Node preNode = null;//表示最近一次访问的节点
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
node = stack.peek();
if (node.getRight() == null || node.getRight() == preNode) {
System.out.println(node.getValue());
node = stack.pop();
preNode = node;
node = null;
} else {
node = node.getRight();
}
}
}
//按层次遍历
public void levelTraverse() {
levelTraverse(root);
}
public void levelTraverse(Node node) {
QueueNode queue = new LinkedBlockingQueueNode();
queue.add(node);
while (!queue.isEmpty()) {
Node temp = queue.poll();
if (temp != null) {
System.out.println(temp.getValue());
queue.add(temp.getLeft());
queue.add(temp.getRight());
}
}
}
}
//树的节点
class Node {
private Node left;
private Node right;
private T value;
public Node() {
}
public Node(Node left,Node right,T value) {
this.left = left;
this.right = right;
this.value = value;
}
public Node(T value) {
this(null,null,value);
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
测试代码:
package com.algorithm.tree;
public class TreeTest {
/**
* @param args
*/
public static void main(String[] args) {
Tree tree = new Tree();
tree.buildTree();
System.out.println("中序遍历");
tree.inOrderTraverse();
tree.nrInOrderTraverse();
System.out.println("后续遍历");
//tree.nrPostOrderTraverse();
tree.postOrderTraverse();
tree.nrPostOrderTraverse();
System.out.println("先序遍历");
tree.preOrderTraverse();
tree.nrPreOrderTraverse();
//
}
}
java编打出5行圣诞树,求教每一步详细思想。下面是代码
按照你的要求加详细注释的圣诞树Java程序如下:(编程思想在注释中说明)
public class ShengDanShu2 {
//这个程序的编程思想是利用对for循环变量i的控制达到一层循环代替双层循环的目的
public static void main(String[] args) {
int n=5; //初始化打印圣诞树层数变量n
int a=0; //初始化打印前置空格数变量a
int b=0; //初始化打印星号数变量b
for(int i=1;i =n;i++){ //打印n层圣诞树
if(a!=(n-i)){ //如果前置空格数不等于n-i
System.out.print(" "); //打印一个空格
a++; //前置空格数加一
i=i-1; //i变量减一 目的是固定住i变量不变直到a==n-i
}else if(b!=(2*i-1)){ //如果星号数不等于2*i-1
System.out.print("*"); //打印一个星号
b++; //星号数加一
i=i-1; //i变量减一 目的是固定住i变量不变直到b==2*i-1
}else if(a==(n-i) b==(2*i-1)){//当以上两个条件都满足时,换行初始化a和b为0
System.out.println(); //打印换行
a=0; //对新的一行重新初始化前置空格数变量a
b=0; //对新的一行重新初始化打印星号数变量b
//这里没有控制for循环的i变量减一,因为这时i变量加一,开始新一行。
}
}
}
}
运行结果:
*
***
*****
*******
*********
分享题目:java树的代码编写 java实现树结构的输出
文章位置:http://scyanting.com/article/ddgjpih.html