zoukankan      html  css  js  c++  java
  • Binary Search Tree Learning Summary

    BST Definition

    BST is short for Binary Search Tree, by definition, the value of right node is always greater or equal to the root node, the value of left node is always less than the root node.


    BST data structure and insert data

        public class TreeNode<T>
        {
            public T data
            {
                get;
                set;
            }
                    
            public TreeNode<T> leftChild
            {
                get;
                set;
            }
    
            public TreeNode<T> rightChild
            {
                get;
                set;
            }
        }
     public class BinarySearchTree<T> where T: IComparable
        {
            public TreeNode<T> root
            {
                get;
                set;
            }
    
            public BinarySearchTree()
            {
                root = null;
            }
    
            public void Insert(T data)
            {
                TreeNode<T> temp = new TreeNode<T>();
                temp.data = data;
                TreeNode<T> current = root;
                TreeNode<T> parent = null;
    
                if (root == null)
                {
                    root = temp;
                    return;
                }
    
                while (current != null)
                {
                    parent = current;
                    if (current.data.CompareTo(data) > 0)
                    {
                        current = current.leftChild;
                    }
                    else
                    {
                        current = current.rightChild;
                    }
                } // end of while
    
                if (data.CompareTo(parent.data) >= 0)
                {
                    parent.rightChild = temp;
                }
                else
                {
                    parent.leftChild = temp;
                }
            }

     insert element to BST

    1) if nothing is there, current element will be root node.  

    2) compare the value in BST, find the position of to be inserted, also track the parent reference. if found the position, compare the target value with parent node value, if less, insert to left, otherwise, insert to the right child.


    Find Min from BST

    Brutal force is to traverse the tree and also record the min value.  However for BST, need to leverage its feature.  becuase its value is kind of sorted.

    so find min, continue find left until leaf node, which is the smallest value.

    same pattern as above, if asked to find max, then starting from root node, find the right leaf node, then that one is the biggest value.

    This is good optimization as you save some steps to traverse the whole tree.


    Delete node with target value, which has two children

    1. Find the target node firstly

    2. if Found, check if in-order successor node is right child of to be deleted node, if is, replace the target node.

    3. If the in-order successor node is not right child, replace the left node with root node.

    4. Please make sure, current's parent left and child node reference needs be pointed to in-order successor node

    5. Whenever you change the node, let it pointing to different node, please check the one pointing to it is updated firstly.  we need ensure the sequence, otherwise, there might be loop reference.

          public bool FindAndDeleteNodeIfItHasTwoChild(T key)
            {
                if (this.root == null)
                {
                    return false;
                }
    
                TreeNode<T> current = this.root;
                TreeNode<T> parent = null;
                TreeNode<T> nextSuccessorNode = null;
                TreeNode<T> nextSuccessorParentNode = null;
    
                // locate the node
                while (current != null)
                {
                    if (key.CompareTo(current.data) == 0)
                    {
                        if (current.leftChild != null && current.rightChild != null)
                        {
                            FindInOrderNextSuccessor(current.rightChild, ref nextSuccessorNode, ref nextSuccessorParentNode);
    
                            if (current.rightChild == nextSuccessorNode)
                            {
                                if (parent.leftChild == current)
                                {
                                    parent.leftChild = nextSuccessorNode;
                                }
                                else if (parent.rightChild == current)
                                {
                                    parent.rightChild = nextSuccessorNode;
                                }
    
                                nextSuccessorNode.leftChild = current.leftChild;
    
                                if (current == root)
                                {
                                    this.root = nextSuccessorNode;
                                }
                                Console.WriteLine("NEXT SUCCESSOR node is right child of target node, replace");
                            }
                            else
                            {
                                // move up
                                nextSuccessorParentNode.leftChild = nextSuccessorNode.rightChild;
    
                                nextSuccessorNode.leftChild = current.leftChild;
                                nextSuccessorNode.rightChild = current.rightChild;
                                if (parent != null)
                                {
                                    if (parent.leftChild == current)
                                    {
                                        parent.leftChild = nextSuccessorNode;
                                    }
                                    else if (parent.rightChild == current)
                                    {
                                        parent.rightChild = nextSuccessorNode;
                                    }
                                }
    
                                if (current == root)
                                {
                                    this.root = nextSuccessorNode;
                                }
                            }
    
                            current.leftChild = null;
                            current.rightChild = null;
    
                            return true;
                        }
                        else
                        {
                            Console.WriteLine("found the value, but it does not have two childs, return.");
                            return false;
                        }
                    }
    
                    parent = current;
                    if (key.CompareTo(current.data) >= 0)
                    {
                        current = current.rightChild;
                    }
                    else
                    {
                        current = current.leftChild;
                    }
                }
    
                return false;
            }
  • 相关阅读:
    PHP入门(二)
    PHP入门(一)
    TypeScript作为前端开发你必须学习的技能(三)
    东北师范大学-构建之法-20201022作业成绩
    20201108-东北师范大学-助教-周总结-第8次
    20201103-东北师范大学-助教-周总结-第7次
    东北师范大学-构建之法-20201015作业成绩
    20201026-东北师范大学-助教-周总结-第6次
    东北师范大学-构建之法-20201008作业成绩
    20201019-东北师范大学-助教-周总结-第5次
  • 原文地址:https://www.cnblogs.com/xuyanran/p/8242250.html
Copyright © 2011-2022 走看看