예제 #1
0
            private BiTree <T> FindFirstUnbalancedNode()
            {
                if (!isBalancedNode)
                {
                    return(this);
                }
                else
                {
                    BiTree <T> toReturn = null;

                    if (LeftChild != null)
                    {
                        toReturn = LeftChild.FindFirstUnbalancedNode();
                    }

                    if (toReturn != null)
                    {
                        return(toReturn);
                    }

                    if (RightChild != null)
                    {
                        toReturn = RightChild.FindFirstUnbalancedNode();
                    }

                    if (toReturn != null)
                    {
                        return(toReturn);
                    }

                    return(null);
                }
            }
예제 #2
0
        static void TestBiTree()
        {
            BiTree <int> temp = new BiTree <int>(62);

            temp.AddLeftChild(58);
            temp.AddRightChild(88);
            temp.LeftChild.AddLeftChild(47);
            temp.LeftChild.LeftChild.AddLeftChild(35);
            temp.LeftChild.LeftChild.AddRightChild(51);
            temp.LeftChild.LeftChild.LeftChild.AddRightChild(37);
            temp.RightChild.AddLeftChild(73);
            temp.RightChild.AddRightChild(99);
            temp.RightChild.RightChild.AddLeftChild(93);

            temp.PreOrderDisplay();
            Console.WriteLine();
            temp.InOrderDisplay();
            Console.WriteLine();
            temp.PostOrderDisplay();
            Console.WriteLine();

            Console.WriteLine();

            temp = new BiTree <int>(62);
            temp.AddAsSortTree(58);
            temp.AddAsSortTree(88);
            Console.WriteLine(temp.IsAVL);
            temp.AddAsSortTree(47);
            Console.WriteLine(temp.IsAVL);
            temp.AddAsSortTree(35);
            temp.AddAsSortTree(51);
            Console.WriteLine(temp.IsAVL);
            temp.AddAsSortTree(37);
            temp.AddAsSortTree(73);
            temp.AddAsSortTree(99);
            temp.AddAsSortTree(93);
            Console.WriteLine(temp.IsAVL);

            temp.PreOrderDisplay();
            Console.WriteLine();
            temp.InOrderDisplay();
            Console.WriteLine();
            temp.PostOrderDisplay();
            Console.WriteLine();

            temp.FindInSortTree(47);

            var newTree = BiTree <int> .AdjustToAVL(temp);

            Console.WriteLine(newTree.IsAVL);

            newTree.PreOrderDisplay();
            Console.WriteLine();
            newTree.InOrderDisplay();
            Console.WriteLine();
            newTree.PostOrderDisplay();
            Console.WriteLine();

            newTree.FindInSortTree(47);
        }
예제 #3
0
            public bool FindInSortTree(T value)
            {
                if (IsEmpty)
                {
                    Console.WriteLine(value + " not found in a empty tree.");

                    return(false);
                }

                if (value.CompareTo(data) == 0)
                {
                    string     outputString     = data.ToString();
                    BiTree <T> targetNodesChild = this;

                    while (targetNodesChild.Parent != null)
                    {
                        outputString = targetNodesChild.Parent.Data + "->" + outputString;

                        targetNodesChild = targetNodesChild.Parent;
                    }

                    Console.WriteLine(value + " found in tree: " + outputString);

                    return(true);
                }
                else if (value.CompareTo(data) < 0)
                {
                    if (LeftChild != null)
                    {
                        return(LeftChild.FindInSortTree(value));
                    }
                    else
                    {
                        Console.WriteLine(value + " not found.");

                        return(false);
                    }
                }
                else
                {
                    if (RightChild != null)
                    {
                        return(RightChild.FindInSortTree(value));
                    }
                    else
                    {
                        Console.WriteLine(value + " not found.");

                        return(false);
                    }
                }
            }
예제 #4
0
            public void AddRightChild(T value)
            {
                if (IsEmpty)
                {
                    throw new Exception("Cannot add child to a empty tree.");
                }

                if (RightChild != null)
                {
                    throw new Exception("Child already exists.");
                }

                RightChild        = new BiTree <T>(value);
                RightChild.Parent = this;
            }
예제 #5
0
            public static BiTree <T> AdjustToAVL(BiTree <T> p_rootNode)
            {
                if (p_rootNode.IsEmpty)
                {
                    return(p_rootNode);
                }

                BiTree <T> rootNode      = p_rootNode;
                BiTree <T> UnbalanceNode = rootNode.FindFirstUnbalancedNode();

                while (UnbalanceNode != null)
                {
                    var temp = UnbalanceNode.AdjustToAVL();
                    if (temp != null)
                    {
                        rootNode = temp;
                    }

                    UnbalanceNode = rootNode.FindFirstUnbalancedNode();
                }

                return(rootNode);
            }