j结点类;
public class Node {
public int id;
public String name;
public Node leftChild;
public Node rightChild;
public Node(int id, String name){
this.id = id;
this.name = name;
}
public void display(){
System.out.println("id-->" + id + ", name-->" + name);
}
}
主类:
public class Tree {
private Node root;
public boolean isEmpty(){
if(null == root)
return true;
return false;
}
public void insert(int id, String name){
Node node = new Node(id, name);
boolean isLeftChild = false;
if(null == root){
root = node;
}else{
Node parent = root;
Node current = root;
while(null != current){
parent = current;
if(current.id > id){
current = current.leftChild;
isLeftChild = true;
}else{
current = current.rightChild;
isLeftChild = false;
}
}
if(isLeftChild){
parent.leftChild = node;
}else{
parent.rightChild = node;
}
}
}
public Node find(int id){
if(isEmpty())
return null;
Node current = root;
while(null != current){
if(current.id == id){
return current;
}else if(current.id > id){
current = current.leftChild;
}else{
current = current.rightChild;
}
}
return null;
}
public boolean delete(int id){
if(isEmpty())
return false;
Node parent = root;
Node current = root;
boolean isLeftChild = true;
while(current.id != id){
if(null == current)
return false;
parent = current;
if(current.id < id){
current = current.leftChild;
isLeftChild = true;
}else{
current = current.rightChild;
isLeftChild = false;
}
}
/*1. none child*/
if(null == current.leftChild && null == current.rightChild){
if(current == root){
current = null;
}else if(isLeftChild){
parent.leftChild = null;
}else{
parent.rightChild = null;
}
}else if(null == current.leftChild){
if(root == current){
root = current.rightChild;
}else if(isLeftChild){
parent.leftChild = current.rightChild;
}else{
parent.rightChild = current.rightChild;
}
}else if(null == current.rightChild){
if(root == current){
root = current.leftChild;
}else if(isLeftChild){
parent.leftChild = current.leftChild;
}else{
parent.rightChild = current.leftChild;
}
}else{
/*待删除节点有两个孩子*/
Node successor = getSuccessor(current);
if(root == current){
root = successor;
}else if(isLeftChild){
parent.leftChild = successor;
}else{
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;
}
return true;
}
/**
* 返回中序遍历的后续节点
* @param delNode
* @return
*/
private Node getSuccessor(Node delNode){
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.rightChild;
while(null != current){
successorParent = successor;
successor = current;
current = current.leftChild;
}
if(successor != delNode.rightChild){
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;
}
return successor;
}
/*
* 中序遍历
*/
public void inOrder(Node node){
if(null == node)
return;
inOrder(node.leftChild);
node.display();
inOrder(node.rightChild);
}
public void display(){
inOrder(root);
}
public static void main(String[] args){
Tree tree = new Tree();
tree.insert(1, "Jordan");
tree.insert(2, "James");
tree.insert(3, "kobe");
tree.insert(5, "wade");
tree.insert(10, "curry");
tree.display();
}
}