Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 24 Insertion and Deletion with Binary Search Trees.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 24 Insertion and Deletion with Binary Search Trees."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 24 Insertion and Deletion with Binary Search Trees

2 Insertion The newnode with the new item is created first, ready for insertion. Starting at the root, compare given newnode data with current node’s data, and go left or right recursively, and link in newnode when reaching a leaf or a missing branch.

3 Insertion into a Binary Search Tree teddy fred nick darryl fong thomas rob brian colin claude node to insert a leaf root of tree

4 createnode function Treepointer createnode(char item[]) { // create a node for a string item Treepointer newpointer; newpointer = (Treepointer) malloc(sizeof(struct node)); newpointer -> left = NULL; newpointer -> right= NULL; strcpy(newpointer -> data, item); return newpointer; } The new node will become a leaf, hence the 2 null branches, and a pointer to the new node is returned.

5 void insert(Treepointer T, Treepointer new) { //insert new into subtree, T if (strcmp(new -> data, T -> data) < 0) {// insert on the left if (T -> left != NULL) { insert(T -> left, new); } else// link as new left leaf { T -> left = new; } else {// insert on the right if (T -> right != NULL) { insert(T -> right, new); } else// link as new right leaf { T -> right = new; } }

6 Growing a tree The entire tree can be grown as a series of insertions, starting with an empty tree. An inorder traversal of the entire tree produces the strings in alphabetic order – a tree sort! #include "tree.h" int main(void) { inorder(grow()); // traverse grown tree return 0; }

7 grow function Treepointer grow(void) { Treepointer root; char item[21]; scanf("%s", item);// 1st item root = createnode(item); while (scanf("%s", item) != EOF) { // read item and insert while not EOF insert(root, createnode(item)); } return root;// root of grown tree }

8 Deletion of a leaf teddy fred nick darryl fong thomas rob brian colin a leaf root of tree Deletion of “rob” only involves returning NULL to nick’s right link.

9 Deletion with a missing branch teddy fred nick darryl fong thomas rob brian colin root of tree missing branch Deletion of “darryl” only involves returning right branch (to fong) to colin’s right link.

10 Deletion of a node with two branches – find predecessor teddy fred nick darryl fong thomas rob brian colin First find the predecessor of target “fred”. Go one left and then all the way right.

11 Deletion of node with 2 branches using predecessor teddy fong nick darryl fred thomas rob brian colin Swap target “fred” with its predecessor “fong”. Delete “fred” as a leaf!

12 Result of deletion teddy fong nick darrylthomas rob brian colin “fong” is has now replaced the deleted item, “fred”.

13 Design of delete function Simple Cases CASE 1 LEAF Given result return NULL

14 Deletion for a Single Branch Node result return left branch CASE 2a Left single branch Given CASE 2b similarly for single right branch

15 Deletion for a Double Branch Node P L Predecessor Swap with Predecessor node(P). Delete as left single branch by linking in left branch(L). For Predecessor, go one left and all the way right. CASE 3 both Non-Null branches

16 P L Result

17 Treepointer delete(Treepointer T, char item[]) { Treepointer pred;// predecessor if (T == NULL) {return NULL; } else if (strcmp(item, T -> data) < 0) {//go left T -> left = delete(T -> left, item); return T; } else if (strcmp(item, T -> data) > 0) {//go right T -> right = delete(T -> right,item); return T; }

18 else // item == data, // so delete node at T { if (T -> left == NULL) {// missing left branch // CASE 1 & 2b return T -> right; // or leaf } else if (T -> right== NULL) // CASE 2a {// missing right branch return T -> left; }

19 else// CASE 3 find predecessor {// go left one node pred = T -> left; while (pred -> right != NULL) {// go all the way right pred = pred -> right; } // swap with predecessor strcpy(T -> data, pred -> data); strcpy(pred -> data, item); // delete item T -> left = delete(T -> left, item); return T; } // end predecessor }// end delete node at T } // end delete


Download ppt "Introduction to C Programming CE00312-1 Lecture 24 Insertion and Deletion with Binary Search Trees."

Similar presentations


Ads by Google