Posts

Program to maintain a heap.

#include #include void restoreup ( int, int * ) ; void restoredown ( int, int *, int ) ; void makeheap ( int *, int ) ; void add ( int, int *, int * ) ; int replace ( int, int *, int ) ; int del ( int *, int * ) ; void main( ) { int arr [20] = { 1000, 7, 10, 25, 17, 23, 27, 16, 19, 37, 42, 4, 33, 1, 5, 11 } ; int i, n = 15 ; clrscr( ) ; makeheap ( arr, n ) ; printf ( "Heap:\n" ) ; for ( i = 1 ; i printf ( "%d\t", arr [i] ) ; i = 24 ; add ( i, arr, &n ) ; printf ( "\n\nElement added %d.\n", i ) ; printf ( "\nHeap after addition of an element:\n" ) ; for ( i = 1 ; i printf ( "%d\t", arr [i] ) ; i = replace ( 2, arr, n ) ; printf ( "\n\nElement replaced %d.\n", i ) ; printf ( "\nHeap after replacement of an element:\n" ) ; for ( i = 1 ; i printf ( "%d\t", arr [i] ) ; i = del ( arr, &n ) ; printf ( "\n\nElement deleted %d.\n", i ) ; printf ( "\nHeap after dele...

Program which maintains a B-tree of order 5.

#include #include #include #include #define MAX 4 #define MIN 2 struct btnode { int count ; int value[MAX + 1] ; struct btnode *child[MAX + 1] ; } ; struct btnode * insert ( int, struct btnode * ) ; int setval ( int, struct btnode *, int *, struct btnode ** ) ; struct btnode * search ( int, struct btnode *, int * ) ; int searchnode ( int, struct btnode *, int * ) ; void fillnode ( int, struct btnode *, struct btnode *, int ) ; void split ( int, struct btnode *, struct btnode *, int, int *, struct btnode ** ) ; struct btnode * delete ( int, struct btnode * ) ; int delhelp ( int, struct btnode * ) ; void clear ( struct btnode *, int ) ; void copysucc ( struct btnode *, int ) ; void restore ( struct btnode *, int ) ; void rightshift ( struct btnode *, int ) ; void leftshift ( struct btnode *, int ) ; void merge ( struct btnode *, int ) ; void display ( struct btnode * ) ; void main( ) { struct node *root ; root = NULL ; clrscr( ) ; root = insert ( 27, root ) ; root = insert ( ...

Program to maintain an AVL tree.

#include #include #include #define FALSE 0 #define TRUE 1 struct AVLNode { int data ; int balfact ; struct AVLNode *left ; struct AVLNode *right ; } ; struct AVLNode * buildtree ( struct AVLNode *, int, int * ) ; struct AVLNode * deldata ( struct AVLNode *, int, int * ) ; struct AVLNode * del ( struct AVLNode *, struct AVLNode *, int * ) ; struct AVLNode * balright ( struct AVLNode *, int * ) ; struct AVLNode * balleft ( struct AVLNode *, int * ) ; void display ( struct AVLNode * ) ; void deltree ( struct AVLNode * ) ; void main( ) { struct AVLNode *avl = NULL ; int h ; clrscr( ) ; avl = buildtree ( avl, 20, &h ) ; avl = buildtree ( avl, 6, &h ) ; avl = buildtree ( avl, 29, &h ) ; avl = buildtree ( avl, 5, &h ) ; avl = buildtree ( avl, 12, &h ) ; avl = buildtree ( avl, 25, &h ) ; avl = buildtree ( avl, 32, &h ) ; avl = buildtree ( avl, 10, &h ) ; avl = buildtree ( avl, 15, &h ) ; avl = buildtree ( avl, 27, &h ) ; avl = buildtree...

Program to maintain a threaded binary tree.

#include #include #include enum boolean { false = 0, true = 1 } ; struct thtree { enum boolean isleft ; struct thtree *left ; int data ; struct thtree *right ; enum boolen isright ; } ; void insert ( struct thtree **, int ) ; void delete ( struct thtree **, int ) ; void search ( struct thtree **, int, struct thtree **, struct thtree **, int * ) ; void inorder ( struct thtree * ) ; void deltree ( struct thtree ** ) ; void main( ) { struct thtree *th_head ; th_head = NULL ; /* empty tree */ insert ( &th_head, 11 ) ; insert ( &th_head, 9 ) ; insert ( &th_head, 13 ) ; insert ( &th_head, 8 ) ; insert ( &th_head, 10 ) ; insert ( &th_head, 12 ) ; insert ( &th_head, 14 ) ; insert ( &th_head, 15 ) ; insert ( &th_head, 7 ) ; clrscr( ) ; printf ( "Threaded binary tree before deletion:\n" ) ; inorder ( th_head ) ; delete ( &th_head, 10 ) ; printf ( "\nThreaded binary tree after deletion:\n" ) ; inorder ( th_he...

Program to insert and delete a node from the binary search tree.

#include #include #include #define TRUE 1 #define FALSE 0 struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void delete ( struct btreenode **, int ) ; void search ( struct btreenode **, int, struct btreenode **, struct btreenode **, int * ) ; void inorder ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int req, i = 0, num, a[ ] = { 11, 9, 13, 8, 10, 12, 14, 15, 7 } ; bt = NULL ; /* empty tree */ clrscr( ) ; while ( i { insert ( &bt, a[i] ) ; i++ ; } clrscr( ) ; printf ( "Binary tree before deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 10 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 14 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 8 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 13 ) ; ...

Program to implement a binary search tree.

#include #include #include struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void inorder ( struct btreenode * ) ; void preorder ( struct btreenode * ) ; void postorder ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int req, i = 1, num ; bt = NULL ; /* empty tree */ clrscr( ) ; printf ( "Specify the number of items to be inserted: " ) ; scanf ( "%d", &req ) ; while ( i++ { printf ( "Enter the data: " ) ; scanf ( "%d", &num ) ; insert ( &bt, num ) ; } printf ( "\nIn-order Traversal: " ) ; inorder ( bt ) ; printf ( "\nPre-order Traversal: " ) ; preorder ( bt ) ; printf ( "\nPost-order Traversal: " ) ; postorder ( bt ) ; } /* inserts a new node in a binary search tree */ void insert ( struct btreenode **sr, int num ) { if ( *sr == NULL ) { *sr = malloc ( sizeof ( struct btre...

Program to build a binary search tree from an array.

#include #include #include struct node { struct node *left ; char data ; struct node *right ; } ; struct node * buildtree ( int ) ; void inorder ( struct node * ) ; char a[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' } ; void main( ) { struct node *root ; clrscr( ) ; root = buildtree ( 0 ) ; printf ( "In-order Traversal:\n" ) ; inorder ( root ) ; getch( ) ; } struct node * buildtree ( int n ) { struct node *temp = NULL ; if ( a[n] != '\0' ) { temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; temp -> left = buildtree ( 2 * n + 1 ) ; temp -> data = a[n] ; temp -> right = buildtree ( 2 * n + 2 ) ; } return temp ; } void inorder ( struct node *root ) { if ( root != NULL ) { ...