Don't know where to begon

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Don't know where to begon

 
0
  #1
May 12th, 2006
OK.......right now i'm learning about binary tree traversal and this problem looks like a total killer to me. Here's it is:

Write a program that takes as input the preorder and inorder traversals of a binary tree, and produces as output the level-order traversal of the tree.

All i gotta say to that is :eek:

I've built a skeleton of the program and so far I've been able to take the preorder traversal of a tree as input and generate the original tree. As for inorder, I really have no clue what to do

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #define SIZE 50
  6.  
  7. typedef struct node *link;
  8.  
  9. struct node {
  10. int item;
  11. struct node *link[2];
  12. };
  13.  
  14. link make_node(int data)
  15. {
  16. link x = (link)malloc(sizeof *x);
  17. x->item = data;
  18. x->link[0] = NULL; x->link[1] = NULL;
  19. return x;
  20. }
  21.  
  22. link make_tree(link root, int data)
  23. {
  24. if (root == NULL)
  25. root = make_node(data);
  26. if (root->item == data)
  27. return root;
  28. else
  29. {
  30. int dir = root->item < data;
  31. root->link[dir] = make_tree(root->link[dir], data);
  32. }
  33. return root;
  34. }
  35.  
  36. void disp_tree(link tree, int level)
  37. {
  38. int i;
  39.  
  40. if ( tree == NULL ) {
  41. for ( i = 0; i < level; i++ )
  42. printf ( "\t" );
  43. printf ( "~\n" );
  44.  
  45. return;
  46. }
  47.  
  48. disp_tree ( tree->link[1], level + 1 );
  49.  
  50. for ( i = 0; i < level; i++ )
  51. printf ( "\t" );
  52. printf ( "%d\n", tree->item );
  53.  
  54. disp_tree ( tree->link[0], level + 1 );
  55. }
  56.  
  57. /*
  58. static int N, head, tail;
  59. struct node *q[50];
  60.  
  61. void put(link item)
  62. {
  63. q[tail++] = item;
  64. tail = tail % N;
  65. }
  66.  
  67. link get()
  68. {
  69. head = head % N;
  70. return q[head++];
  71. }
  72. */
  73. // preorder
  74. void preorder(link root, int a[])
  75. {
  76. while (*a != 0)
  77. {
  78. make_tree(root, *a);
  79. a++;
  80. }
  81.  
  82. disp_tree(root, 1);
  83. }
  84. // inorder
  85. void inorder()
  86. {}
  87.  
  88. int main(void)
  89. {
  90. int num;
  91. puts("1: Preorder Traversal");
  92. puts("2: Inorder Traversal");
  93. puts("Enter your choice:");
  94. scanf("%d", &num);
  95. fflush(stdin);
  96.  
  97. int i = 0, j = 0, val[SIZE] = {0};
  98. char trav[SIZE];
  99. puts("Enter the traversal order");
  100. fgets(trav, SIZE, stdin);
  101. while (isdigit(trav[i]))
  102. {
  103. val[j++] = 10*val[i] + (trav[i++]-'0');
  104. while (trav[i] == ' ')
  105. i++;
  106. }
  107.  
  108. if (num == 1)
  109. {
  110. link root = make_node(val[0]);
  111. preorder(root, val);
  112. }
  113. else
  114. inorder();
  115.  
  116. return 0;
  117. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Don't know where to begon

 
0
  #2
May 13th, 2006
>As for inorder, I really have no clue what to do

You mean you don't know how inorder traversal would look or you don't know how to code it?

Inorder
1. Traverse the left subtree
2. Visit the root
3. Traverse the right subtree

That's the basic idea...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Don't know where to begon

 
0
  #3
May 14th, 2006
No that's not what I meant. I'm supposed to change an inorder traversal into the original binary tree and then produce a level order traversal. Sorry for any misunderstanding.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Don't know where to begon

 
0
  #4
May 15th, 2006
Well if the user explicitly gives you the preorder and inorder traversals of a binary tree, then recovery of the original binary can be done like thus:

Clearly the first element in the preorder is the root. We can then search for this element in the inorder traversal. The elements of the tree are now uniquely partitioned into left and right subtrees by this element. (ie., the subsequence of elements preceding the root in the inorder sequence is the inorder sequence for the left sub-tree and similarly the subsequence of elements succeeding the root is the inorder sequence for the right sub-tree) We can then recursively construct the left and right sub-trees from the preorder and inorder subsequences.
And Once we have the original binary tree, then we can use the Al-Gore-it-him for level-traversal which is well documented on the web me thinks.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 31
Reputation: ricky0161 is an unknown quantity at this point 
Solved Threads: 0
ricky0161 ricky0161 is offline Offline
Light Poster

Re: Don't know where to begon

 
0
  #5
May 15th, 2006
I Agree With Iamthwee
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Don't know where to begon

 
0
  #6
May 15th, 2006
Originally Posted by iamthwee
Well if the user explicitly gives you the preorder and inorder traversals of a binary tree, then recovery of the original binary can be done like thus:



And Once we have the original binary tree, then we can use the Al-Gore-it-him for level-traversal which is well documented on the web me thinks.
Oo that seems to make a bit more sense. Where did you get that quote btw?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC