Exemple #1
0
 private static void Add(BinarySearchNode <T> parent, T childValue)
 {
     if (parent.Key.CompareTo(childValue) >= 0)
     {
         if (parent.LeftChild != null)
         {
             Add(parent.LeftChild, childValue);
         }
         else
         {
             parent.LeftChild = new BinarySearchNode <T>(childValue)
             {
                 Parent = parent
             };
         }
     }
     else
     {
         if (parent.RigthChild != null)
         {
             Add(parent.RigthChild, childValue);
         }
         else
         {
             parent.RigthChild = new BinarySearchNode <T>(childValue)
             {
                 Parent = parent
             };
         }
     }
 }
Exemple #2
0
        public static BinarySearchNode <T> GetMinimum <T>(BinarySearchNode <T> node) where T : IComparable
        {
            BinarySearchNode <T> result = node;

            while (result.LeftChild != null)
            {
                result = result.LeftChild;
            }

            return(result);
        }
Exemple #3
0
        private void GetPostorderTreeWalk(BinarySearchNode <T> node)
        {
            if (node.LeftChild != null)
            {
                GetPostorderTreeWalk(node.LeftChild);
            }

            if (node.RigthChild != null)
            {
                GetPostorderTreeWalk(node.RigthChild);
            }

            treeWalk.Add(node.Key);
        }
Exemple #4
0
        public BinarySearchTree(T[] values)
        {
            if (values == null)
            {
                return;
            }

            this.count = values.Length;

            this.Parent = new BinarySearchNode <T>(values[0]);

            for (int i = 1; i < values.Length; i++)
            {
                Add(this.Parent, values[i]);
            }
        }
Exemple #5
0
        public static BinarySearchNode <T> LookFor <T>(BinarySearchNode <T> rootNode, T value) where T : IComparable
        {
            if (rootNode.Key.Equals(value))
            {
                return(rootNode);
            }
            else if (rootNode.Key.CompareTo(value) > 0 && rootNode.LeftChild != null)
            {
                return(LookFor(rootNode.LeftChild, value));
            }
            else if (rootNode.Key.CompareTo(value) < 0 && rootNode.RigthChild != null)
            {
                return(LookFor(rootNode.RigthChild, value));
            }

            return(null);
        }
Exemple #6
0
        public static BinarySearchNode <T> LookFor <T>(BinarySearchNode <T> rootNode, BinarySearchNode <T> node) where T : IComparable
        {
            //Check the difference with rootNode.Equal(node)
            if (object.ReferenceEquals(rootNode, node))
            {
                return(rootNode);
            }
            else if (rootNode.Key.CompareTo(node.Key) > 0 && rootNode.LeftChild != null)
            {
                return(LookFor(rootNode.LeftChild, node));
            }
            else if (rootNode.Key.CompareTo(node.Key) < 0 && rootNode.RigthChild != null)
            {
                return(LookFor(rootNode.RigthChild, node));
            }

            return(null);
        }
Exemple #7
0
        public static BinarySearchNode <T> GetPredecessor <T>(BinarySearchNode <T> node) where T : IComparable
        {
            if (node.LeftChild != null)
            {
                return(TreeOperation.GetMaximum(node.LeftChild));
            }

            BinarySearchNode <T> result = node.Parent;

            while (result != null && result.LeftChild == node)
            {
                node = result;

                result = node.Parent;
            }

            return(result);
        }
Exemple #8
0
 public virtual void Delete(BinarySearchNode <T> newNode)
 {
 }
Exemple #9
0
 public BinarySearchNode <T> GetPredecessor(BinarySearchNode <T> node)
 {
     return(TreeOperation.GetPredecessor(node));
 }