forked from Rohail-Suii/DSA-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.h
More file actions
128 lines (123 loc) · 2.36 KB
/
tree.h
File metadata and controls
128 lines (123 loc) · 2.36 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
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
127
128
#include <iostream>
using namespace std;
class tree{
public:
double num;
string name;
string contactnum;
string Location;
int Callnum1;
tree *left;
tree *right;
tree *root;
tree(){
num=0;
name="";
contactnum="";
Location="";
Callnum1=0;
left = NULL;
right = NULL;
root = NULL;
}
void insert(string l,int c){
tree *curr = new tree;
cout<<"Enter your name\n";
cin>>curr->name;
cout<<"Enter your CNIC\n";
cin>>curr->num;
cout<<"Enter your Contact number\n";
cin>>curr->contactnum;
curr->Location=l;
curr->Callnum1=c;
if(root==NULL){
root = curr;
}else{
tree *p = root;
tree *k = p;
while(p!=NULL){
k=p;
if(curr->num < p->num){
p=p->left;
}else{
p=p->right;
}
}
if(curr->num < k->num){
k->left = curr;
}else{
k->right = curr;
}
}
}//when root has one branch
void deletenode(int key){
tree *p = root;
tree *k = p;
while(p->num!=key && p!=NULL){
k=p;
if(key<p->num){
p=p->left;
}else{
p=p->right;
}
}
if(p->left==NULL && p->right==NULL){
if(k->left=p){
k->left=NULL;
}else{
k->right=NULL;
}
}else if(p->left==NULL or p->right==NULL){
if(k->left==p && p->right==NULL){
k->left=p->left;
}else if(k->left==p && p->left==NULL){
k->left = p->right;
}else if(k->right==p && p->left==NULL){
k->right = p->right;
}else if(k->right==p && p->right==NULL){
k->right = p->left;
}
}else{
tree *p2 = p->right;
if(p2->left==NULL){
p->num=p2->num;
p->right=p2->right;
}else{
while(p2->left!=NULL){
k=p2;
p2=p2->left;
}
p->num=p2->num;
k->left=NULL;
}
}
}
void inorder(tree *p){
if(p!=0){
inorder(p->left);
cout<<"Call Number "<<p->Callnum1<<endl;
cout<<"Name "<<p->name<<endl;
cout<<"Location "<<p->Location<<endl;
cout<<"Contact Number "<<p->contactnum<<endl;
inorder(p->right);
}
}
int search(tree *p,string loc){
if(p!=0){
inorder(p->left);
if(p->Location==loc){
return 1;
}
inorder(p->right);
}return 0;
}
};
//int main(){
// insert();
// inorder(root);
// int num;
// cout<<"\nEnter num to delete";
// cin>>num;
// deletenode(num);
// inorder(root);
//}