Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
threaded_binary_trees.c File Reference

This file is a simple implementation of a Threaded Binary Tree. More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for threaded_binary_trees.c:

Data Structures

struct  Node
 Node, the basic data structure of the tree. More...
 

Typedefs

typedef struct Node node
 Node, the basic data structure of the tree.
 

Functions

nodecreate_node (int data)
 creates a new node param[in] data value to be inserted
 
void insert_bt (node **root, int data)
 inserts a node into the tree param[in,out] root pointer to node pointer to the topmost node of the tree param[in] data value to be inserted into the tree
 
void search (node *root, int ele)
 searches for the element
 
void inorder_display (node *curr)
 performs inorder traversal param[in] curr node pointer to the topmost node of the tree
 
void postorder_display (node *curr)
 performs postorder traversal param[in] curr node pointer to the topmost node of the tree
 
void preorder_display (node *curr)
 performs preorder traversal param[in] curr node pointer to the topmost node of the tree
 
void delete_bt (node **root, int ele)
 deletion of a node from the tree if the node isn't present in the tree, it takes no action.
 
int main ()
 main function
 

Detailed Description

This file is a simple implementation of a Threaded Binary Tree.

Threaded Binary Tree is a binary tree variant in which all left child pointers that are NULL (in Linked list representation) point to its in-order predecessor, and all right child pointers that are NULL (in Linked list representation) point to its in-order successor. It has the following functionalities:

  • Insertion
  • Search
  • Deletion
  • Listing of node keys inorder,preorder,postorder

-see binary_search_tree.c

Author
Amitha Nayak

Function Documentation

◆ create_node()

node * create_node ( int  data)

creates a new node param[in] data value to be inserted

Returns
a pointer to the new node
39{
40 node *ptr = (node *)malloc(sizeof(node));
41 ptr->rlink = ptr->llink = NULL;
42 ptr->data = data;
43 return ptr;
44}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
Definition prime_factoriziation.c:25
Node, the basic data structure in the tree.
Definition binary_search_tree.c:15
int data
data of the node
Definition binary_search_tree.c:18

◆ delete_bt()

void delete_bt ( node **  root,
int  ele 
)

deletion of a node from the tree if the node isn't present in the tree, it takes no action.

param[in,out] root pointer to node pointer to the topmost node of the tree param[in] ele value to be deleted from the tree

174{
175 node *temp;
176 node *prev;
177 if (*root == NULL)
178 return;
179 else
180 {
181 temp = *root;
182 prev = NULL;
183 // search
184 while (temp != NULL)
185 {
186 if (temp->data == ele)
187 {
188 break;
189 }
190 else if (ele > temp->data)
191 {
192 prev = temp;
193 temp = temp->rlink;
194 }
195 else
196 {
197 prev = temp;
198 temp = temp->llink;
199 }
200 }
201 }
202
203 if (temp == NULL)
204 return;
205 else
206 {
207 node *replacement; // deleted node's replacement
208 node *t;
209 if (temp->llink == NULL && temp->rlink == NULL)
210 {
211 replacement = NULL;
212 }
213 else if (temp->llink == NULL && temp->rlink != NULL)
214 {
215 replacement = temp->rlink;
216 }
217 else if (temp->llink != NULL && temp->rlink == NULL)
218 {
219 replacement = temp->llink;
220 }
221 else
222 {
223 replacement = temp->rlink; // replaced with inorder successor
224 t = replacement;
225 while (t->llink != NULL)
226 {
227 t = t->llink;
228 }
229 t->llink =
230 temp->llink; // leftmost node of the replacement is linked to
231 // the left child of the deleted node
232 }
233
234 if (temp == *root)
235 {
236 free(*root);
237 *root = replacement;
238 }
239 else if (prev->llink == temp)
240 {
241 free(prev->llink);
242 prev->llink = replacement;
243 }
244 else if (prev->rlink == temp)
245 {
246 free(prev->rlink);
247 prev->rlink = replacement;
248 }
249 }
250}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26

◆ inorder_display()

void inorder_display ( node curr)

performs inorder traversal param[in] curr node pointer to the topmost node of the tree

130{
131 if (curr != NULL)
132 {
133 inorder_display(curr->llink);
134 printf("%d\t", curr->data);
135 inorder_display(curr->rlink);
136 }
137}
void inorder_display(node *curr)
performs inorder traversal param[in] curr node pointer to the topmost node of the tree
Definition threaded_binary_trees.c:129
Here is the call graph for this function:

◆ insert_bt()

void insert_bt ( node **  root,
int  data 
)

inserts a node into the tree param[in,out] root pointer to node pointer to the topmost node of the tree param[in] data value to be inserted into the tree

52{
53 node *new_node = create_node(data);
54 node *temp; // to be deleted
55 node *prev; // keeps track of the parent of the element deleted
56 if (*root == NULL)
57 {
58 *root = new_node;
59 }
60 else
61 {
62 temp = *root;
63 prev = NULL;
64 while (temp != NULL)
65 {
66 if (new_node->data > temp->data)
67 {
68 prev = temp;
69 temp = temp->rlink;
70 }
71 else if (new_node->data < temp->data)
72 {
73 prev = temp;
74 temp = temp->llink;
75 }
76 else
77 {
78 return;
79 }
80 }
81
82 if (new_node->data > prev->data)
83 {
84 prev->rlink = new_node;
85 }
86 else
87 {
88 prev->llink = new_node;
89 }
90 }
91}
node * create_node(int data)
creates a new node param[in] data value to be inserted
Definition threaded_binary_trees.c:38
Here is the call graph for this function:

◆ main()

int main ( void  )

main function

256{
257 printf("BINARY THREADED TREE: \n");
258 node *root = NULL;
259 int choice, n;
260 do
261 {
262 printf("%s\n", "1. Insert into BT");
263 printf("%s\n", "2. Print BT - inorder");
264 printf("%s\n", "3. Print BT - preorder");
265 printf("%s\n", "4. print BT - postorder");
266 printf("%s\n", "5. delete from BT");
267 printf("%s\n", "6. search in BT");
268 printf("%s\n", "Type 0 to exit");
269 scanf("%d", &choice);
270
271 switch (choice)
272 {
273 case 1:
274 printf("%s\n", "Enter a no:");
275 scanf("%d", &n);
276 insert_bt(&root, n);
277 break;
278 case 2:
279 inorder_display(root);
280 printf("\n");
281 break;
282 case 3:
283 preorder_display(root);
284 printf("\n");
285 break;
286 case 4:
287 postorder_display(root);
288 printf("\n");
289 break;
290 case 5:
291 printf("%s\n", "Enter a no:");
292 scanf("%d", &n);
293 delete_bt(&root, n);
294 break;
295 case 6:
296 printf("%s\n", "Enter a no:");
297 scanf("%d", &n);
298 search(root, n);
299 break;
300 }
301 } while (choice != 0);
302 return 0;
303}
void delete_bt(node **root, int ele)
deletion of a node from the tree if the node isn't present in the tree, it takes no action.
Definition threaded_binary_trees.c:173
void search(node *root, int ele)
searches for the element
Definition threaded_binary_trees.c:98
void postorder_display(node *curr)
performs postorder traversal param[in] curr node pointer to the topmost node of the tree
Definition threaded_binary_trees.c:143
void preorder_display(node *curr)
performs preorder traversal param[in] curr node pointer to the topmost node of the tree
Definition threaded_binary_trees.c:157
void insert_bt(node **root, int data)
inserts a node into the tree param[in,out] root pointer to node pointer to the topmost node of the tr...
Definition threaded_binary_trees.c:51
Here is the call graph for this function:

◆ postorder_display()

void postorder_display ( node curr)

performs postorder traversal param[in] curr node pointer to the topmost node of the tree

144{
145 if (curr != NULL)
146 {
147 postorder_display(curr->llink);
148 postorder_display(curr->rlink);
149 printf("%d\t", curr->data);
150 }
151}
Here is the call graph for this function:

◆ preorder_display()

void preorder_display ( node curr)

performs preorder traversal param[in] curr node pointer to the topmost node of the tree

158{
159 if (curr != NULL)
160 {
161 printf("%d\t", curr->data);
162 preorder_display(curr->llink);
163 preorder_display(curr->rlink);
164 }
165}
Here is the call graph for this function:

◆ search()

void search ( node root,
int  ele 
)

searches for the element

Parameters
[in]rootnode pointer to the topmost node of the tree
[in]elevalue searched for
99{
100 node *temp = root;
101 while (temp != NULL)
102 {
103 if (temp->data == ele)
104 {
105 break;
106 }
107 else if (ele > temp->data)
108 {
109 temp = temp->rlink;
110 }
111 else
112 {
113 temp = temp->llink;
114 }
115 }
116
117 if (temp == NULL)
118 {
119 printf("%s\n", "Element not found.");
120 }
121 else
122 printf("%s\n", "Element found.");
123}