In Order Traversal of Binary Tree

need some help printing a tree read in from data file

Page 1 of 1

3 Replies - 8867 Views - Last Post: 08 March 2010 - 02:25 PM Rate Topic: -----

#1 Whiskers12345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}
void readem(string word, treetype *root)
{
	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;
	readem(word, root);
	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  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Whiskers12345  Icon User is offline

  • New D.I.C Head

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

Re: In Order Traversal of Binary Tree

Posted 08 March 2010 - 01:58 PM

View PostMartyn.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?
Was This Post Helpful? 0
  • +
  • -

#4 robob686  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1