public void Print(TreeDSNode root)
        {
            TreeDSNode currentNode = null;
            var queue = new Queue<TreeDSNode>();
            queue.Enqueue(root);
            var stack = new Stack<TreeDSNode>();
            while (queue.Count > 0)
            {
                currentNode = queue.Dequeue();
                stack.Push(currentNode);
                if (currentNode.isLeftNodeAvailable())
                {
                    queue.Enqueue(currentNode.Left);
                }
                if (currentNode.isRightNodeAvailable())
                {
                    queue.Enqueue(currentNode.Right);
                }
            }

            while (stack.Count > 0)
            {
                var nodeToPrint = stack.Pop();
                PrintNode(nodeToPrint);
            }
        }
Ejemplo n.º 2
0
        public int HeightOfTree(TreeDSNode root)
        {
            int leftHeight = 0, rightHeight = 0;

            if (root == null)
            {
                return(0);
            }
            if (root.isLeftNodeAvailable())
            {
                leftHeight = HeightOfTree(root.Left);
            }
            if (root.isRightNodeAvailable())
            {
                rightHeight = HeightOfTree(root.Right);
            }
            return(leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1);
        }
Ejemplo n.º 3
0
        public TreeDSNode Find(TreeDSNode root)
        {
            TreeDSNode currentNode = null;
            var        queue       = new Queue <TreeDSNode>();

            queue.Enqueue(root);
            while (queue.Count > 0)
            {
                currentNode = queue.Dequeue();
                if (currentNode.isLeftNodeAvailable())
                {
                    queue.Enqueue(currentNode.Left);
                }
                if (currentNode.isRightNodeAvailable())
                {
                    queue.Enqueue(currentNode.Right);
                }
            }
            return(currentNode);
        }
 public void Print(TreeDSNode root, List <int> path)
 {
     if (root == null)
     {
         return;
     }
     path.Add(root.Value);
     if (root.isLeftNodeNull() && root.isRightNodeNull())
     {
         String pathStr = string.Join(",", path);
         console.WriteLine("Tree path: " + pathStr);
     }
     else
     {
         if (root.isLeftNodeAvailable())
         {
             Print(root.Left, new List <int>(path));
         }
         if (root.isRightNodeAvailable())
         {
             Print(root.Right, new List <int>(path));
         }
     }
 }
Ejemplo n.º 5
0
 private bool isHalfNode(TreeDSNode currentNode)
 {
     return((currentNode.isLeftNodeAvailable() && currentNode.isRightNodeNull()) || (currentNode.isRightNodeAvailable() && currentNode.isLeftNodeNull()));
 }
Ejemplo n.º 6
0
 private bool isFullNode(TreeDSNode currentNode)
 {
     return(currentNode.isLeftNodeAvailable() && currentNode.isRightNodeAvailable());
 }