-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary tree Data Structure.c
127 lines (105 loc) · 3.68 KB
/
Binary tree Data Structure.c
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
/* Binary tree Data structure implementation
Date created: Saturday; February 28, 2023 */
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
};
struct node* create_node(int data) {
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
struct node* insert_node(struct node* root, int data) {
if (root == NULL) {
return create_node(data);
}
if (data < root->data) {
root->left = insert_node(root->left, data);
} else if (data > root->data) {
root->right = insert_node(root->right, data);
}
return root;
}
struct node* delete_node(struct node* root, int data) {
if (root == NULL) {
return root;
}
if (data < root->data) {
root->left = delete_node(root->left, data);
} else if (data > root->data) {
root->right = delete_node(root->right, data);
} else {
if (root->left == NULL) {
struct node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
struct node* temp = root->right;
while (temp->left != NULL) {
temp = temp->left;
}
root->data = temp->data;
root->right = delete_node(root->right, temp->data);
}
return root;
}
struct node* edit_node(struct node* root, int old_data, int new_data) {
if (root == NULL) {
return root;
}
if (old_data < root->data) {
root->left = edit_node(root->left, old_data, new_data);
} else if (old_data > root->data) {
root->right = edit_node(root->right, old_data, new_data);
} else {
root->data = new_data;
}
return root;
}
void print_inorder(struct node* root) {
if (root != NULL) {
print_inorder(root->left);
printf("%d ", root->data);
print_inorder(root->right);
}
}
int main() {
struct node* root = NULL;
root = insert_node(root, 50);
root = insert_node(root, 30);
root = insert_node(root, 20);
root = insert_node(root, 40);
root = insert_node(root, 70);
root = insert_node(root, 60);
root = insert_node(root, 80);
printf("Inorder traversal of the binary tree: ");
print_inorder(root);
printf("\n");
int old_data, new_data;
printf("Enter the data value of the node to edit: ");
scanf("%d", &old_data);
printf("Enter the new data value for the node: ");
scanf("%d", &new_data);
root = edit_node(root, old_data, new_data);
printf("Inorder traversal of the binary tree after editing a node: ");
print_inorder(root);
printf("\n");
int data_to_delete;
printf("Enter the data value of the");
root = delete_node(root, data_to_delete);
printf("Inorder traversal of the binary tree after deleting a node: ");
print_inorder(root);
printf("\n");
return 0;
}
// In this modified version, after the binary tree is constructed and printed, the user is prompted to enter the data value of the node they want to edit. After entering the old and new data values, the `edit_node` function is called to modify the node with the old data value to have the new data value. Then, the binary tree is printed again to show the modification.
// Next, the user is prompted to enter the data value of the node they want to delete. After entering the value, the `delete_node` function is called to remove the node with that data value from the binary tree. Finally, the binary tree is printed again to show the deletion.