Building a Binary Tree from a file!

Hello everyone! I'm new here, and I have this project that I'm supposed to do with C++. So, I have this txt file that has

question : Is it a feline?
object : cat
question : Is it a canine?
object : dog
object : pangolin

And, from that txt file, I have to create it like this

---------------------- Is it a feline?
-------------------/ ------------------\
-----------------Cat ----------------Is it a canine?
------------------------------------/---------------\
---------------------------------Dog--------------Pangolin

I'm really torn about this because my building code has recursion but I don't know where to put the return, you can check the code here.

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
tree *loading()
{
	tree *ptr;
	fgets(qoro,100,data2file);
	if(qoro==NULL)
	{
		return NULL;
	}
	else
	{
	ptr=new tree;
	ptr->question[0]='\0';
	ptr->object[0]='\0';
	ptr->yes=NULL;
	ptr->no=NULL;
		if(qoro[0]=='q')
		{
			strcpy(ptr->question,qoro);
			if(root==NULL)
			{
			root=ptr;
			}
			ptr->object[0]='\0';
			ptr->yes=loading();
			ptr->no=loading();
		}
		else
		{
			strcpy(ptr->object,qoro);
			ptr->question[0]='\0';
			ptr->yes=NULL;
			ptr->no=NULL;
		}
	}
}


It actually works fine, is just that the nodes won't connect. Any help? :(
Doesn't the return ptr; go after line 34?

You might be confused with root there. The original call to the function should be: root = loading();. I don't think there needs any access to root from within the function.

Should work... I could see ways a teacher could expand on this, multiple types of feline (stray, wild), multiple felines of that type (tabby, Russian blue).
Last edited on
Thanks, but it didn't work. :(. It still outputs the same, as

Is it a feline?
cat


which should be

Is it a feline?
cat
is it a canine?
dog
pangolin


Edit :

This code is to create a tree from a file. So, if the root node is null, then the first item will be in the root node, hence root=ptr. :)

But what happens is

Is it a feline -> (Left node) Cat, (right node) null [which in this case should be "Is it a canine?". :(.

I'm sorry if I'm confusing. But thank you so much for the explanations!!
Last edited on
Maybe you are having an issue with recursion. I wrote a program based off of your code, and it works without any significant changes.

The print should look something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print(tree* ptr)
{
if (!ptr) return;

if question   : print question
else if object: print object

print(ptr->yes);
print(ptr->no);
}

// Main
tree* root = loadtree
print(root);



Here is the program I wrote, please don't copy it too much :)


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
63
64
65
66
67
#include <fstream>
#include <iostream>
#include <cstring>

struct Node{
  char question[100];
  char object[100];
  Node* yes;
  Node* no;
};

Node *loading(std::ifstream& in){
  char temp[100];
  if (!(in.getline(temp, 100))) return NULL;
  
  Node *ptr = new Node();
  
  ptr->question[0]='\0';
  ptr->object[0]='\0';
  ptr->yes=NULL;
  ptr->no=NULL;
  if(temp[0]=='q')
  {
    strcpy(ptr->question,temp);
    ptr->object[0]='\0';
    ptr->yes=loading(in);
    ptr->no=loading(in);
  }
  else
  {
    strcpy(ptr->object,temp);
    ptr->question[0]='\0';
    ptr->yes=NULL;
    ptr->no=NULL;
  }
}

void print(Node* ptr)
{
  if (!ptr) return;
  
  if (ptr->question[0] != '\0')
    std::cout << ptr->question << '\n';
  else if (ptr->object[0] != '\0')
    std::cout << ptr->object << '\n';
    
  print(ptr->yes);
  print(ptr->no);
}
void clean(Node* ptr)
{
  if (!ptr) return;
  clean(ptr->yes);
  clean(ptr->no);
  delete ptr;
}

int main(void)
{
  std::ifstream in("data.txt");
  Node* head = NULL;
  head = loading(in);
  print(head);
  clean(head);
  head = NULL;
  return 0;
}


~Binary trees are when I decided it would be easier to use a graphical API than figure out how to relate depth with width on the console.~
Last edited on
Sorry for the late reply, I did a test on your program and it works perfectly! I won't copy it, I'll analyze it and try to create my own version of the program! Thank you so much!
Topic archived. No new replies allowed.