java数据结构(二叉树)

时间:2022-02-05 10:22:55

Node节点:

 public class Node {
public long data;
public String sData;
public Node leftChild;
public Node rightChild;
public Node(long data,String sData) {
this.data = data;
this.sData = sData;
}
}

Tree:

 public class Tree {
public Node root; public void insert(long value,String sValue){
Node newNode = new Node(value,sValue);
Node current = root;
Node parent;
if(root == null){
root = newNode;
return;
}else{
while(true){
parent = current;
if(current.data > value){
current = current.leftChild;
if(current == null){
parent.leftChild = newNode;
return ;
}
}else{
current = current.rightChild;
if(current == null){
parent.rightChild = newNode;
return;
}
}
}
}
} public Node find(long value){
Node current = root;
while(current.data != value){
if(current.data > value){
current = current.leftChild;
}else{
current = current.rightChild;
}
if(current == null){
return null;
}
}
return current;
}
}