Ejemplo n.º 1
0
        public void Inorder(Node Root)
        {
            if (Root != null)
            {
                Inorder(Root.leftc);
                Console.Write(Root.item + " ");
                Inorder(Root.rightc);

            }
        }
Ejemplo n.º 2
0
 // creat a method named Preorder and Node Root is the input
 public void Preorder(Node Root)
 {
     // if Root != null is true
     if (Root != null)
     {
         Console.Write(Root.item + " ");
         Preorder(Root.leftc);
         Preorder(Root.rightc);
     }
 }
Ejemplo n.º 3
0
 // creat a default constructor
 public Tree()
 {
     // node root refer to null
     root = null;
 }
Ejemplo n.º 4
0
        // creat a public method named Insert and
        //it has no output and has int input
        public void Insert(int id)
        {
            // creat a Node object named newNode
            Node newNode = new Node();
            // assign id to newNode.item
            newNode.item = id;
            // if root == null is true, root refer to the heap that newNode refer to
            if (root == null)
                root = newNode;
            // if root == null is false
            // current refer to the heap that root refer to and declare a node named parent
            else
            {
                Node current = root;
                Node parent;
                // while loop
                while(true)
                {
                    // root == null is false. parent refer to the heap that current refer to
                    parent = current;
                    // if id < current.item
                    if (id < current.item)
                    {
                        // current refer to the heap of current.leftc
                        current = current.leftc;
                        // if current == null is true
                        // parent.leftc refer to the heap of newNode and return so while loop stops
                        if (current == null)
                        {
                            parent.leftc = newNode;
                            return;
                        }
                    }
                    // if id > = current.item
                    else
                    {
                        // current refer to the heap of current.rightc
                        current = current.rightc;
                        // if current == null is true, parent.rightc refer to the heap of newNode
                        // return so the while loop stops
                        // if current == null is fasle
                        //go back to the beginning of the loop with current refer to the heap of current.rightc
                        if (current == null)
                        {
                            parent.rightc = newNode;
                            return;
                        }
                    }

                }
            }
        }