Java数据结构与算法

Java数据结构:二叉排序树(BST)

2020-07-18  本文已影响0人  Patarw

一、基本介绍

二、创建一颗二叉树

 //节点类
class Node{
 public int value; 
 public Node left; //左节点
 public Node right; //右节点
 @Override
 public String toString() {
    return "Node [value=" + value + "]";
  }
 public Node(int value) {
    super();
    this.value = value;
 }

//添加方法
public void add(Node node) {
    if(this.value > node.value) {
        if(this.left == null) {
            this.left = node;
        }else {
            this.left.add(node);
        }
    }else {
        if(this.right == null) {
            this.right = node;
        }else {
            this.right.add(node);
        }
    }
}   
}
 //中序遍历
public void midOrder() {
    if(this.left != null) {
        this.left.midOrder();
    }
    
    System.out.println(this);
    
    if(this.right != null) {
        this.right.midOrder();
    }
}

然后再到BinarySortTree类中添加根节点的遍历:

    public void midOrder() {
        this.root.midOrder();       
  }

再到main方法中调用输出结果:
1 -》3 -》5 -》7 -》9 -》10 -》12

三、删除一个节点

删除需要三种情况来考虑,

代码:
节点类中:

    //删除方法
public void delete(int value,Node node1) {
    if(this.value > value && this.left != null) {           
        this.left.delete(value,node1);  
    }else if(this.value < value && this.right != null) {
        this.right.delete(value,node1);
    }else if(this.value == value) {
        Node node = node1;
        
        //1.当节点为叶子节点时
        if(node != null) {
        if(this.right == null && this.left == null) {
            if(node.value > value) {
                node.left = null;
            }else if(node.value <= value){
                node.right = null;
            }
        }
        //2.当节点为带单子节点时
        else if(this.right != null && this.left == null) {              
            if(node.value > value) {
                node.left = this.right;
            }else if(node.value <= value){
                node.right = this.right;
            }               
        }else if(this.left != null && this.right == null) {
            if(node.value > value) {
                node.left = this.left;
            }else if(node.value <= value){
                node.right = this.left;
            }   
        }
        //3.当节点有左右节点时
        else if(this.right != null && this.left != null){
            Node temp = null;
            if(node.value > value) {                    
                node.left = this.right;                     
                this.right.add(this.left);
            }else if(node.value <= value){                  
                node.right = this.right;
                this.right.add(this.left);
            }   
        }
        }
    }
}

//查询父节点
public Node findParent(int value) {
    if(this.value > value && this.left != null) {   
       if(this.left.value == value && this.left != null){
            return this;
        }else {
            return this.left.findParent(value);
        }
    }else if(this.value < value && this.right != null) {
          if(this.right.value == value){
                return this;
            }else {
                return this.right.findParent(value);    
            }
    }else {     
        return null;
    }

}

BinarySortTree中

        public void deleteNode(int value) {
    if(this.root.value == value) {                  
            //1.当节点为叶子节点时               
            if(this.root.right == null && this.root.left == null) {
                this.root = null;
            }
            //2.当节点为带单子节点时
            else if(this.root.right != null && this.root.left == null) {                                    
                this.root = this.root.right;            
            }else if(this.root.left != null && this.root.right == null) {
                this.root = this.root.left;
            }
            //3.当节点有左右节点时
            else if(this.root.right != null && this.root.left != null){                                                             
                    this.root.right.add(this.root.left);    
                    this.root = this.root.right;            
            }
            this.root.midOrder();
            }else {         
              root.delete(value,this.root.findParent(value));
              root.midOrder();
    }
}
上一篇下一篇

猜你喜欢

热点阅读