Choosing between the two(Binary Tree)

May 20, 2015 at 2:43am
I want my output to go on like this:
You: Hi
[a]Hello [b] How are you
Choose: a
Me: Hello
You: How's life?
[a]I'm good [b]lel
Choose: b
Me: lel

My current output is this:
Hi Hello I'm good
Hi Hello lel
Hi How are you

i don't know how to implement the choosing function. Right now, my code can only output all of the paths. How to do this?
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  #include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

void title()
{
	system("COLOR F3");

	cout << "\n\n\n\n\n\n\n\n";
	cout << "================================================================================\n"; 
	cout << "                                   Game Title         \n\n";
	cout << "                Press any key to start the interactive story game...";
	cout << "\n";
	cout << "================================================================================";
	system("pause>0");
}

struct node
{
	char data[1000];
	struct node* left;
	struct node* right;
};

void printArray(char ints[], int len)
{
	int i;
	for (i = 0; i<len; i++) {
		printf("%c", ints[i]);
	}
#ifdef DEBUG_INFO
	printf(" (%d)");
#endif
	cout << "\n";
}

void printPathsRecur(struct node* node, char path[], int pathLen)
{
	if (node == NULL) return;

	strncpy_s(&path[pathLen], 1000, node->data, 1000 - pathLen);
	pathLen += strlen(node->data);

	strncpy_s(&path[pathLen], 1000, "", 1000 - pathLen); 
	pathLen++;

	if (node->left == NULL && node->right == NULL)
	{
		printArray(path, pathLen);
	}
	else
	{
		printPathsRecur(node->left, path, pathLen);
		printPathsRecur(node->right, path, pathLen);
	}
}

void printPaths(struct node *node)
{
	char path[1500];
	printPathsRecur(node, path, 0);
}

//void choosepath(struct node* node, char choice)
//{
//	
//}

struct node* newNode(char *data)
{
	struct node* node = (struct node*)malloc(sizeof(struct node));
	strncpy_s(node->data, data, 1000);
	node->left = NULL;
	node->right = NULL;
	return node;
}

int main()
{
	title();
	system("cls");
	system("COLOR F2");
	cout << ".......";
	system("pause>0");
	system("cls");
	struct node *root = newNode("Hi");
	root->left = newNode("Hello");
	root->right = newNode("How are you");
	root->left->left = newNode("I'm good");
	root->left->right = newNode("lel");
	printPaths(root);
	system("pause>0");
	return 0;
}
Last edited on May 20, 2015 at 2:44am
May 20, 2015 at 3:10am
I need an algortihm/idea for this. Thank you!
May 20, 2015 at 5:51am
bump
May 20, 2015 at 9:23am
I changed this part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void printArray(char ints[], int len)
{
	char choice;
	cout << "Hi!\n";
	cout << "[a]Hello   [b]How are you?";
	cin >> choice;
	int i;
	for (i = 0; i<len; i++) {
		if (choice == 'a')
		printf("%c ", ints[1]);
		else if (choice == 'b')
			printf("%c ", ints[2]);
	}
#ifdef DEBUG_INFO
	printf(" (%d)");
#endif
	cout << "\n";
}

and it outputs
1
2
3
4
5
6
Hi!
[a]Hello      [b]How are you? 
a//this is the inputted choice
i i i i i i i i i i i i i i i i i 
Hi!
[a]Hello   [b]How are you?
May 20, 2015 at 9:35am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	cout << ints[0] << ints[1];
	cout << "\n[a]Hello   [b]How are you?";
	cout << "\n You: ";
	cin >> choice;
	if (choice == 'a')
	{
		cout << ints[3];
		cout << ints[4];
		cout << ints[5];
		cout << ints[6];
		cout << ints[7];
		cout << ints[8];
	}
	else if (choice == 'b')
	{
		cout << ints[4] << ints[5] << ints[6] << ints[13] << ints[14] << ints[15] << ints[16] << ints[17] << ints[18] << ints[19] << ints[20];
	}

I tried this code and it outputs Hello if a user choose "a" but i have the problem if the player choose "b". And it also looping back.
And also how do i make the code shorter?
May 20, 2015 at 9:52am
What has ints to do with the tree?

From what you describe it seems as if you want to find a certain node according to a certain identifier? If so add this identifier to the node.
May 20, 2015 at 10:01am
Do i have to create a new function for this?
May 20, 2015 at 10:35am
You mean finding the node? Of course. It will be similar to printPathsRecur(...) only that you don't copy or print instead you compare the identifier and return the node if found.
May 21, 2015 at 2:51am
I fixed it! :D
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>
#include<stdio.h>
#include<Windows.h>
#include<mmsystem.h>
#include<thread>
#include<stdlib.h>
using namespace std;

void openingmusic()
{
	PlaySound(TEXT("Untitled"), NULL, SND_ASYNC | SND_LOOP);
}

void storymusic()
{
	PlaySound(TEXT("suppressed"), NULL, SND_ASYNC | SND_LOOP);
}

void title()
{
	system("COLOR F3");

	cout << "\n\n\n\n\n\n\n\n";
	cout << "================================================================================\n"; 
	cout << "                                   Game Title         \n\n";
	cout << "                Press any key to start the interactive story game...";
	cout << "\n";
	cout << "================================================================================";
	system("pause>0");
}

struct node
{
	char data[1000];
	struct node* left;
	struct node* right;
};

void printArray(char ints[], int len)
{
	int i;
	for (i = 0; i<len; i++) {
		printf("%c", ints[i]);
	}
#ifdef DEBUG_INFO
	printf(" (%d)");
#endif
	cout << "\n";
}

void printPathsRecur(struct node* node, char path[], int pathLen)
{
	if (node == NULL) return;

	strncpy_s(&path[pathLen], 1000, node->data, 1000 - pathLen);
	pathLen += strlen(node->data);

	strncpy_s(&path[pathLen], 1000, "", 1000 - pathLen); 
	pathLen++;

	if (node->left == NULL && node->right == NULL)
	{
		printArray(path, pathLen);
	}
	else
	{
		printPathsRecur(node->left, path, pathLen);
		printPathsRecur(node->right, path, pathLen);
	}
}

void printPaths(struct node *node)
{
	char path[1500];
	printPathsRecur(node, path, 0);
}

struct node* newNode(char *data)
{
	struct node* node = (struct node*)malloc(sizeof(struct node));
	strncpy_s(node->data, data, 1000);
	node->left = NULL;
	node->right = NULL;
	return node;
}

int main()
{
	char choice;
	thread a(openingmusic);
	title();
	a.join();
	thread b(storymusic);
	system("cls");
	system("COLOR F2");
	cout << ".......";
	system("pause>0");
	system("cls");
	struct node *root = newNode("Hi");
	root->left = newNode("Hello");
	root->right = newNode("How are you");
	cout << "Hi\n";
	cout << "[a]Hello  [b]How are you\n";
	cout << "Your choice: "; 
	cin >> choice;
	if (choice == 'a')	{
		printPaths(root->left);
		cout << "[c] I'm good  [d] lel\n";
		cin >> choice;
		if (choice == 'c')
		{
			root->left->left = newNode("I'm good");
			printPaths(root->left->left);
		}else
			if (choice == 'd'){
			root->left->right = newNode("lel");
			printPaths(root->left->right);
			}
	}
	if (choice == 'b'){
		printPaths(root->right);
	}
	b.join();
	system("pause>0");
	return 0;
}
Topic archived. No new replies allowed.