# In Order Traversal of Binary Tree

Page 1 of 1

## 3 Replies - 9720 Views - Last Post: 08 March 2010 - 02:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=160651&amp;s=d28f1bad7ae559096e73b7639e1dbff4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Whiskers12345

Reputation: 0
• Posts: 14
• Joined: 01-March 10

# In Order Traversal of Binary Tree

Posted 08 March 2010 - 01:13 PM

So I am working on a program that reads in a quote from a text book and uses a tree to sort the quote into a list of words. Just need some help getting the code to print the words read in in order any help would be appreciated..thank you
```
#include <string>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;

struct treetype
{
string word;
treetype *left;
treetype *right;

};
void createTree(treetype *&root)
{
root = new treetype;
root -> word = "horse";
root -> left = NULL;
root -> right = NULL;
}

bool EmptyTree(treetype *root)
{
return root == NULL;
}
void inserttree (treetype *root, string word)
{
treetype *c, *parent, *knew;
if (!EmptyTree(root))
{
c = root;
parent = NULL;
while( c!=NULL && c->word != word)
{
parent = c;
if (word < parent->word)
c = c-> left;
else
c = c-> right;
}

knew = new treetype;
knew->word = word;
knew->left = NULL;
knew->right = NULL;

if (word < parent-> word )
parent -> left = knew;
else
parent -> right = knew;
}
else
root -> word = word;
}
{
ifstream inf;
inf.open("prog4.dat");

while(!inf.eof())
{
inf >> word;
inserttree (root,word);
}
}
void inorder(treetype *c, ofstream &outf)
{
if( c!= NULL )
{
inorder(c-> left, outf);
outf << c->word <<endl;
inorder(c -> right, outf);
}
}
void main()
{
treetype *root, *c;
string word;
ofstream outf;
outf.open("prog4.out");
inorder(c, outf);
}

```

INPUT FILE:

Horse race politics.
The first is the rating game. We seem to have become number happy. In this
computerized era, it is all too easy to measure quantity and think we are measuring
quality. We judge education by grade point averages and movies by the number of
asteriks (or stars or some other symbols) assigned to them. The future of
television shows, beauty contest entrants, novels, and even corporations rest on the
ratings. We have even been known to judge each other on a 10-point scale made
famous in a Bo Derek movie a few years back. During the 1988 presidential
campaign, many observers felt that both the media and the electorate were more
concerned with ratings than with issues, with polls than policies. As in a horse
race, the main question had become "who is winning?"

the root is supposed to be set to "horse"
and the program outputs nothing at this point

thank you!

Is This A Good Question/Topic? 0

## Replies To: In Order Traversal of Binary Tree

### #2 Martyn.Rae

• The programming dinosaur

Reputation: 542
• Posts: 1,406
• Joined: 22-August 09

## Re: In Order Traversal of Binary Tree

Posted 08 March 2010 - 01:23 PM

You don't seem to be calling createTree.

### #3 Whiskers12345

Reputation: 0
• Posts: 14
• Joined: 01-March 10

## Re: In Order Traversal of Binary Tree

Posted 08 March 2010 - 01:58 PM

Martyn.Rae, on 08 March 2010 - 12:23 PM, said:

You don't seem to be calling createTree.

Thank-you, i called create tree but now the program crashes, it doesnt give any errors or warnings?

### #4 robob686

Reputation: 7
• Posts: 44
• Joined: 24-April 09

## Re: In Order Traversal of Binary Tree

Posted 08 March 2010 - 02:25 PM

First, you shouldn't hard code the word horse into your code, you want generality in your code so that it will work for many files... so maybe something like
```void createTree(treetype *&root, string word)
{
root = new treetype;
root -> word = word;
root -> left = NULL;
root -> right = NULL;
}

```

And you could read from the file in the main method, and loop through there inserting each node after you create the tree. Post if you have any other problems.