Exemplo n.º 1
0
 private void preorder_Recursive(binaryTreeNode head)
 {
     if (head != null)
     {
         Console.Write("{0} ", head.n);
         preorder_Recursive(head.left);
         preorder_Recursive(head.right);
     }
 }
Exemplo n.º 2
0
 public void postOrder_Recursive(binaryTreeNode head)
 {
     if (head == null)
     {
         return;
     }
     postOrder_Recursive(head.left);
     postOrder_Recursive(head.right);
     Console.Write("{0} ", head.n);
 }
Exemplo n.º 3
0
 private int leafNodes(binaryTreeNode head)
 {
     if (head == null)
     {
         return(0);
     }
     if (head.left == null && head.right == null)
     {
         return(1);
     }
     return(leafNodes(head.right) + leafNodes(head.left));
 }
Exemplo n.º 4
0
 private int halfNodes(binaryTreeNode head)
 {
     if (head == null)
     {
         return(0);
     }
     if ((head.left != null && head.right == null) || (head.left == null && head.right != null))
     {
         return(halfNodes(head.left) + halfNodes(head.right) + 1);
     }
     return(halfNodes(head.left) + halfNodes(head.right));
 }
Exemplo n.º 5
0
 private bool search(binaryTreeNode head, int n)
 {
     if (head == null)
     {
         return(false);
     }
     if (head.n == n)
     {
         return(false);
     }
     return(search(head.left, n) || search(head.right, n));
 }
Exemplo n.º 6
0
 private int fullNodes(binaryTreeNode head)
 {
     if (head == null)
     {
         return(0);
     }
     if (head.left != null && head.right != null)
     {
         return(fullNodes(head.left) + fullNodes(head.right) + 1);
     }
     else
     {
         return(fullNodes(head.left) + fullNodes(head.right));
     }
 }
Exemplo n.º 7
0
        private static binaryTreeNode insert(binaryTreeNode head, int val)
        {
            if (head == null)
            {
                return(new binaryTreeNode(val));
            }

            if (val < head.n)
            {
                head.left = insert(head.left, val);
            }
            else
            {
                head.right = insert(head.right, val);
            }
            return(head);
        }
Exemplo n.º 8
0
        private static binaryTreeNode insertInOrder(binaryTreeNode head, int val)
        {
            if (head == null)
            {
                return(new binaryTreeNode(val));
            }

            Queue <binaryTreeNode> q = new Queue <binaryTreeNode>();

            q.Enqueue(head);



            binaryTreeNode temp = null;

            while (q.Count > 0)
            {
                temp = q.Dequeue();

                if (temp != null)
                {
                    if (temp.left != null)
                    {
                        q.Enqueue(temp.left);
                    }
                    else
                    {
                        temp.left = new binaryTreeNode(val);
                        return(head);
                    }

                    if (temp.right != null)
                    {
                        q.Enqueue(temp.right);
                    }
                    else
                    {
                        temp.right = new binaryTreeNode(val);
                        return(head);
                    }
                }
            }

            return(head);
        }
Exemplo n.º 9
0
        private binaryTreeNode deepestNode(binaryTreeNode head)
        {
            if (head == null)
            {
                return(null);
            }
            Queue <binaryTreeNode> q = new Queue <binaryTreeNode>();

            q.Enqueue(head);
            q.Enqueue(null);
            binaryTreeNode prevNode = null;
            binaryTreeNode temp     = null;

            while (q.Count > 0)
            {
                prevNode = temp;
                temp     = q.Dequeue();
                if (temp != null)
                {
                    if (temp.left != null)
                    {
                        q.Enqueue(temp.left);
                    }
                    if (temp.right != null)
                    {
                        q.Enqueue(temp.right);
                    }
                }
                else
                {
                    if (q.Count == 0)
                    {
                        return(prevNode);
                    }
                    else
                    {
                        q.Enqueue(null);
                    }
                }
            }

            return(prevNode);
        }
Exemplo n.º 10
0
        // level order traversal
        private void printTree(binaryTreeNode head)
        {
            if (head == null)
            {
                return;
            }
            Queue <binaryTreeNode> q = new Queue <binaryTreeNode>();

            q.Enqueue(head);
            q.Enqueue(null);

            while (q.Count > 0)
            {
                binaryTreeNode temp = q.Dequeue();
                if (temp != null)
                {
                    Console.Write("{0} ", temp.n);
                    if (temp.left != null)
                    {
                        q.Enqueue(temp.left);
                    }
                    if (temp.right != null)
                    {
                        q.Enqueue(temp.right);
                    }
                }
                else
                {
                    if (q.Count > 0)
                    {
                        Console.WriteLine();
                        q.Enqueue(null);
                    }
                }
            }
        }
Exemplo n.º 11
0
 public void Insert(int n)
 {
     head = insertInOrder(head, n);
 }
Exemplo n.º 12
0
 public binaryTree()
 {
     head = null;
 }
Exemplo n.º 13
0
        // deepest node in binary tree
        public void deepestNode()
        {
            binaryTreeNode node = deepestNode(head);

            Console.WriteLine("Deepest Node is {0} ", node.n);
        }
Exemplo n.º 14
0
 public binaryTreeNode(int val)
 {
     left   = null;
     right  = null;
     this.n = val;
 }