Node節(jié)點(diǎn)
public class Node {
int data; //節(jié)點(diǎn)數(shù)據(jù)
Node left; //左子節(jié)點(diǎn)
Node right; //右子節(jié)點(diǎn)
public Node(int data){
this.data = data;
}
}
實(shí)現(xiàn)
BinaryTree
package com.example.demo.day07.binary;
import com.example.demo.day07.binary.Node;
import com.example.demo.day07.binary.Tree;
public class BinaryTreeNode implements Tree {
private Node root;
@Override
public Node find(int key) {
Node current = root;
while (current!=null){
if (current.data>key){
current = current.left;
}
else if (current.data<key){
current=current.right;
}
else {
return current;
}
}
return null;
}
@Override
public boolean insert(int data) {
Node node = new Node(data);
Node current = root;
while (current!=null){
if (current.data>data){
current = current.left;
}
else if (current.data<data){
current = current.right;
}
else {
current = current;
}
}
current = node;
return true;
}
}