Ejemplo n.º 1
0
        public void Generate(int depth)
        {
            this.n = 0;
            NodeTree <T> root = new NodeTree <T>(this.n);

            this.root = root;
            CLR(this.root, 0, depth);
        }
Ejemplo n.º 2
0
 private void ChangeDepth(NodeTree <T> elem, int depth)
 {
     if (elem != null)
     {
         elem.Depth = depth;
         ChangeDepth(elem.Left, depth + 1);
         ChangeDepth(elem.Right, depth + 1);
     }
 }
Ejemplo n.º 3
0
        private void CLR(NodeTree <T> node, int depth, int ldepth)
        {
            node.Depth = depth;
            if (depth != ldepth)
            {
                this.n++;
                NodeTree <T> left_node = new NodeTree <T>(this.n);
                node.Left = left_node;
                CLR(left_node, depth + 1, ldepth);

                this.n++;
                NodeTree <T> right_node = new NodeTree <T>(this.n);
                node.Right = right_node;
                CLR(right_node, depth + 1, ldepth);
            }
            else
            {
                node.Left  = null;
                node.Right = null;
            }
        }
Ejemplo n.º 4
0
        public void Remove(NodeTree <T> elem)
        {
            NodeTree <T> parent = SearchParent(elem.Id);

            if (elem.Right != null)
            {
                if (elem.Left != null)
                {
                    NodeTree <T> buf = elem.Left;
                    while (buf.Right != null)
                    {
                        buf = buf.Right;
                    }

                    buf.Right = elem.Right;
                }
                else
                {
                    elem.Left = elem.Right;
                }
            }

            if (parent != null)
            {
                if (parent.Left.Id == elem.Id)
                {
                    parent.Left = elem.Left;
                }
                else
                {
                    parent.Right = elem.Left;
                }
            }
            else
            {
                root = root.Left;
            }
            ChangeDepth(root, 0);
        }
Ejemplo n.º 5
0
        private NodeTree <T> SearchParent(int id)
        {
            NodeTree <T> child  = this.root;
            NodeTree <T> parent = null;

            while (true)
            {
                if (child.Id == id)
                {
                    return(parent);
                }
                else if (child.Right.Id > id)
                {
                    parent = child;
                    child  = child.Left;
                }
                else
                {
                    parent = child;
                    child  = child.Right;
                }
            }
        }
Ejemplo n.º 6
0
        public NodeTree <T> Search(int id)
        {
            NodeTree <T> result = root;

            while (true)
            {
                if (result.Id == id)
                {
                    return(result);
                }
                else
                {
                    if (result.Right.Id > id)
                    {
                        result = result.Left;
                    }
                    else
                    {
                        result = result.Right;
                    }
                }
            }
        }