Exemplo n.º 1
0
        public static NodeT TMenu(NodeT tree)
        {
            while (true)
            {
                int num2;
                Console.Clear();
                Console.WriteLine("              --** Tree **--");
                Console.WriteLine(" ------------------------------------------------");
                Console.WriteLine("  Choose an option: ");
                Console.WriteLine("1) Insert new node . " + " ");
                Console.WriteLine("2) Traversal==>Postorder Inorder Preorder.");
                Console.WriteLine("3) Delete.");
                Console.Write("\r\n Select here : ");
                num2 = Convert.ToInt32(Console.ReadLine());
                switch (num2)
                {
                case 1:
                    int      v;
                    NodeTree root = new NodeTree();
                    Console.Write("how many do you want to insert?  ");
                    int a = Convert.ToInt32(Console.ReadLine());
                    for (int i = 0; i < a; i++)
                    {
                        v = Convert.ToInt32(Console.ReadLine());

                        tree.InsertToTree(root, v);
                        tree.PrintInorder(root);
                    }
                    break;
                }
                return(tree);
            }
        }
Exemplo n.º 2
0
        //true?
        public NodeTree()
        {
            int item;

            item = key;
            left = right = null;
        }
Exemplo n.º 3
0
 NodeTree DeleteRec(NodeTree root, int key)
 {
     if (root == null)
     {
         return(root);
     }
     if (key < root.key)
     {
         root.left = DeleteRec(root.left, key);
     }
     else if (key > root.key)
     {
         root.right = DeleteRec(root.right, key);
     }
     else
     {
         if (root.left == null)
         {
             return(root.right);
         }
         else if (root.right == null)
         {
             return(root.left);
         }
         root.key   = MinValue(root.right);
         root.right = DeleteRec(root.right, root.key);
     }
     return(root);
 }
Exemplo n.º 4
0
 public void PrintPreorder(NodeTree node)
 {
     if (node == null)
     {
         return;
     }
     //(in driver) Console.Write(node.Data + " ");
     PrintPreorder(node.left);
     PrintPreorder(node.right);
 }
Exemplo n.º 5
0
        int MinValue(NodeTree root)
        {
            int minv = root.key;

            while (root.left != null)
            {
                minv = root.left.key;
                root = root.left;
            }
            return(minv);
        }
Exemplo n.º 6
0
        public static bool Search(NodeTree node, int a)
        {
            if (node == null)
            {
                return(false);
            }
            if (node.Data == a)
            {
                return(true);
            }
            bool result1 = Search(node.right, a);
            bool result2 = Search(node.left, a);

            return(result1 || result2);
        }
Exemplo n.º 7
0
 public NodeTree InsertToTree(NodeTree root, int value)
 {
     if (root == null)
     {
         root      = new NodeTree();
         root.Data = value;
     }
     else if (value < root.Data)
     {
         root.left = InsertToTree(root.left, value);
     }
     else
     {
         root.right = InsertToTree(root.right, value);
     }
     return(root);
 }
Exemplo n.º 8
0
 //Delete with a specific key.
 public void DeleteKey(int key)
 {
     Root = DeleteRec(Root, key);
 }