Exemplo n.º 1
0
        public void Insert(T item)
        {
            if (this.Root == null)
            {
                this.Root = new BinarySearchTreeNode <T>(item);
            }

            else
            {
                BinarySearchTreeNode <T> basenode = this.Root;
                BinarySearchTreeNode <T> parent   = null;
                int?cmp = null;

                while (basenode != null)
                {
                    parent = basenode;
                    if ((cmp = item.CompareTo(basenode.Item)) >= 0)
                    {
                        basenode = basenode.RightChild;
                    }
                    else
                    {
                        basenode = basenode.LeftChild;
                    }
                }

                BinarySearchTreeNode <T> node = new BinarySearchTreeNode <T>(item);
                node.Parent = parent;
                if (cmp >= 0)
                {
                    parent.RightChild = node;
                }
                else
                {
                    parent.LeftChild = node;
                }
                Console.Out.WriteLine("inserting below {0}", parent.Item);
            }

            this.Length += 1;
        }
Exemplo n.º 2
0
        private void ToBreadthFirst(BinarySearchTreeNode <T> node, IList <T> list)
        {
            IQueue <BinarySearchTreeNode <T> > queue = new Queue <BinarySearchTreeNode <T> >();

            queue.Enqueue(node);
            BinarySearchTreeNode <T> current;

            while (!queue.Empty)
            {
                current = queue.Dequeue();
                list.Add(current.Item);
                if (current.LeftChild != null)
                {
                    queue.Enqueue(current.LeftChild);
                }
                if (current.RightChild != null)
                {
                    queue.Enqueue(current.RightChild);
                }
            }
        }
Exemplo n.º 3
0
        private void RemoveNode(BinarySearchTreeNode <T> node)
        {
            if (node.RightChild == null && node.LeftChild == null)
            {
                if (node.Parent.RightChild == node)
                {
                    node.Parent.RightChild = null;
                }
                else
                {
                    node.Parent.LeftChild = null;
                }
            }

            else if (node.RightChild != null && node.LeftChild != null)
            {
                BinarySearchTreeNode <T> successor = node.Successor;
                T auxiliar = node.Item;
                node.Item      = successor.Item;
                successor.Item = auxiliar;
                this.RemoveNode(successor);
            }

            else             //then it has only one child
            {
                BinarySearchTreeNode <T> child = (node.RightChild == null ? node.LeftChild : node.RightChild);
                if (node.Parent.RightChild == node)
                {
                    node.Parent.RightChild = child;
                }
                else
                {
                    node.Parent.LeftChild = child;
                }
                child.Parent = node.Parent;
            }
        }
Exemplo n.º 4
0
 public void Clear()
 {
     this.Root   = null;
     this.Length = 0;
 }