简单的顺序二叉树,实现增删改查
package TreeDemos;
import java.util.*;
public class TreeDemo<K extends Comparable<K>,V > {
// 建立排序二叉树,并实现增删改查功能
// 孩子表示法
public static class Node<K extends Comparable<K>, V> {
public K key;
public V value;
public Node<K, V> lchlid;
public Node<K, V> rchlid;
public Node(K key, V value, Node<K, V> lchlid, Node<K, V> rchlid) {
this.key = key;
this.value = value;
this.lchlid = lchlid;
this.rchlid = rchlid;
}
public void put(Node<K, V> newNode) {
if (newNode.key.compareTo(this.key) < 0) {
if (lchlid != null) {
lchlid.put(newNode);
} else {
lchlid = newNode;
}
} else if (newNode.key.compareTo(this.key) > 0) {
if (rchlid != null) {
rchlid.put(newNode);
} else {
rchlid = newNode;
}
} else {
value = newNode.value;
}
}
public ArrayList<Node<K, V>> nodes = new ArrayList<Node<K, V>>();
public void traversal(Node<K, V> root){
// 将遍历结果保存到ArrayList中
if(lchlid!=null){
lchlid.traversal(root);
}
root.nodes.add(this);
if(rchlid!=null){
rchlid.traversal(root);
}
}
public Node<K, V> find(K key) {
if (key.compareTo(this.key) < 0 && lchlid != null) {
return lchlid.find(key);
} else if (key.compareTo(this.key) > 0 && rchlid != null) {
return rchlid.find(key);
} else if (key.compareTo(this.key) == 0) {
return this;
} else {
return null;
}
}
public void delete(K key, Node<K, V> parent,Node<K, V> root) {
if (key.compareTo(this.key) < 0 && lchlid != null) {
lchlid.delete(key, this ,root);
} else if (key.compareTo(this.key) > 0 && rchlid != null) {
rchlid.delete(key, this,root);
} else if (key.compareTo(this.key) == 0) {
delete1(parent,root);
} else {
System.out.println("此节点不存在");
}
}
public Node<K,V> getPre(){
Node<K,V> l=lchlid;
while (l.rchlid!=null){
l=l.rchlid;
}
return l;
}
public void delete1(Node<K, V> parent,Node<K, V> root){
// 分为三种情况 第一种:叶子节点,直接删除; 第二种:只含有一个子节点,继承 ,第三种:含有两个节点
if (lchlid == null && rchlid == null) {
if (parent.lchlid == this) {
parent.lchlid = null;
}
if (parent.rchlid == this) {
parent.rchlid = null;
}
} else if (lchlid != null && rchlid == null) {
value = lchlid.value;
this.key=lchlid.key;
lchlid = null;
} else if (lchlid == null) {
value = rchlid.value;
this.key=rchlid.key;
rchlid = null;
} else {
Node<K,V> pre=getPre();
root.delete(pre.key,null,root);
this.value=pre.value;
this.key=pre.key;
}
}
public String getString() {
return "[" + key + ":" + value + "]";
}
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (lchlid != null) {
stringBuilder.append(lchlid.toString());
}
stringBuilder.append(getString());
if (rchlid != null) {
stringBuilder.append(rchlid.toString());
}
return stringBuilder.toString();
}
}
public Node<K,V> root;
public TreeDemo(ArrayList<Node<K,V>> nodes){
for(Node<K,V> node :nodes){
put(node);
}
}
public void put(Node<K,V> node){
if(root==null){
root=node;
}
else{
root.put(node);
}
}
public Node<K,V> find(K key){
return root.find(key);
}
public void delete(K key){
root.delete(key,null,root);
}
public String toString(){
// 中序遍历
return root.toString();
}
public static void main(String[] args) {
ArrayList<Node<Integer,String>> nodes=new ArrayList<Node<Integer,String>>();
nodes.add(new Node<Integer,String>(324,"A",null,null));
nodes.add(new Node<Integer,String>(83,"B",null,null));
nodes.add(new Node<Integer,String>(87,"J",null,null));
nodes.add(new Node<Integer,String>(5432,"F",null,null));
nodes.add(new Node<Integer,String>(34,"G",null,null));
nodes.add(new Node<Integer,String>(44,"K",null,null));
nodes.add(new Node<Integer,String>(56,"K",null,null));
nodes.add(new Node<Integer,String>(754,"K",null,null));
nodes.add(new Node<Integer,String>(45,"K",null,null));
TreeDemo<Integer,String> myTree=new TreeDemo<>(nodes);
System.out.println(myTree);
Node<Integer,String> res= myTree.find(56);
System.out.println(res.getString());
myTree.delete(56);
System.out.println(myTree);
}
}