閑來(lái)沒(méi)事政冻,學(xué)習(xí)了下二叉樹
public class TreeNode {
/**
* 左邊節(jié)點(diǎn)
*/
public TreeNode leftTree;
/**
* 右邊節(jié)點(diǎn)
*/
public TreeNode rightTree;
/**
* 數(shù)據(jù)
*/
public int iDate;
public int parent;
public TreeNode(int i) {
// TODO Auto-generated constructor stub
this.iDate = i;
}
public void displayNode() {
}
public void add(TreeNode root, int idate) {
// parent=root;
TreeNode node = find(root, idate);
if (node != null) {
if (node.iDate < idate) {
node.rightTree = new TreeNode(idate);
node.rightTree.parent=node.iDate;
} else {
node.leftTree = new TreeNode(idate);
node.leftTree.parent=node.iDate;
}
}
}
private TreeNode find(TreeNode root, int i) {
// TODO Auto-generated method stub
if (root.iDate < i) {
if (root.rightTree != null) {
return find(root.rightTree, i);
} else {
return root;
}
} else {
if (root.leftTree != null) {
return find(root.leftTree, i);
} else {
return root;
}
}
}
}
----------------------------------
public class TreeTest {
public static int[] temp={7,3,9,8,10,2,1};
public static void main(String[] args) {
TreeNode node=new TreeNode(5);
for (int i = 0; i < temp.length; i++) {
node.add(node,temp[i]);
}
System.out.println(node);
// node.find(3);
}
}