Exemple #1
0
 static BST Search(int x)
 {
     if (T != null)
     {
         BST i = T;
         while (i != null)
         {
             if (i.Info > x)
             {
                 i = i.Lptr;
             }
             else if (i.Info < x)
             {
                 i = i.Rptr;
             }
             else
             {
                 return(i);
             }
         }
     }
     return(null);
 }
Exemple #2
0
        static void Main(string[] args)
        {
            BST tree = new BST(new Node(10));

            tree.DeleteKey(10);
//            tree.Minimum();
            tree.Insert(30);
            Console.WriteLine(tree.Minimum().Key);

//            tree.Insert(20);
//            tree.Insert(30);
//            tree.Insert(5);
//            tree.Insert(7);
//            tree.InOrderTreeWalk();
//            Console.WriteLine(tree.Minimum().Key);

//            Node nodeToSearch = tree.Search(20);
//            if (nodeToSearch == null)
//            {
//                Console.WriteLine("Didn't find the element");
//            }
//            else
//            {
//                Console.WriteLine("Find the node");
//            }
//            tree.DeleteKey(30);
//            Node nodeToSearchAgain = tree.Search(20);
//            if (nodeToSearchAgain == null)
//            {
//                Console.WriteLine("Didn't find the element");
//            }
//            else
//            {
//                Console.WriteLine("Find the node");
//            }
        }
        static void Main(string[] args)
        {
            BST <int> bst    = new BST <int>();
            Random    random = new Random();

            for (int i = 0; i < 10; i++)
            {
                //int val = random.Next(0, 100);
                if (!bst.Search(i))
                {
                    bst.Add(i);
                }
            }

            bst.InorderTraversal();

            bst.Remove(1);
            bst.Remove(2);
            bst.Remove(3);
            bst.Remove(4);
            bst.Remove(5);
            Console.WriteLine();
            bst.InorderTraversal();
        }
Exemple #4
0
 /// <summary>
 /// constructor and pass parameter
 /// </summary>
 /// <param name="nodeData"></param>
 public BST(T nodeData)
 {
     NodeData  = nodeData;
     LeftTree  = null;
     RightTree = null;
 }
Exemple #5
0
        static void Main(string[] args)
        {
            BST B = new BST();

            int choice;

            while (true)
            {
                Console.WriteLine("Enter your choice: \n1. Inorder Tree Walk\n2. Tree Search\n3. Iterative Tree Search\n4. Tree Maximum\n5. Tree Minimum\n6. Tree Successor\n7. Tree Insert\n8. Transplant\n9. Tree Delete\n10. EXIT");
                choice = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("\n");

                if (choice == 1)
                {
                    B.InorderTreeWalk(B.Root);
                }
                else if (choice == 2)
                {
                    Console.WriteLine("Enter the key you want to search: ");
                    int  key   = Convert.ToInt32(Console.ReadLine());
                    Node match = B.TreeSearch(key, B.Root);

                    if (match == null)
                    {
                        Console.WriteLine("No such key exist.");
                    }
                    else
                    {
                        Console.WriteLine("key: " + match.key);
                        if (match.parent != null)
                        {
                            Console.WriteLine("parent key: " + match.parent.key);
                        }
                        if (match.left != null)
                        {
                            Console.WriteLine("Left child key: " + match.left.key);
                        }
                        if (match.right != null)
                        {
                            Console.WriteLine("right child key: " + match.right.key);
                        }
                    }
                }
                else if (choice == 3)
                {
                    Console.WriteLine("Enter the key you want to search iteratively: ");
                    int  key   = Convert.ToInt32(Console.ReadLine());
                    Node match = B.IterativeTreeSearch(key, B.Root);

                    if (match == null)
                    {
                        Console.WriteLine("No such key exist.");
                    }
                    else
                    {
                        Console.WriteLine("key: " + match.key);
                        if (match.parent != null)
                        {
                            Console.WriteLine("parent key: " + match.parent.key);
                        }
                        if (match.left != null)
                        {
                            Console.WriteLine("Left child key: " + match.left.key);
                        }
                        if (match.right != null)
                        {
                            Console.WriteLine("right child key: " + match.right.key);
                        }
                    }
                }
                else if (choice == 4)
                {
                    Node max = B.TreeMaximum(B.Root);
                    if (max != null)
                    {
                        Console.WriteLine("MAXIMUM: " + max.key);
                    }
                    else
                    {
                        Console.WriteLine("Tree is empty.");
                    }
                }
                else if (choice == 5)
                {
                    Node min = B.TreeMinimum(B.Root);
                    if (min != null)
                    {
                        Console.WriteLine("MINIMUM: " + min.key);
                    }
                    else
                    {
                        Console.WriteLine("Tree is empty.");
                    }
                }
                else if (choice == 6)
                {
                    Console.WriteLine("Enter the key you want to find successor of: ");
                    int  key       = Convert.ToInt32(Console.ReadLine());
                    Node x         = B.TreeSearch(key, B.Root);
                    Node successor = B.TreeSuccessor(x);
                    if (successor != null)
                    {
                        Console.WriteLine("The successor of key " + key + " is " + successor.key);
                    }
                    else
                    {
                        Console.WriteLine("successor don't exist.");
                    }
                }
                else if (choice == 7)
                {
                    Console.WriteLine("Enter the key you want to insert: ");
                    int key = Convert.ToInt32(Console.ReadLine());
                    B.TreeInsert(key);
                }
                else if (choice == 8)
                {
                    //B.Transplant();
                }
                else if (choice == 9)
                {
                    Console.WriteLine("Enter the key you want to delete: ");
                    int  key = Convert.ToInt32(Console.ReadLine());
                    Node z   = B.TreeSearch(key, B.Root);

                    if (z != null)
                    {
                        B.TreeDelete(z);
                    }
                    else
                    {
                        Console.WriteLine("No such key exist.");
                    }
                }
                else
                {
                    break;
                }
            }
        }
Exemple #6
0
 public BST(int x)
 {
     this.Info = x;
     this.Lptr = this.Rptr = null;
 }