Пример #1
0
        private bool AddChild(Node <T> childNode)
        {
            if (childNode.Key.CompareTo(this.Key) > 0)
            {
                //child node key is greater than this key
                if (RightChildNode is null)
                {
                    RightChildNode = childNode;
                    return(true);
                }

                return(RightChildNode.AddChild(childNode));
            }
            else if (childNode.Key.CompareTo(this.Key) < 0)
            {
                //child node key is lesser than this key
                if (LeftChildNode is null)
                {
                    LeftChildNode = childNode;
                    return(true);
                }

                return(LeftChildNode.AddChild(childNode));
            }
            else
            {
                //child node key is equal to this key
                //do nothing
                return(false);
            }
        }
Пример #2
0
        public int Balance()
        {
            var leftWeight  = LeftChildNode.Balance();
            var rightWeight = RightChildNode.Balance();

            var ratio = (double)leftWeight / (leftWeight + rightWeight);

            Position = Length * (1 - ratio);

            return(leftWeight + rightWeight);
        }
Пример #3
0
        public Node <T> GetNodeByKey(T key)
        {
            if (key.CompareTo(this.Key) > 0 && !(RightChildNode is null))
            {
                //child node is greater than this key
                return(RightChildNode.GetNodeByKey(key));
            }
            else if (key.CompareTo(this.Key) < 0 && !(LeftChildNode is null))
            {
                //child node is smaller than this key
                return(LeftChildNode.GetNodeByKey(key));
            }

            return(key.CompareTo(this.Key) == 0 ? this : null);
        }
Пример #4
0
        public void Print(int depth = 0)
        {
            var msg = "";

            for (var i = 0; i < depth; i++)
            {
                msg += "\t";
            }

            msg += ToString();

            Console.WriteLine(msg);

            LeftChildNode.Print(depth + 1);
            RightChildNode.Print(depth + 1);
        }
Пример #5
0
        //needs fixing
        public int NumberOfChildren()
        {
            if (NumberOfDirectChilren() == 0)
            {
                return(0);
            }

            var result = this.NumberOfDirectChilren();

            if (!(this.LeftChildNode is null))
            {
                result += LeftChildNode.NumberOfChildren();
            }

            if (!(this.RightChildNode is null))
            {
                result += RightChildNode.NumberOfChildren();
            }

            return(result);
        }
Пример #6
0
        private Node <T> DeleteNodeByKey(T key, out bool noChange)
        {
            noChange = false;

            if (key.CompareTo(this.Key) == 0)
            {
                switch (this.NumberOfDirectChilren())
                {
                case 0:
                    return(null);

                case 1:
                    if (this.LeftChildNode is null)
                    {
                        return(this.RightChildNode);
                    }
                    else
                    {
                        return(this.LeftChildNode);
                    }

                case 2:
                    this.Key      = LeftChildNode.MaximalNode().Key;
                    LeftChildNode = LeftChildNode.DeleteNodeByKey(this.Key, out noChange);
                    break;
                }
            }
            else if (key.CompareTo(this.Key) > 0)
            {
                RightChildNode = this.RightChildNode.DeleteNodeByKey(key, out noChange);
            }
            else if (key.CompareTo(this.Key) < 0)
            {
                LeftChildNode = this.LeftChildNode.DeleteNodeByKey(key, out noChange);
            }

            noChange = true;
            return(this);
        }