Example #1
0
        public ZBinaryTreeNode <T> Find(T data)
        {
            ZBinaryTreeNode <T> node    = null;
            ZBinaryTreeNode <T> current = Root;

            while (current != null)
            {
                int compareResult = current.Value.CompareTo(data);

                if (compareResult == 0)
                {
                    return(current);
                }
                else if (compareResult < 0)
                {
                    current = current.LeftNode;
                }
                else
                {
                    current = current.RightNode;
                }
            }

            return(node);
        }
Example #2
0
        private static ZBinaryTreeNode <T> BuildTree(List <T> preorderList, List <T> inorderList, int start, int end, int preIndex)
        {
            if (start > end)
            {
                return(null);
            }

            //Pick current node from preorder traversal using preIndex and increment preIndex
            ZBinaryTreeNode <T> node = new ZBinaryTreeNode <T>(preorderList[preIndex++]);

            //If this node has no children then return
            if (start == end)
            {
                return(node);
            }

            //Else find the index of this node in inorder traversal
            int inIndex = Search(inorderList, start, end, node.Value);

            //Using index in inorder traversal, construct left and right subtrees
            node.LeftNode  = BuildTree(inorderList, preorderList, start, inIndex - 1, preIndex);
            node.RightNode = BuildTree(inorderList, preorderList, inIndex + 1, end, preIndex);

            return(node);
        }
Example #3
0
        public List <T> LevelOrderList()
        {
            List <T> items = new List <T>();
            ZQueue <ZBinaryTreeNode <T> > que = new ZQueue <ZBinaryTreeNode <T> >();

            if (Root != null)
            {
                items.Add(Root.Value);
                if (Root.LeftNode != null)
                {
                    que.Enqueue(Root.LeftNode);
                }
                if (Root.RightNode != null)
                {
                    que.Enqueue(Root.RightNode);
                }
            }

            while (que.Count > 0)
            {
                ZBinaryTreeNode <T> node = que.Dequeue();
                items.Add(node.Value);
                if (Root.LeftNode != null)
                {
                    que.Enqueue(Root.LeftNode);
                }
                if (Root.RightNode != null)
                {
                    que.Enqueue(Root.RightNode);
                }
            }

            return(items);
        }
Example #4
0
        public ZBinaryTreeNode <T> FindParent(T data)
        {
            ZBinaryTreeNode <T> current = Root;
            ZBinaryTreeNode <T> parent  = null;

            if (Root.Value.Equals(data))
            {
                return(parent);
            }

            while (current != null)
            {
                int compareResult = current.Value.CompareTo(data);

                if (compareResult == 0)
                {
                    return(parent);
                }
                else if (compareResult < 0)
                {
                    parent  = current;
                    current = current.LeftNode;
                }
                else
                {
                    parent  = current;
                    current = current.RightNode;
                }
            }

            return(parent);
        }
Example #5
0
        public void Delete(T data)
        {
            ZBinaryTreeNode <T> node   = null;
            ZBinaryTreeNode <T> parent = FindParent(data);

            if (parent != null)
            {
                if (parent.LeftNode != null && parent.LeftNode.Value.Equals(data))
                {
                    node = parent.LeftNode;
                    //case1: has no children.
                    if (node.LeftNode == null && node.RightNode == null)
                    {
                        parent.LeftNode = null;
                    }
                    //case2: has left child but no right child.
                    else if (node.RightNode == null)
                    {
                        parent.LeftNode = node.LeftNode;
                    }
                    //case3: has right child and right child has no left child of it's own.
                    else if (node.RightNode.LeftNode == null)
                    {
                        parent.LeftNode = node.RightNode;
                    }
                    //case4: Finally, if the deleted node's right child does have a left child, then the deleted node needs to be replaced by the deleted node's right child's left-most descendant. That is, we replace the deleted node with the deleted node's right subtree's smallest value.
                    else
                    {
                        ZBinaryTreeNode <T> minNode = FindMin(node.RightNode);
                        parent.LeftNode = minNode;
                    }
                }
                else if (parent.RightNode != null && parent.RightNode.Value.Equals(data))
                {
                    node = parent.RightNode;
                    //case1: has no children.
                    if (node.LeftNode == null && node.RightNode == null)
                    {
                        parent.RightNode = null;
                    }
                    //case2: has left child but no right child.
                    else if (node.RightNode == null)
                    {
                        parent.RightNode = node.LeftNode;
                    }
                    //case3: has right child and right child has no left child of it's own.
                    else if (node.RightNode.LeftNode == null)
                    {
                        parent.RightNode = node.RightNode;
                    }
                    //case4: Finally, if the deleted node's right child does have a left child, then the deleted node needs to be replaced by the deleted node's right child's left-most descendant. That is, we replace the deleted node with the deleted node's right subtree's smallest value.
                    else
                    {
                        ZBinaryTreeNode <T> minNode = FindMin(node.RightNode);
                        parent.RightNode = minNode;
                    }
                }
            }
        }
Example #6
0
 private void PostOrder(ZBinaryTreeNode <T> node, List <T> items)
 {
     if (node != null)
     {
         PostOrder(node.LeftNode, items);
         PostOrder(node.RightNode, items);
         items.Add(node.Value);
     }
 }
Example #7
0
 public ZBinaryTreeNode <T> FindMin(ZBinaryTreeNode <T> node)
 {
     if (node.LeftNode == null)
     {
         return(node);
     }
     else
     {
         return(FindMax(node.LeftNode));
     }
 }
Example #8
0
        public void Add(T data)
        {
            ZBinaryTreeNode <T> current = Root;

            if (current == null)
            {
                Root = new ZBinaryTreeNode <T>(data);
            }
            else
            {
                while (true)
                {
                    int compareResult = current.Value.CompareTo(data);

                    if (compareResult < 0)
                    {
                        if (current.RightNode == null)
                        {
                            current.RightNode = new ZBinaryTreeNode <T>(data);
                            return;
                        }
                        else
                        {
                            current = current.RightNode;
                        }
                    }
                    if (compareResult > 0)
                    {
                        if (current.LeftNode == null)
                        {
                            current.LeftNode = new ZBinaryTreeNode <T>(data);
                            return;
                        }
                        else
                        {
                            current = current.LeftNode;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
Example #9
0
        private int CountChildren(ZBinaryTreeNode <T> node)
        {
            if (node == null)
            {
                return(0);
            }
            int left  = CountChildren(node.LeftNode);
            int right = CountChildren(node.RightNode);

            if (left >= right)
            {
                return(left + 1);
            }
            else
            {
                return(right + 1);
            }
        }
Example #10
0
 private bool GetAncesters(ZBinaryTreeNode <T> node, T data, List <ZBinaryTreeNode <T> > items)
 {
     if (node != null)
     {
         if (!node.Value.Equals(data))
         {
             if (GetAncesters(node.LeftNode, data, items) || GetAncesters(node.RightNode, data, items))
             {
                 items.Add(node);
                 return(true);
             }
         }
         else
         {
             return(true);
         }
     }
     return(false);
 }
Example #11
0
        public ZBinaryTreeNode <T> Search(T data)
        {
            ZBinaryTreeNode <T>           match = null;
            ZQueue <ZBinaryTreeNode <T> > que   = new ZQueue <ZBinaryTreeNode <T> >();

            if (Root != null)
            {
                if (data.Equals(Root.Value))
                {
                    return(Root);
                }

                if (Root.LeftNode != null)
                {
                    que.Enqueue(Root.LeftNode);
                }
                if (Root.RightNode != null)
                {
                    que.Enqueue(Root.RightNode);
                }
            }

            while (que.Count > 0)
            {
                ZBinaryTreeNode <T> node = que.Dequeue();
                if (data.Equals(node.Value))
                {
                    return(node);
                }

                if (Root.LeftNode != null)
                {
                    que.Enqueue(Root.LeftNode);
                }
                if (Root.RightNode != null)
                {
                    que.Enqueue(Root.RightNode);
                }
            }

            return(match);
        }
Example #12
0
        public int CountFullNodes()
        {
            int total = 0;
            ZQueue <ZBinaryTreeNode <T> > que = new ZQueue <ZBinaryTreeNode <T> >();

            if (Root != null)
            {
                if (Root.LeftNode != null && Root.RightNode != null)
                {
                    total++;
                }

                if (Root.LeftNode != null)
                {
                    que.Enqueue(Root.LeftNode);
                }
                if (Root.RightNode != null)
                {
                    que.Enqueue(Root.RightNode);
                }
            }

            while (que.Count > 0)
            {
                ZBinaryTreeNode <T> node = que.Dequeue();
                if (Root.LeftNode != null && Root.RightNode != null)
                {
                    total++;
                }

                if (Root.LeftNode != null)
                {
                    que.Enqueue(Root.LeftNode);
                }
                if (Root.RightNode != null)
                {
                    que.Enqueue(Root.RightNode);
                }
            }
            return(total);
        }
Example #13
0
 public ZBinaryTreeNode(T data, ZBinaryTreeNode <T> left, ZBinaryTreeNode <T> right)
 {
     this.Value     = data;
     this.LeftNode  = left;
     this.RightNode = right;
 }