示例#1
0
 public void add_node(T d)
 { //adding nodes to the binary search tree
     if (this.left == null && IsBiggerThan(this.data, d))
     {
         this.left        = new BT <T>(d);
         this.left.level  = this.level + 1;
         this.left.parent = this;
     }
     else if (this.right == null && IsLessThan(this.data, d))
     {
         this.right        = new BT <T>(d);
         this.right.level  = this.level + 1;
         this.right.parent = this;
     }
     else
     {
         if (IsBiggerThan(this.data, d))
         {
             this.left.add_node(d);
         }
         else if (IsLessThan(this.data, d))
         {
             this.right.add_node(d);
         }
     }
 }
示例#2
0
 public BT()
 {
     this.level = 0;
     left       = null;
     right      = null;
     parent     = null;
 }
示例#3
0
        static void Main(string[] args)
        {
            Random   rnd  = new Random();
            BT <int> root = new BT <int>(15);

            for (int i = 1; i <= 150; ++i)
            {
                root.add_node(rnd.Next(1, 31));
            }
            Console.WriteLine("Print full tree: ");
            root.full_print(); //full tree
            root.delete_node(20);
            Console.WriteLine();
            Console.WriteLine("Print full tree after deleting one element: ");
            root.full_print(); //tree after deleting one node
            Console.WriteLine();
            Console.Write("Traverse: ");
            root.traverse();
            Console.WriteLine();
            BT <int> node = root.search(15);

            Console.WriteLine("Parent: " + node.get_parent().ToString());
            Console.WriteLine("Left Child: " + node.get_left().ToString());
            Console.WriteLine("Right Child: " + node.get_right().ToString());
            Console.Write("Print leaves: ");
            root.leaves_print();
            Console.Read();
        }
示例#4
0
 public BT(T d)
 {
     this.data  = d;
     this.level = 0;
     left       = null;
     right      = null;
     parent     = null;
 }
示例#5
0
 public T get_right()
 {
     if (this.right != null)
     {
         return(this.right.data);
     }
     else
     {
         BT <T> temp = new BT <T>();
         return(temp.data);
     }
 }
示例#6
0
 public T get_left()
 {
     if (this.left != null)
     {
         return(this.left.data);
     }
     else
     {
         BT <T> temp = new BT <T>();
         return(temp.data);
     }
 }
示例#7
0
 public T get_parent()
 {
     if (this.parent != null)
     {
         return(this.parent.data);
     }
     else
     {
         BT <T> temp = new BT <T>();
         return(temp.data);
     }
 }
示例#8
0
 public BT <T> search(T n)
 {
     if (IsEqualTo(this.data, n))
     {
         return(this);
     }
     else if (IsBiggerThan(this.data, n) && this.left != null)
     {
         return(this.left.search(n));
     }
     else if (IsLessThan(this.data, n) && this.right != null)
     {
         return(this.right.search(n));
     }
     else
     {
         BT <T> temp = new BT <T>();
         Console.WriteLine("This node does not exist");
         return(temp);
     }
 }
示例#9
0
        public void delete_node(T d)
        {
            BT <T> n    = this.search(d);
            BT <T> temp = new BT <T>();

            if (!IsEqualTo(n.data, temp.data))
            {
                BT <T> p = n.parent;
                if (n.left == null && n.right == null)//deleting the leaf
                {
                    if (p.left == n)
                    {
                        p.left = null;
                    }
                    else if (p.right == n)
                    {
                        p.right = null;
                    }
                }
                else if (n.left == null || n.right == null)//when the element we want to delete has one child
                {
                    if (n.left == null)
                    {
                        if (p.left == n)
                        {
                            p.left = n.right;
                        }
                        else
                        {
                            p.right = n.right;
                        }
                    }
                    else
                    if (p.left == n)
                    {
                        p.left = n.right;
                    }
                    else
                    {
                        p.right = n.right;
                    }
                }
                else //when the element we want to delete has two children
                {
                    BT <T> s = n.right;
                    n.data = s.data;
                    if (s.parent.left == s)
                    {
                        s.parent.left = s.right;
                        if (s.right != null)
                        {
                            s.right.parent = s.parent;
                        }
                    }
                    else
                    {
                        s.parent.right = s.left;
                        if (s.left != null)
                        {
                            s.right.parent = s.parent;
                        }
                    }
                }
            }
        }