-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletion_in_binary_tree_by_adjusting_rightmost_node.cpp
More file actions
168 lines (135 loc) · 3.6 KB
/
Copy pathdeletion_in_binary_tree_by_adjusting_rightmost_node.cpp
File metadata and controls
168 lines (135 loc) · 3.6 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <malloc.h>
/* tree node*/
struct node {
int key;
struct node * left;
struct node * right;
};
/* function for creating new node of tree*/
struct node *newNode(int data)
{
struct node *node = (struct node *)malloc(sizeof(struct node));
node->key = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* function for traversing in inorder tree traversal */
void inOrderTraversal(struct node * root){
if(root == NULL){
return ;
}
inOrderTraversal(root->left);
printf("%d ",root->key);
inOrderTraversal(root->right);
}
struct node * temp5 ;
void preOrderTraversal(struct node * root,struct node * child){
if(root == NULL){
return ;
}
if (root->left == child ){
temp5 = root;
return;
}
if(root->right == child){
temp5 = root;
return;
}
inOrderTraversal(root->left);
inOrderTraversal(root->right);
}
struct node * tempforsearch ;
void search(struct node* node,
int val)
{
if (node == NULL)
return ;
if (node->key == val) {
tempforsearch = node;
}
else {
search(node->left, val);
search(node->right, val);
}
}
/*function to find parent of a node in binary tree */
node * findParent(struct node* node,
int val, struct node * parent)
{
if (node == NULL)
return NULL;
if (node->key == val) {
return parent;
}
else {
findParent(node->left, val, node);
findParent(node->right, val, node);
}
}
/* function for traversing in order level tree traversal */
int height(struct node * root){
if (root == NULL){
return 0;
} else{
int lheight = height(root->left);
int rheight = height(root->right);
if (lheight > rheight)
return(lheight + 1);
else return(rheight + 1);
}
}
node * printnodesatheight(struct node * root,int level,int right){
if (root == NULL)
return NULL;
if (level == 1){
if (right == 1){
return root;
}
}
else if (level > 1)
{
printnodesatheight(root->left, level-1,0);
printnodesatheight(root->right, level-1,1);
}
}
node * breadthFirstSearch(struct node * root){
struct node * rightmostnode;
int h = height(root);
rightmostnode = printnodesatheight(root,h,-1 );
return rightmostnode;
}
/* function for deleting a node from the tree */
void deleteANodeFromBinaryTree(int key,struct node * root){
search(root, key);
struct node * deletionNode = tempforsearch;
struct node * rightmost = breadthFirstSearch(root);
struct node * parentofrightmost = findParent(root,rightmost->key,NULL);
parentofrightmost->right = NULL;
rightmost->left = deletionNode->left;
rightmost->right = deletionNode->right;
preOrderTraversal(root,deletionNode);
struct node * parentofdeletion = temp5;
if(parentofdeletion->left == deletionNode){
parentofdeletion->left = rightmost;
}else{
parentofdeletion->right = rightmost;
}
}
int main(){
struct node * root = newNode(10);
root->left = newNode(11);
root->left->left = newNode(7);
root->left->right = newNode(12);
root->right = newNode(9);
root->right->left = newNode(15);
root->right->right = newNode(8);
printf("%s","Inorder traversal of the tree is \n");
inOrderTraversal(root);printf("\n");
int key = 11;
deleteANodeFromBinaryTree(key,root);
printf("%s","Inorder traversal of the tree is \n");
inOrderTraversal(root);printf("\n");
return 0;
}