-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTreeRecall.cpp
More file actions
74 lines (72 loc) · 1.52 KB
/
binarySearchTreeRecall.cpp
File metadata and controls
74 lines (72 loc) · 1.52 KB
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
#include <iostream>
using namespace std;
struct node {
node * left;
int data;
node * right;
};
node * CreateNode() {
node * temp;
temp = new node;
if (temp) {
temp -> left = NULL;
temp -> right = NULL;
return temp;
}
}
node * InsertNodes(node ** rootNode, int data = 0) {
node * temp1, * temp2;
if ( * rootNode != NULL) {
temp2 = * rootNode;
if (temp2 -> data < data) {
InsertNodes( & temp2 -> right, data);
} else {
InsertNodes( & temp2 -> left, data);
}
} else {
temp1 = CreateNode();
cout << "hello " << temp1 << endl;
* rootNode = temp1;
temp1 -> data = data;
//system("pause");
}
}
void PrintData(node **rootNode) {
node * temp;
temp = *rootNode;
if (*rootNode != NULL) {
cout << temp -> data << " ";
PrintData(&temp -> left);
PrintData(&temp -> right);
} else {
// cout<<"Node is Empty"<<endl;
return;
}
}
int main() {
struct node * rootNode = NULL;
int choice, d;
while (1) {
system("cls");
cout << "Enter choice" << endl;
cout << "1.Insert Nodes& Branches" << endl;
cout << "2.Print data" << endl;
cin >> choice;
switch (choice) {
case 1: {
cout << "Enter Data" << endl;
cin >> d;
if (rootNode == NULL) {
rootNode = InsertNodes( & rootNode, d);
} else {
InsertNodes( & rootNode, d);
}
break;
}
case 2: {
PrintData(&rootNode);
system("pause");
}
}
}
}