Example #1
0
        public void Visualize()
        {
            if (this == null)
            {
                return;
            }
            int             height      = Height(this);
            PerfectTree     perfecttree = new PerfectTree(height);
            PerfectTreeNode proot       = perfecttree.root;

            PrintComplete(this, proot);
        }
Example #2
0
        public void PrintComplete(Treenode broot, PerfectTreeNode proot) //Print BinaryTree elements with corresponding spots from complete tree
        {
            if (broot == null)
            {
                return;
            }

            //proot.value = broot.data.ToString();

            Console.SetCursorPosition(proot.col, proot.row);
            Console.Write(broot.data);

            PrintComplete(broot.left, proot.right);
            PrintComplete(broot.right, proot.right);
        }
Example #3
0
            PerfectTreeNode BuildPerfectTree(int h)
            {
                if (h == 0)
                {
                    return(null);
                }
                int                     increasecol = Convert.ToInt32(Math.Pow(2, h));
                PerfectTreeNode         root        = new PerfectTreeNode();
                Queue <PerfectTreeNode> q           = new Queue <PerfectTreeNode>();

                q.Enqueue(root);
                int level = 1;

                while (level <= h)
                {
                    int startcol = Convert.ToInt32(Math.Pow(2, (h - level)) - 1); //2^(h-level)-1;
                    int r        = (level - 1) * 2;
                    int count    = q.Count;
                    while (count > 0)
                    {
                        PerfectTreeNode curr = q.Dequeue();
                        curr.row = r;
                        curr.col = startcol;
                        PerfectTreeNode left  = new PerfectTreeNode();
                        PerfectTreeNode right = new PerfectTreeNode();
                        curr.left  = left;
                        curr.right = right;
                        q.Enqueue(left);
                        q.Enqueue(right);
                        count--;
                        startcol    += increasecol;
                        increasecol /= 2;
                    }
                    level++;
                }
                return(root);
            }
Example #4
0
 public PerfectTree(int h)
 {
     height = h;
     root   = BuildPerfectTree(h); //build an empty perfect tree
 }