-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
151 lines (135 loc) · 2.66 KB
/
BST.cpp
File metadata and controls
151 lines (135 loc) · 2.66 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <graphviz/gvc.h>
using namespace std;
class Node
{
public:
int key;
Node *left,*right;
Node* newNode(int item);
Node* insert(Node *root,int key);
Node* minValueNode(Node *node);
Node* del(Node *root,int key);
void write_item(Node *root,std::ofstream& ofile);
void Graph(Node *root);
void Sort(Node *root);
};
Node* Node :: newNode(int item)
{
Node *temp = new Node[10];
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Node* Node :: insert(Node *root,int key)
{
if(root == NULL)
return newNode(key);
if(key < root->key)
root->left = insert(root->left , key);
else if(key > root->key)
root->right = insert(root->right , key);
return root;
}
Node* Node :: minValueNode(Node *node)
{
Node *current = node;
while(current->left != NULL)
current = current->left;
return current;
}
Node* Node :: del(Node *root,int key)
{
if(root == NULL)
return root;
if(key < root->key)
root->left = del(root->left , key);
else if(key > root->key)
root->right = del(root->right , key);
else
{
if(root->left == NULL)
{
Node *temp = root->right;
delete root;
return temp;
}
else if(root->right == NULL)
{
Node * temp = root->left;
delete root;
return temp;
}
Node *temp = minValueNode(root->right);
root->key = temp->key;
root->right = del(root->right,temp->key);
}
return root;
}
void Node :: write_item(Node *root , std::ofstream& ofile)
{
if(root != NULL)
{
if(root->left != NULL)
ofile<<root->key<<"--"<<root->left->key<<";"<<endl;
if(root->right != NULL)
ofile<<root->key<<"--"<<root->right->key<<";"<<endl;
write_item(root->left,ofile);
write_item(root->right,ofile);
}
}
void Node :: Graph(Node *root)
{
ofstream ofile;
ofile.open("diagram.dot");
ofile<<"Graph{\n";
write_item(root,ofile);
ofile<<"}\n";
ofile.close();
}
void Node :: Sort(Node *root) //Inorder traversal for sorted items...
{
if(root != NULL)
{
Sort(root->left);
cout<<root->key<<" ";
Sort(root->right);
}
}
int main()
{
Node ob;
Node *root;
root = NULL;
int c;
while(true)
{
cout<<"\n1.Insert \n2.Delete\n3.Display graph\n4.Sorted order\n5.exit\n";
cout<<"Enter your choice : ";
cin>>c;
switch(c)
{
case 1:cout<<"Enter an inserting element : ";
int item;
cin>>item;
root = ob.insert(root,item);
break;
case 2:cout<<"Enter a key to delete : ";
int key;
cin>>key;
root = ob.del(root,key);
break;
case 3:ob.Graph(root);
break;
case 4:cout<<"Sorted order : ";
ob.Sort(root);
break;
case 5:exit(0);
default:cout<<"Invalid choice\n";
exit(0);
}
}
return 0;
}