示例#1
0
    void post_order_to_tree_convert(int[] postorder)
    {
        root = new binary_tree <int> .binary_node <int>(postorder[0]);

        foreach (int i in postorder)
        {
            if (i == postorder[0])
            {
                continue;
            }
            binary_search_node temp = new binary_search_node(i);
            insert_data(root, temp, null);
        }
    }
示例#2
0
    void tree(string expression)
    {
        Stack <binary_tree <string> .binary_node <string> > my_stack = new Stack <binary_tree <string> .binary_node <string> >();

        foreach (char i in expression)
        {
            string q = i.ToString();
            binary_tree <string> .binary_node <string> temp = new binary_tree <string> .binary_node <string>(q);

            if (q == "*" || q == "/" || q == "+" || q == "-" || q == "^")
            {
                temp.left  = my_stack.Pop();
                temp.right = my_stack.Pop();
            }
            my_stack.Push(temp);
        }
        binary_tree <string> bt = new binary_tree <string>(my_stack.Pop());
    }
示例#3
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();
        }
示例#4
0
    private void remove_conditions(List <binary_node <int> > temp_path, binary_node <int> main_node)
    {
        if (main_node == root)
        {
            main_node.Colors = black; return;
        }
        binary_node <int> .Colour side_color = black;
        binary_node <int>         parent     = temp_path[temp_path.Count - 2];
        binary_node <int>         grand_parent;
        binary_node <int>         side = new binary_node <int>(0);

        if (parent == root)
        {
            grand_parent = null;
        }
        else
        {
            grand_parent = temp_path[temp_path.Count - 3];
        }

        (side_color, side) = sibling_color(main_node, temp_path[temp_path.Count - 2]);
        bool first_cond = false;

        if (side == null)
        {
            first_cond = true;
        }
        else if (side_color == black)
        {
            if ((side.left == null) && (side.right == null))
            {
                first_cond = true;
            }
            else if (side.left != null)
            {
                if ((side.right == null) && (side.left.Colors == black))
                {
                    first_cond = true;
                }
            }
            if (side.right != null)
            {
                if ((side.left == null) && (side.right.Colors == black))
                {
                    first_cond = true;
                }
                else if ((side.right.Colors == black) && (side.left.Colors == black))
                {
                    first_cond = true;
                }
            }
        }
        if (first_cond)
        {
            if (parent.Colors == red)
            {
                main_node.Colors = black;
                parent.Colors    = black;
                side.Colors      = red;
                return;
            }
            else
            {
                side.Colors      = red;
                main_node.Colors = black;
                remove_conditions(path(parent), parent);
            }
        }
        else if (side_color == red)
        {
            binary_node <int> temp;
            parent.Colors = red;
            side.Colors   = black;
            if (parent.left == main_node)
            {
                temp = right_right(parent);
            }
            else
            {
                temp = left_left(parent);
            }
            if (parent == root)
            {
                temp = root;
            }
            else if (grand_parent.data > temp.data)
            {
                grand_parent.left = temp;
            }
            else
            {
                grand_parent.right = temp;
            }
            main_node.Colors = black;
            root.Colors      = black;
            remove_conditions(path(main_node), main_node);
        }
        else if (side_color == black)
        {
            side.Colors = red;
            binary_node <int> away;
            binary_node <int> close;
            binary_node <int> temp;
            if (parent.left == main_node)
            {
                close = side.left;
                away  = side.right;
            }
            else
            {
                close = side.right;
                away  = side.left;
            }
            if (away == null)
            {
                away = new binary_tree <int> .binary_node <int>(0);
            }
            if (close == null)
            {
                close = new binary_tree <int> .binary_node <int>(0);
            }
            if (away.Colors == black && close.Colors == red)
            {
                close.Colors = black;
                side.Colors  = red;
                if (parent.left == side)
                {
                    temp = right_right(side);
                }
                else
                {
                    temp = right_right(side);
                }
                if (parent == root)
                {
                    temp = root;
                }
                else if (parent.data > temp.data)
                {
                    parent.left = temp;
                }
                else
                {
                    parent.right = temp;
                }
                root.Colors = black;
                remove_conditions(path(main_node), main_node);
                return;
            }
            side.Colors   = parent.Colors;
            parent.Colors = black;
            if (parent.left == side)
            {
                temp = left_left(parent);
            }
            else
            {
                temp = right_right(parent);
            }
            if (parent == root)
            {
                root = temp;
            }
            else if (grand_parent.data > temp.data)
            {
                grand_parent.left = temp;
            }
            else
            {
                grand_parent.right = temp;
            }
            away.Colors = black;
            root.Colors = black;
        }
    }