C++ Binary Search Tree preorder printing tree problem

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming problem related to implementing a binary search tree (BST) that reads integer keys from a file and prints the tree using preorder traversal. The focus is on the implementation details, including code structure and output formatting.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant asks for help on how to print a binary search tree in a specific indented format after inserting keys from a file.
  • Another participant shares their code but notes the lack of comments, which may hinder understanding.
  • A suggestion is made to improve the insert function by reducing redundant comparisons of node data.
  • There is a proposal to utilize the currently unused depth variable in the node structure to manage indentation for the printed output.
  • A question is raised regarding the purpose of the variable "c" in the tree traversal function.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best approach to implement the printing functionality or the overall structure of the code. Multiple suggestions and critiques are presented without agreement on a single solution.

Contextual Notes

There are unresolved aspects regarding the use of certain variables and the overall efficiency of the code. The depth variable is mentioned but not implemented, and the purpose of the variable "c" remains unclear.

Faris1985
Messages
3
Reaction score
0
Q.When run the program should ask for an input file containing integer keys to be added to the
search tree. The file will contain one key per line. Add these keys in the order given in the file to
the search tree. Then print the entire tree using preorder traversal i.e., root, left-child, right-child.
The output must be indented properly to show the appropriate levels.
For example, if the input keys are 4, 3, 7, 8, 6. The output should look like this
4
<2 spaces>3
<2 spaces>7
<3 spaces>6
<3 spaces>8I have made the tree. How do I go about printing it as shown above?? ANY help would highly be appreciated.
 
Last edited:
Physics news on Phys.org
This is my code. Sorry i could not include comments :(
Code:
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
using namespace std;


struct node
{
	int data;
	int depth;
    struct node *left, *right;
};


void insert(struct node *r, struct node *p)
{
    if ((r->right == NULL) && p->data > r->data)
    r->right = p;
    else if ((r->right != NULL) && p->data> r->data)
    insert(r->right, p);
    if ((r->left == NULL) && p->data< r->data)
    r->left = p;
    else if ((r->left != NULL) && p->data< r->data )
    insert(r->left, p);
}
void tree(struct node *r, int c)
{
    int top, flag;
    struct node *w, *stack[20];
    if (r != NULL)
    {
        if (c != 4)
        {
            if (c == 1)
            cout<<r->data<<endl;
			
            tree(r->left, c);
            if (c == 2)
            cout<<r->data<<endl;
            tree(r->right, c);
            if (c == 3)
            cout<<r->data<<endl;
        }
    }
 
}

void main()
{
    int choice, c, i, flag,z,j=1;
    char temp = 'N', temp1[15];
    struct node *s, *root, *r, *q;
    root = NULL;

	string line;
	string v;
	cout<<"Enter an unspaced string of path to the file\n (ALL FOLDER NAMES AS WELL AS FILENAMES MUST NOT CONTAIN SPACES)\n"<<endl;
	cin>>v;
	int a;

	
	
	
	fstream myfile (v);
	if (myfile.is_open()) //if the file is open
	{
		while (! myfile.eof() ) //while the end of file is NOT reached
		{
			getline(myfile,line);
			int TempNumOne=line.size();
			
			
			s = (node*)malloc(sizeof(struct node));
			s->left = NULL;
			s->right = NULL;
			int pp=atoi(line.c_str());
			s->data=pp;

			
			if (root == NULL)
                root = s;
                else
                insert(root, s);
               
		}
	}
		myfile.close(); 
      
			cout<<"\nPreorder\n"<<endl;
			if (root == NULL)
				cout<<("Tree Not Started Yet");
			else
				tree(root, 1);
			j++;
			cout<<("\n Press Any Key To Continue...");
		
}
 
Last edited by a moderator:
When you enter code, please surround it with [noparse]
Code:
[/noparse] tags. This makes your code easier to read by preserving your indentation.
 
Clean up your insert routine to only test p->data against r->data once.

Think about using the (currently unset) depth variable in nodes to control spaces.
What is the point of "c" in routine "tree"?
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 13 ·
Replies
13
Views
5K