Datastructures- binary tree

Can someone please check my code? I'm trying to implement a binary tree

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

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

node* first = NULL, *current;


void preorder(node* head);
int main()
{
	char ch = 'y';
	while(ch == 'y')
	{
		if (first == NULL)
		{
			node* newNode = new node;
			cout << "Enter a number: \n";
			cin >> newNode->data;
			newNode->left = NULL;
			newNode->right = NULL;
			first = newNode;

		}
		else
		{
			current = first;
			node* newNode = new node;
			cout << "Enter a number: \n";
			cin >> newNode->data;
			newNode->left = newNode->right = NULL;
			while(current != NULL)
			{
				if (newNode->data < current->data)
				{
					current = current->left;
				}
				else
				{
					current = current->right;
				}
	
			}
			current = newNode;

		}
		cout << "Insert another node? \n";
		cin >> ch;
	}
	preorder(first);
	system("pause");
	return 0;
}

void preorder(node* head)
{
	if (head != NULL)
	{
		cout << head->data << " ";
		preorder(head->left);
		preorder(head->right);
	}
}
Topic archived. No new replies allowed.