packagebinary_tree;
/**
*@authorzhou shengshuai
*
*/
classNodes {
intvalue;
Nodes leftN;
Nodes rightN;
publicNodes(int value) {
this.value = value;
}
publicvoiddisplayNodes() {
System.out.println(value);
}
}
classTrees {
privateNodesroot;
publicTrees() {
this.root =null;
}
publicvoidinsert(Nodes node) {
if(root ==null) {
root = node;
} else{
Nodes current =root;
while (current !=null) {
if(node.value < current.value) {
if (current.leftN ==null) {
current.leftN = node;
break;
} else {
current= current.leftN;
}
} else{
if (current.rightN ==null) {
current.rightN= node;
break;
} else {
current= current.rightN;
}
}
}
}
}
}
publicclassTreeTest {
/**
*@paramargs
*/
publicstaticvoidmain(String[] args) {
Trees ts = newTrees();
ts.insert(newNodes(50));
ts.insert(newNodes(20));
ts.insert(newNodes(80));
ts.insert(newNodes(60));
ts.insert(newNodes(90));
ts.insert(newNodes(30));
ts.insert(newNodes(40));
ts.insert(newNodes(70));
ts.insert(newNodes(100));
ts.insert(newNodes(10));
}
}