Connecting Tech Pros Worldwide Help | Site Map

C B-Tree problem need it important

Newbie
 
Join Date: Mar 2007
Posts: 2
#1: Mar 23 '07
I have a problem
I want to print a B tree by this way
the In put will be: 8 5 3 7 4 2

the out put will be:

8
/ \
3 5
/ \ \
2 4 7

I create the tree bu I can print it I just print it by
level wise
8 3 5 2 4 7
how I can print it ?
step by step.
DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#2: Mar 23 '07

re: C B-Tree problem need it important


first work out how wide your screen is (usually about 80 characters)
at each level n, the elements will be 80/(2^n) characters apart (although you may need to take into account the space the characters themselves take up.

If you want a simple check that the binary tree has been created properly (and it appears it probably has) you can simply to a preorder traversal....

Expand|Select|Wrap|Line Numbers
  1.  
  2. printTree(Node n)
  3. {
  4.   printTree(n.Left);
  5.   printf("%d ", n.value);
  6.   printTree(n.Right);
  7. }
  8.  
and the values should all be in order. (This won't tell you for certain that the tree is correct, but ordering things is the most common use for a binary tree, and if you can show that it works for many different cases then it's probably right). If you want to make sure you have a Balanced Binary Tree, you can either print visually as you intended, or have each node print it's value and Depth. Then you make sure you have enough of any particular depth.
Reply