Exemplo n.º 1
0
        private void RemoveTree(BinarySearchTreeNode <AnyType> SubTree)
        {
            if (SubTree != null)
            {
                if (SubTree.Parent != null)
                {
                    if (SubTree.Parent.Left == SubTree)
                    {
                        SubTree.Parent.Left = null;
                    }
                    if (SubTree.Parent.Right == SubTree)
                    {
                        SubTree.Parent.Right = null;
                    }
                }

                if (SubTree.Left != null)
                {
                    RemoveTree(SubTree.Left);
                }
                if (SubTree.Right != null)
                {
                    RemoveTree(SubTree.Right);
                }

                if (SubTree.IsNotEmpty)
                {
                    SubTree.RemoveAll();
                    this.ActiveSize--;
                }

                SubTree = null;

                this.TotalSize--;
            }
        }
Exemplo n.º 2
0
        public BinarySearchTreeNode <AnyType> GetMaxNodeActive(BinarySearchTreeNode <AnyType> SubTree)
        {
            BinarySearchTreeNode <AnyType> Result = SubTree;

            if (Result != null)
            {
                while (Result.Right != null)
                {
                    Result = Result.Right;
                }

                while (Result.IsEmpty && Result.Parent != null && Result != SubTree)
                {
                    Result = Result.Parent;
                }

                while (Result.IsEmpty && Result.Parent == null && Result == SubTree)
                {
                    Result = GetMaxNodeActive(Result.Left);
                }
            }

            return(Result);
        }
Exemplo n.º 3
0
        private BinarySearchTreeNode <AnyType> Find(AnyType Data, BinarySearchTreeNode <AnyType> SubTree)
        {
            BinarySearchTreeNode <AnyType> ResultNode = null;

            if (SubTree != null)
            {
                if (Data.CompareTo(SubTree.Data) == 0 && ResultNode == null)
                {
                    ResultNode = SubTree;
                }

                if (Data.CompareTo(SubTree.Data) < 0 && SubTree.Left != null && ResultNode == null)
                {
                    ResultNode = Find(Data, SubTree.Left);
                }

                if (Data.CompareTo(SubTree.Data) > 0 && SubTree.Right != null && ResultNode == null)
                {
                    ResultNode = Find(Data, SubTree.Right);
                }
            }

            return(ResultNode);
        }
Exemplo n.º 4
0
        private void ConvertTreeToDictionary(Dictionary <AnyType, uint> IndexAndCounts, BinarySearchTreeNode <AnyType> SubTree)
        {
            if (SubTree != null)
            {
                ConvertTreeToDictionary(IndexAndCounts, SubTree.Left);

                IndexAndCounts.Add(SubTree.Data, SubTree.CollisionCount);

                ConvertTreeToDictionary(IndexAndCounts, SubTree.Right);
            }
        }
Exemplo n.º 5
0
        private BinarySearchTreeNode <AnyType> Add(AnyType Data, BinarySearchTreeNode <AnyType> SubTree)
        {
            if (SubTree == null)
            {
                BinarySearchTreeNode <AnyType> NewNode = new BinarySearchTreeNode <AnyType>(Data, SubTree);

                this.RootNode = NewNode;

                this.ActiveSize++;
                this.TotalSize++;

                return(NewNode);
            }
            else
            {
                if (Data.CompareTo(SubTree.Data) == 0)
                {
                    if (SubTree.IsEmpty)
                    {
                        SubTree.InsertOne();

                        this.ActiveSize++;
                    }
                    else
                    {
                        SubTree.InsertOne();
                    }

                    return(SubTree);
                }

                if (Data.CompareTo(SubTree.Data) < 0)
                {
                    if (SubTree.Left != null)
                    {
                        return(Add(Data, SubTree.Left));
                    }
                    else
                    {
                        BinarySearchTreeNode <AnyType> NewNode = new BinarySearchTreeNode <AnyType>(Data, SubTree);

                        SubTree.Left = NewNode;

                        this.ActiveSize++;
                        this.TotalSize++;

                        return(NewNode);
                    }
                }

                if (Data.CompareTo(SubTree.Data) > 0)
                {
                    if (SubTree.Right != null)
                    {
                        return(Add(Data, SubTree.Right));
                    }
                    else
                    {
                        BinarySearchTreeNode <AnyType> NewNode = new BinarySearchTreeNode <AnyType>(Data, SubTree);

                        SubTree.Right = NewNode;

                        this.ActiveSize++;
                        this.TotalSize++;

                        return(NewNode);
                    }
                }

                return(null);
            }
        }