By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

C B-Tree problem need it important

P: 2
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.
Mar 23 '07 #1
Share this Question
Share on Google+
1 Reply


DeMan
100+
P: 1,806
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.
Mar 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.