My compltete Binary tree

Hello
My program's supposed to produce a compltete Binary tree whose preorder traversal is SBDHEJKTFG;But it doesn't work.Why?

Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;

       
 
   struct Node{
          char data;
          Node* LChild;
          Node* RChild;
       }root, *temp;
       
void preTraverse(Node *N)
{
    cout << N->data;
    preTraverse(N->LChild);
    preTraverse(N->RChild);
}
             
int main()
{

   
   root.data = 'S';
   
   temp=root.LChild = new Node;
   
   temp->data = 'B';
   
   temp->RChild = new Node;
   temp->RChild->data = 'E';
      temp->data = 'J';
   temp->RChild->RChild = new Node;
   temp->RChild->RChild->data = 'G';
   temp->LChild->LChild = new Node;
   temp->LChild->LChild->data = 'K';
   temp = temp->LChild = new Node;
   
   
   temp->data = 'D';
   
   temp->RChild = new Node;
   temp->data = 'X';
   temp = temp->LChild = new Node;
   
   temp->data = 'H';
   
   temp = &root;
   
   temp =  temp->RChild = new Node;
   
   temp->data = 'J';
   temp->RChild = new Node;
   temp->RChild->data = 'F';
   temp->LChild = new Node;
   temp->LChild->data = 'K';
   
   preTraverse(&root);
   
   cin.get();
   
   return 0;
}

Why doesn't your tree code have an insert function? It's almost impossible to trace what you've done.
I didn't know how to wite it.

But the problem is with preTraverse().I don't know what is proformed when preTraverse() is run with a null parameter.

The question was about a complete binary tree whose preorder traversal is
'SBDHXEJKTFG'

I wanted to test my solution
Last edited on
preTraverse() is fine. But it's impossible to verify that tree.

I've spent some time explaining tree matters in the recent past and gave an example. Don't just copy the code, try and understand how it works.
http://www.cplusplus.com/forum/beginner/74949/#msg402173
Would you please draw the proposed tree?
This prints some part of the tree:

temp = &root;
cout << (temp)->data;
cout << temp->LChild->data;
cout << temp->RChild->data;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   temp  = temp->LChild;
   
   cout << temp->LChild->data;
   cout << temp->RChild->data;
   
   temp  = temp->LChild;
   
   cout << temp->LChild->data;
   cout << temp->RChild->data;
   
   temp  = temp->LChild;
   
   cout << temp->LChild <<endl;
   cout << temp->RChild;


as is evident from the above code, the last LChild is uninitialized, while its sibling is 0.Why is it so?
I correct my problem.Now my question is that the inorder traversal of the right subtree of root is the same as its preorder traversal.Why is that?

And the subtree is not a riht linear subtree.See the snippet:
1
2
3
4
5
6
7
8
9
   temp = &root;
   
   temp =  temp->RChild = new Node;
   
   temp->data = 'T';
   temp->RChild = new Node;
   temp->RChild->data = 'G';
   temp->LChild = new Node;
   temp->LChild->data = 'F';
Last edited on
You're right, pre, in and post order traversals do produce different sequences.
corrected the problem.Thank you
Topic archived. No new replies allowed.