-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.2(MinimalTree).cpp
More file actions
68 lines (52 loc) · 845 Bytes
/
4.2(MinimalTree).cpp
File metadata and controls
68 lines (52 loc) · 845 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
class tree{
public:
struct Node{
int val;
Node *left;
Node *right;
};
void insert( int val){
insertNode(head, val);
}
void Print()
{
Print1(head);
}
private:
void Print1(Node* head)
{
if (head==NULL) return;
Print1(head->left);
cout << head->val<< endl;
Print1(head->right);
}
Node* head= NULL;
void insertNode(Node* root, int val){
Node *newNode = new Node();
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
if( root==NULL){
root= newNode;
} else {
if(val<root->val){
insertNode(root->left, val);
} else {
insertNode(root->right, val);
}
}
}
};
int main(){
tree B;
B.insert(20);
B.insert(1);
B.insert(2);
B.insert(30);
B.insert(50);
B.Print();
return 0;
}