-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary_Seach_Tree_queue.cpp
More file actions
92 lines (92 loc) · 1.65 KB
/
Binary_Seach_Tree_queue.cpp
File metadata and controls
92 lines (92 loc) · 1.65 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
#include<iostream>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
typedef node* NODE;
NODE get_node(int x)
{
NODE n=new node;
if(n==NULL)
cout<<"Memory Full";
n->data=x;
n->left=NULL;
n->right=NULL;
return n;
}
NODE insert_node(NODE &parent,int x)
{
if(parent==NULL)
parent=get_node(x);
else
{
if(parent->data>x)
parent->left=insert_node(parent->left,x);
else if(parent->data<x)
parent->right=insert_node(parent->right,x);
}
return parent;
}
void inorder(NODE p)
{
if(p!=NULL)
{
inorder(p->left);
cout<<p->data<<"->";
inorder(p->right);
}
}
NODE delete_node(NODE root)
{
if(root==NULL)
{
cout<<"Queue Empty";
return root;
}
NODE temp1=root,temp2=root;
while(temp1->left!=NULL)
{
temp2=temp1;
temp1=temp1->left;
}
if(temp1==root)
root=root->right;
else
temp2->left=temp1->right;
cout<<"\nRemoved : "<<temp1->data;
delete temp1;
return root;
}
int main()
{
NODE root=NULL;
int x,k=1,ch;
while(k)
{
cout<<"\n1.Insert\n2.Remove\n3.Display\n4.Exit";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter Element : ";
cin>>x;
root=insert_node(root,x);
break;
case 2:
root=delete_node(root);
break;
case 3:
inorder(root);
break;
case 4:
k=0;
break;
default:
cout<<"\nWrong option";
break;
}
}
}