Exemplo n.º 1
0
 public BinarySearchTree()
 {
     root = null;
 }
        public static List <List <int> > BFSTest(BNode root)
        {
            List <List <int> > output = new List <List <int> >();

            List <int> result = new List <int>();

            if (root == null)
            {
                return(output);
            }

            BNode temp = new BNode();

            Queue <BNode> elements = new Queue <BNode>();

            elements.Enqueue(root);
            int  count            = 0;
            bool isRightSidePrint = true;

            while (elements.Count > 0)
            {
                count = elements.Count;

                while (count > 0)
                {
                    temp = elements.Dequeue();

                    result.Add(temp.value);

                    if (!isRightSidePrint)
                    {
                        if (temp.left != null)
                        {
                            elements.Enqueue(temp.left);
                        }

                        if (temp.right != null)
                        {
                            elements.Enqueue(temp.right);
                        }
                    }
                    else
                    {
                        if (temp.right != null)
                        {
                            elements.Enqueue(temp.right);
                        }
                        if (temp.left != null)
                        {
                            elements.Enqueue(temp.left);
                        }
                    }
                    count--;
                }

                isRightSidePrint = !isRightSidePrint;
                output.Add(result);
                result = new List <int>();
            }

            return(output);
        }