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;
            }
  • 相关阅读:
    【调查】用户眼中已经消亡的十大IT技术
    Asp.net Cache
    双卡已落伍 三卡三待全键盘手机亮相
    C#修改本机的IP,网关等网络地址的代码片段
    【ZT】实战.NET大规模网站架构:Web加速器
    【面霸】面试ERP实施顾问的三招“必杀技”
    数据库优化使用索引优化存储过程
    【備查】informix sql函数使用说明大全
    “8月3日我们要过男人节”
    Asp.Net网站优化:数据库索引优化
  • 原文地址:https://www.cnblogs.com/xuyanran/p/8242250.html
Copyright © 2011-2022 走看看