Beispiel #1
0
        public int FindMaxWithoutRecursion(BinaryTreeNode root)
        {
            int max = -1;
            QueueUsingLL <BinaryTreeNode> queueObj = new QueueUsingLL <BinaryTreeNode>();
            BinaryTreeNode temp;

            if (root != null)
            {
                queueObj.EnQueue(root);
                while (!queueObj.IsEmpty())
                {
                    temp = queueObj.DeQueue();
                    if (temp.data > max)
                    {
                        max = temp.data;
                    }
                    if (temp.left != null)
                    {
                        queueObj.EnQueue(temp.left);
                    }
                    if (temp.right != null)
                    {
                        queueObj.EnQueue(temp.right);
                    }
                }
            }
            return(max);
        }
Beispiel #2
0
        public bool SearchIfDataExists(BinaryTreeNode root, int data)
        {
            QueueUsingLL <BinaryTreeNode> queueObj = new QueueUsingLL <BinaryTreeNode>();
            BinaryTreeNode temp;

            if (root != null)
            {
                queueObj.EnQueue(root);
                while (!queueObj.IsEmpty())
                {
                    temp = queueObj.DeQueue();
                    if (temp.data == data)
                    {
                        return(true);
                    }
                    if (temp.left != null)
                    {
                        queueObj.EnQueue(temp.left);
                    }
                    if (temp.right != null)
                    {
                        queueObj.EnQueue(temp.right);
                    }
                }
            }
            return(false);
        }
Beispiel #3
0
        public void InsertData(int data)
        {
            QueueUsingLL <BinaryTreeNode> queueObj = new QueueUsingLL <BinaryTreeNode>();

            if (root != null)
            {
                queueObj.EnQueue(root);
                while (!queueObj.IsEmpty())
                {
                    var temp = queueObj.DeQueue();
                    if (data <= temp.data)
                    {
                        if (temp.left == null)
                        {
                            temp.left = new BinaryTreeNode(data);
                            break;
                        }
                        else
                        {
                            queueObj.EnQueue(temp.left);
                        }
                    }

                    if (data > temp.data)
                    {
                        if (temp.right == null)
                        {
                            temp.right = new BinaryTreeNode(data);
                            break;
                        }
                        else
                        {
                            queueObj.EnQueue(temp.right);
                        }
                    }
                }
            }
            else
            {
                root = new BinaryTreeNode(data);
            }
        }
Beispiel #4
0
        public void LevelOrderTraversal(BinaryTreeNode root)
        {
            QueueUsingLL <BinaryTreeNode> queueObj = new QueueUsingLL <BinaryTreeNode>();
            BinaryTreeNode temp;

            if (root == null)
            {
                return;
            }
            queueObj.EnQueue(root);
            while (!queueObj.IsEmpty())
            {
                temp = queueObj.DeQueue();
                resultListLevelOrderNonRecursive.InsertAtEnd(temp.data);
                if (temp.left != null)
                {
                    queueObj.EnQueue(temp.left);
                }
                if (temp.right != null)
                {
                    queueObj.EnQueue(temp.right);
                }
            }
        }