Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
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 More...
 
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 More...
 
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. More...
 
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 }

◆ 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 }

◆ 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 }
data
Definition: prime_factoriziation.c:25
node
Node, the basic data structure in the tree.
Definition: binary_search_tree.c:15
node::data
int data
data of the node
Definition: binary_search_tree.c:18