public void insert(nod node, int value, string name, string dob, string adress)
 {
     if (root == null)
     {
         root = new nod(value, name, dob, adress);
     }
     else
     {
         if (value <= node.key)
         {
             if (node.left != null)
             {
                 insert(node.left, value, name, dob, adress);
             }
             else
             {
                 node.left = new nod(value, name, dob, adress);
             }
         }
         else if (value > node.key)
         {
             if (node.right != null)
             {
                 insert(node.left, value, name, dob, adress);
             }
             else
             {
                 node.right = new nod(value, name, dob, adress);
             }
         }
     }
 }
 public void in_order_traverse(nod rut)
 {
     if (rut != null)
     {
         in_order_traverse(rut.left);
         Console.WriteLine("\t" + rut.key + "\t\t" + rut.name + "\t" + rut.dob + "\t" + rut.adress);
         in_order_traverse(rut.right);
     }
 }
 public nod search_recursively(int _key, nod rev)
 {
     if (rev == null)
     {
         return(null);
     }
     if (_key == rev.key)
     {
         return(rev);
     }
     if (rev.key > _key)
     {
         return(search_recursively(_key, rev.left));
     }
     else
     {
         return(search_recursively(_key, rev.right));
     }
 }
예제 #4
0
        static void Main(string[] args)
        {
            binary_tree bst = new binary_tree();

            int op = 0;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("     \t<------------------Binary Search Tree---------------->");

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("\t\t\t\t\t\t\t\tBig Oh O(n)\n\t\t\t\t\t\t\t\tTheta log(n)\n\t\t\t\t\t\t\t\tOmega 0(1)");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.ResetColor();
            do
            {
                Console.WriteLine("1)press 1 to insert element in the tree");
                Console.WriteLine("2)press 2 to search element in the tree");
                Console.WriteLine("3)press 3 for deletion");
                Console.WriteLine("4)press 4 for In Order traversing");
                Console.WriteLine("5)press 5 for Pre Order traversing");
                Console.WriteLine("6)press 6 for Post Order traversing");
                Console.WriteLine("7)press 7 to create NIC");
                Console.WriteLine("8)press 8 to compare searching efficiencies");
                Console.WriteLine();
                try
                {
                    op = int.Parse(Console.ReadLine());
                    if (op == 1)
                    {
                        Console.Clear();
                        Console.WriteLine("enter element to insert");
                        int el = int.Parse(Console.ReadLine());
                        bst.insert_(el);
                        Console.ReadKey();
                        Console.Clear();
                    }
                    if (op == 2)
                    {
                        Console.Clear();
                        Console.WriteLine("enter element to search");
                        int el = int.Parse(Console.ReadLine());
                        bst.search(el);
                        Console.ReadKey();
                        Console.Clear();
                    }
                    if (op == 3)
                    {
                        Console.Clear();
                        Console.WriteLine("enter element to delete");
                        int el = int.Parse(Console.ReadLine());
                        bst.delete(el);
                        Console.ReadKey();
                        Console.Clear();
                    }
                    if (op == 4)
                    {
                        if (bst.root != null)
                        {
                            Console.Clear();
                            Console.WriteLine("\nIN Order Traversing");
                            bst.in_order_traverse(bst.root);
                            Console.ReadKey();
                            Console.Clear();
                        }
                        else
                        {
                            Console.WriteLine("tree is empty");
                        }
                    }
                    if (op == 5)
                    {
                        if (bst.root != null)
                        {
                            Console.Clear();
                            Console.WriteLine("\nPre order Traversing");
                            bst.pre_order_traverse(bst.root);
                            Console.ReadKey();
                            Console.Clear();
                        }
                        else
                        {
                            Console.WriteLine("tree is empty");
                        }
                    }

                    else if (op == 6)
                    {
                        if (bst.root != null)
                        {
                            Console.Clear();
                            Console.WriteLine("\nPost order Traversing");
                            bst.post_order_traverse(bst.root);
                            Console.ReadKey();
                            Console.Clear();
                        }
                        else
                        {
                            Console.WriteLine("tree is empty");
                        }
                    }
                    else if (op == 8)
                    {
                        Console.Clear();
                        Console.WriteLine("eneter # of random elements to insert");
                        int    rno = int.Parse(Console.ReadLine());
                        Random r   = new Random();
                        int[]  arr = new int[rno];
                        for (int i = 0; i < rno; i++)
                        {
                            int x = r.Next(0, 1000);
                            arr[i] = x;
                            bst.insert_(x);
                        }
                        int count = 0;
                        for (int i = 0; i < arr.Length; i++)
                        {
                            count++;
                            if (arr[i] == 93)
                            {
                                break;
                            }
                        }

                        Console.WriteLine("\n\tWe inserted " + rno + " random elements in an array and the binary search tree");
                        Console.WriteLine("\nno of visited elements in linear searching " + count);
                        Console.WriteLine("\nNow searching  the same number in the binary search tree\n");
                        bst.search(93);
                        Console.ReadKey();
                        Console.Clear();
                    }
                    else if (op == 7)
                    {
                        Console.Clear();
                        nic nic = new nic();
                        int op1 = 0;
                        do
                        {
                            Console.WriteLine("press 1 to create");
                            Console.WriteLine("press 2 to search");
                            Console.WriteLine("press 3 to view");
                            Console.WriteLine("press 4 to delete");

                            op1 = int.Parse(Console.ReadLine());
                            if (op1 == 1)
                            {
                                Console.Clear();
                                Console.WriteLine();
                                Console.WriteLine("enter new nic number");
                                int nicc = int.Parse(Console.ReadLine());
                                Console.WriteLine("enter name ");
                                string name = Console.ReadLine();
                                Console.WriteLine("enter dob");
                                string dob = Console.ReadLine();
                                Console.WriteLine("enter adress");
                                string adress = Console.ReadLine();
                                nic.insert(nic.root, nicc, name, dob, adress);
                                Console.WriteLine("inserted!");
                                Console.ReadKey();
                                Console.Clear();
                            }

                            if (op1 == 2)
                            {
                                Console.WriteLine("enter nic# to search");
                                int nicc = int.Parse(Console.ReadLine());

                                nod temp = nic.search_recursively(nicc, nic.root);
                                if (temp != null)
                                {
                                    Console.WriteLine("found");
                                    Console.WriteLine("\tname:{0}\tDOB:{1}\tadress:{2}", temp.name, temp.dob, temp.adress);
                                }
                                else
                                {
                                    Console.WriteLine("not found");
                                }
                                Console.ReadKey();
                            }

                            if (op1 == 3)
                            {
                                Console.WriteLine("\n\tID card#\tname\tDOB\tadress");
                                nic.in_order_traverse(nic.root);
                                Console.ReadKey();
                            }
                        } while (op1 != 0);
                        Console.Clear();
                    }
                }
                catch (Exception)
                {
                    Console.Clear();
                    op = 7;
                }

                //if root null dont traverse
            } while (op != 0);

            Console.ReadKey();
        }