please help me

i want function to inorder traversal code (non recurcive)
for this code

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
  #include<iostream>
using namespace std;

struct node
{
	int data;
	node* left;
	node* right;
};
node* tree = NULL;
node* insert(node* tree, int ele);
void preorder(node* tree);
void inorder(node* tree);
void postorder(node* tree);
int count = 1;
void main()
{
	int ch, ele;
	do
	{
		cout << "\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a";
		cout << "\n\t\a\a2----PRE-ORDER TRAVERSAL.\a\a";
		cout << "\n\t\a\a3----IN-ORDER TRAVERSAL.\a\a";
		cout << "\n\t\a\a4----POST-ORDER TRAVERSAL.\a\a";
		cout << "\n\t\a\a5----EXIT.\a\a";
		cout << "\n\t\a\aENTER CHOICE::\a\a";
		cin >> ch;
		switch (ch)
		{
		case 1:
			cout << "\n\t\a\a*ENTER THE ELEMENT::\a\a";
			cin >> ele;
			tree = insert(tree, ele);
			break;
		case 2:
			cout << "\n\t\a\a*PRE-ORDER TRAVERSAL OF A TREE\a\a";
			preorder(tree);
			break;
		case 3:
			cout << "\n\t\a\a*IN-ORDER TRAVERSAL OF A TREE*\a\a";
			inorder(tree);
			break;
		case 4:
			cout << "\n\t\a\a*POST-ORDER TRAVERSAL OF A TREE*\a\a";
			postorder(tree);
			break;
		case 5:
			exit(0);
		}
	} while (ch != 5);
}
node* insert(node* tree, int ele)
{
	int count= 0;

	if (tree == NULL)
	{
		tree = new node;
		tree->left = tree->right = NULL;
		tree->data = ele;
		count++;
	}
	else
		if (count % 2 == 0)
			tree->left = insert(tree->left, ele);
		else
			tree->right = insert(tree->right, ele);
	return(tree);
}
void preorder(node* tree)
{
	if (tree != NULL)
	{
		cout << tree->data;
		preorder(tree->left);
		preorder(tree->right);
	}
}

void postorder(node* tree)
{
	if (tree != NULL)
	{
		postorder(tree->left);
		postorder(tree->right);
		cout << tree->data;
	}
}
> 15 int count = 1;
> 54 int count= 0;
Well your insert is broken for starters, because your messy global variable has nothing to do with the local variable of the same name.

Further, because insert is recursive, you get a FRESH 'count = 0' on each invocation, so your
if (count % 2 == 0)
would seem to just create a list, rather than a tree.

You also need to free the allocated memory for option 5 before exiting the program.

Why does in-order have to be non-recursive? Your postorder and preorder display functions are recursive, so why not in-order?

1
2
3
4
5
6
7
8
9
void inorder(node* tree)
{
	if (tree != NULL)
	{
		inorder(tree->left);
		cout << tree->data;
		inorder(tree->right);
	}
}


Also note that using the same names for function params and local variables as global names isn't recommended. In general non-const global variables aren't a good idea.
@seeplus i need this code in non recursive because i need something in non recursive thank you!!
No doubt copied from the same place that this person copied it from.
https://myclass913.wordpress.com/materials-and-notes/data-structures/binary-tree/

At least they didn't declare another local variable with the same name, nor mis-declare main as returning void.

> cout << "\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a";
WTF are all these \a's for?
\a being "make a beep" is going to be a howling banshee for anyone who has sound enabled in their terminal.
Seesh!

> i need this code in non recursive because i need something in non recursive thank you!!
Because your google-fu only turned up recursive implementations, which you've just palmed off on us as your own work.
You're too lazy to help.
@salem c
I want to tell you that my level is higher than your level and I only want to tell you go to hell
Go away, fail the course already and go sign up at your local burger flipper joint.

The world is full of chancer wannabies who think they can scam their way to some piece of paper.
Your only skill is google, and you've been found out.

Either get off your arse and do some REAL studying for once, or get out of programming.

Last edited on
my level is higher than your level
Uh, huh, so says the guy who insists main can be declared as void.

You are as non-compliant as your compiler.
abuh the cheat originally wrote
i want function to inorder traversal code (non recurcive)
for this code

Found this code and palmed it off as their own work...
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
#include<iostream>
using namespace std;

struct node
{
	int data;
	node* left;
	node* right;
};
node* tree = NULL;
node* insert(node* tree, int ele);
void preorder(node* tree);
void inorder(node* tree);
void postorder(node* tree);
int count = 1;
void main()
{
	int ch, ele;
	do
	{
		cout << "\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a";
		cout << "\n\t\a\a2----PRE-ORDER TRAVERSAL.\a\a";
		cout << "\n\t\a\a3----IN-ORDER TRAVERSAL.\a\a";
		cout << "\n\t\a\a4----POST-ORDER TRAVERSAL.\a\a";
		cout << "\n\t\a\a5----EXIT.\a\a";
		cout << "\n\t\a\aENTER CHOICE::\a\a";
		cin >> ch;
		switch (ch)
		{
		case 1:
			cout << "\n\t\a\a*ENTER THE ELEMENT::\a\a";
			cin >> ele;
			tree = insert(tree, ele);
			break;
		case 2:
			cout << "\n\t\a\a*PRE-ORDER TRAVERSAL OF A TREE\a\a";
			preorder(tree);
			break;
		case 3:
			cout << "\n\t\a\a*IN-ORDER TRAVERSAL OF A TREE*\a\a";
			inorder(tree);
			break;
		case 4:
			cout << "\n\t\a\a*POST-ORDER TRAVERSAL OF A TREE*\a\a";
			postorder(tree);
			break;
		case 5:
			exit(0);
		}
	} while (ch != 5);
}
node* insert(node* tree, int ele)
{
	int count= 0;

	if (tree == NULL)
	{
		tree = new node;
		tree->left = tree->right = NULL;
		tree->data = ele;
		count++;
	}
	else
		if (count % 2 == 0)
			tree->left = insert(tree->left, ele);
		else
			tree->right = insert(tree->right, ele);
	return(tree);
}
void preorder(node* tree)
{
	if (tree != NULL)
	{
		cout << tree->data;
		preorder(tree->left);
		preorder(tree->right);
	}
}

void postorder(node* tree)
{
	if (tree != NULL)
	{
		postorder(tree->left);
		postorder(tree->right);
		cout << tree->data;
	}
}

Quoted before the inevitable "close account and bugger off".
@salem c
first of all you blamed me that i copied this command from google and its not. Its from the proffesor of my subject in the unversity and he wanted me to change this command from recursive to non-recursive and I believe that you fail of doing it and started bringing excuses you piece of shit.
Last edited on
Found / given is just a matter of semantics, the end result is still the same, the code isn't YOURS.

Besides, if it really is from your "professor", the code is so awful you should be dropping the course and getting your money back.
Much of the so called C++ code taught (and still available via the Internet) is just previous C code with small changes (printf to cout etc) to make it look like C++. C++ taught as 'C with bits added on' - rather than a language in it's own right.
Topic archived. No new replies allowed.