二叉樹附加刪除算法

public class Tree {
    
    TreeNode last = null;
    TreeNode root = null;
    
    
    public Tree(int value){
        root = createNode(value);
    }
        
    //結構
     static class TreeNode{
         int data;
         TreeNode left;
         TreeNode right; 
     }
     
     //查找結點
     public TreeNode searchTreeNode(int key,TreeNode tree){
         TreeNode keyNode = null;
         if(tree == null){
             return tree;
         }
         if(tree.data == key){
             return tree;
         }else if(key < tree.data ){
             last = tree;
             keyNode = searchTreeNode(key,tree.left);
         }else if(key > tree.data){
             last = tree;
             keyNode = searchTreeNode(key,tree.right);
         }
         return keyNode;
     }
     
     
     //添加節點
     public void addTreeNode(int key){
         if(searchTreeNode(key,root) == null){
             TreeNode tree = createNode(key);
             if(key < last.data){
                 last.left = tree;
             }else if(key > last.data){
                 last.right = tree;
             }
         }
         last = null;
     }
     
     //遍歷
     public void iterate(TreeNode node){
        if(node ==null){
            return;
        }
        System.out.println(node.data);
        iterate(node.left);
        iterate(node.right);
     }
    
     public TreeNode createNode(int key){
        TreeNode tree = new TreeNode();
        tree.data = key;
        return tree;
     }
     
     public void removeNode(int key){
         TreeNode tree = searchTreeNode(key,root);
         if(tree.left == null && tree.right == null){
             
         }
         //若是右節點爲空       當前節點取代左節點
         else if(tree.right == null){
             tree.data = tree.left.data;        // 把當前的左節點指向須要刪除的左節點的左節點
             tree.right = tree.left.right;        // 把刪除節點的右節點指向刪除節點的左節點右節點
             tree.data = tree.left.data;        // SET刪除節點的值
         }else if(tree.left  == null){
             tree.right = tree.right.right;
             tree.left = tree.right.left;
             tree.data = tree.right.data;
         }else{
             //若是左右都不爲空
             //得到右樹最小的一個
             TreeNode nodeRight = tree.right;
             TreeNode lastLeftParent = null;
             while(nodeRight.left != null){
                 lastLeftParent = nodeRight;
                 nodeRight = nodeRight.left;
             }
             
            
             tree.data = nodeRight.data;    
             if(lastLeftParent != null)
                 lastLeftParent.left = nodeRight.right;    // 連接右子樹
             else
                 tree.right = nodeRight.right;            // 連接右子樹
         }
     }
     
     public static void main(String[] args){
         Tree tree = new Tree(4);
         tree.addTreeNode(5);
         tree.addTreeNode(6);
         tree.addTreeNode(3);
         tree.addTreeNode(7);
         tree.addTreeNode(8);
         tree.addTreeNode(9);
        

//         System.out.println(tree.searchTreeNode(7, tree.root).data);
         System.out.println("---");
         tree.iterate(tree.root);
     }
     
}
相關文章
相關標籤/搜索