Sunday, August 30, 2009

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...

Saturday, August 29, 2009

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 - We have to adjust the pointer of the parent of the node to be deleted such that...

Friday, August 28, 2009

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. If there is no such ancestor, then succ(u) is undefined. An iterator...

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:#define...