建立一个二叉树,附带查询代码,JAVA代码

java二叉树代码~

java构造二叉树,可以通过链表来构造,如下代码: public class BinTree {public final static int

class Node { private int value; private Node left; private Node right; // 存储节点 public void store(int value) { if (this.value > value) { if (left == null) { left = new Node(); left.value = value; } else { left.store(value); } } else { if (right == null) { right = new Node(); right.value = value; } else { /*right.value = value;*/ right.store(value); } } } // 查找节点 public boolean find(int value) { if (this.value == value) { return true; } else if (this.value > value) { if (right == null) return false; return right.find(value); } else { if (left == null) return false; return left.find(value); } } // 前序遍历 public void preList() { System.out.print(this.value + ">>"); if (left != null) left.preList(); if (right != null) right.preList(); } // 中序遍历 public void middleList() { if (left != null) left.middleList(); System.out.print(this.value + ">>"); if (right != null) right.middleList(); } // 后序遍历 public void afterList() { if (left != null) left.afterList(); if (right != null) right.afterList(); System.out.print(this.value + ">>"); } /** * @return the value */ public int getValue() { return value; } /** * @param value the value to set */ public void setValue(int value) { this.value = value; }}

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;
}
}

求数据结构二叉树查找结点及其父节点的代码,谢谢!!!
答:build_tree(1,x);//构建二叉树(结构体数组模拟)cin>>m;//查询次数 for(int i=0;i<m;i++){ int num,y;cin>>num;//查询值 y=mp[num];//mp[num]是num在tree数组中的位置,查询效率O(log2n)y/=2;//...

建立一个二叉树,附带查询代码,JAVA代码
答:// { "root", null, "left", "right", null } // 则构建出一个根节点,带有两个孩子("left","right")的树 public Tree( Object[] values ){ // 创建根 _root = new TreeNode( values[0], null );//...

数据结构-二叉树的创建?
答:二叉树建立实现代码一,如下所示。//创建树//按先后次序输入二叉树中结点的值(一个字符),#表示空树//构造二叉链表表示的二叉树BiTree CreateTree(BiTree t){ char ch; scanf("%c", &ch); if(ch == '...

请问C语言如何创建二叉树???
答:void insert(Tree* tree, int value)//创建树 { Node* node=(Node*)malloc(sizeof(Node));//创建一个节点 node->data = value;node->left = NULL;node->right = NULL;if (tree->root == NULL)//判断树是...

用java怎么构造一个二叉树呢?
答:java构造二叉树,可以通过链表来构造,如下代码:public class BinTree {public final static int MAX=40;BinTree []elements = new BinTree[MAX];//层次遍历时保存各个节点 int front;//层次遍历时队首 int rear...

求C++的二叉树建立程序代码!
答://前序遍历生成二叉树 void createtree(bintree *t){ datatype c;c=getchar();if(c == '#')t = NULL;else{ t = (bintree)malloc(sizeof(BinNode));(*t)->data = c;createtree(&(*t)->lchild);creat...

二叉树怎么建立?
答:一、我们要明确的一点是只有中序是无法创建二叉树的,它要结合先序,两者相联系才可以。二、根据二叉树的图,得出先序的顺序是ABDECFG,而与此同时的中序DBEAFCG,根据这个建立。三、然后就是要根据二叉树的原则编写代码...

建立一棵二叉树,数据以字符串形式从键盘输入。
答:代码如下:char a[105];int len,i;//i逐渐增加 void build(int s){ if(i==len) return;//已经建完树了 char c=a[i];//当前的字符 i++;if(!tree[s].l) tree[s].l=c;//如果树的左边是空的,...

求c语言数据结构二叉树的建树,前序遍历,输出树的代码,能用采纳。
答:} int main(){ BiTree T;//将二叉树初始为一个空的二叉树 Init_BiTree(&T);//创建二叉树 Create_BiTree(&T);//先序遍历 printf("\n先序遍历结果:");PreOrder_BiTree(T,Print_BiTreeNode);return 0;} ...

如何用python构造一个n层的完全二叉树
答:用python构造一个n层的完全二叉树的代码如下: typedef struct {int weight;int parent, lchild, rchild; } HTNode ,*HuffmanTree; // 动态分配数组存储huffman树 算法设计void createHuffmantree(){ ht=(HuffmanTree)...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网