Pre Order traversal - Recursive and Iterative solution

Example Consider the tree: div class="separator” style="clear: both; text-align: center;“> To traverse a binary tree in Preorder, following operations are carried-out (i) Visit the root, (ii) Traverse the left subtree, and (iii) Traverse the right subtree. Therefore, the Preorder traversal of the above tree will outputs: 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10 Recursive solution preorder(N \*root) { if(root) { printf("Value : %d", root->value); preorder(root->left); preorder(root->right); } } Iterative solution [Read More]

tree1.h fully updated

//We will do following Functions: 1. lookup 2. Insert 3. printTree,printPost, printPre 4. Selection Function 5. Delete(Hence implemented - child0,child1,child2, inorder2 functions) #ifndef TREE1_H #define TREE1\_H ``` ``` \# include <stdio.h> # include <stdlib.h> #define isChildless(p) p->left==NULL &&p->right==NULL enum BOOL{false,true} ; typedef enum BOOL BOOL; ``` ``` struct node { int data; struct node\* left; struct node* right; } ; typedef struct node node; /* Given a binary tree, return true if a node with the target data is found in the tree. [Read More]

Deleting a node from a Binary Search Tree

Suppose we want to delete a node k. First we need to search for the node k. Offcourse k should not be null. Then we have three cases: 1. The node to be deleted has no children - The memory occupied by this node must be freed and either the left link or the right link of the parent of this node must be set to NULL. 2. The node to be deleted has exactly one child - [Read More]

Inorder successor OR Predecessor of BST node

It is important to understand inorder successor of BST because it helps us understand deletion of node from a tree, when the node deleted has 2 children. To find the inorder successor of node u: If u has a right child, r, then succ(u) is the leftmost descendent of r Otherwise, succ(u) is the closest ancestor, v, of u (if any) such that u is descended from the left child of v. [Read More]

Implementing BST in C ( header file )

This post contains header file which contains binary search tree related function. The tree contains following functions: Search Create new node Get the size of tree Insert Delete Print inorder, pre order and post order Starting the header file: Lets name our header file as Tree1.h. #ifndef TREE1\_H #define TREE1\_H #include <stdio.h> #include <malloc.h> enum BOOL{false,true}; struct node { int data; struct node\* left; struct node\* right; } ; typedef struct node node; typedef enum BOOL BOOL; Defining some functions via c macros: [Read More]