Title: Introduction to C Programming CE003121
1Introduction to C ProgrammingCE00312-1
- Lecture 24
- Insertion and Deletion
- with Binary Search Trees
2Insertion
- The newnode with the new item is created first,
ready for insertion. -
- Starting at the root, compare given newnode data
with current nodes data, and go left or right
recursively, and link in newnode when reaching a
leaf or a missing branch.
3Insertion into a Binary Search Tree
root of tree
fred
teddy
colin
nick
darryl
thomas
brian
node to insert
fong
rob
claude
a leaf
4createnode function
- Treepointer createnode(char item)
- // create a node for a string item
- Treepointer newpointer
- newpointer
- (Treepointer) malloc(sizeof(struct node))
- newpointer -gt left NULL
- newpointer -gt right NULL
- strcpy(newpointer -gt 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.
5void insert(Treepointer T, Treepointer new)
// insert new into subtree, T if (strcmp(new
-gt data, T -gt data) lt 0) // insert on the
left if (T -gt left ! NULL) insert(T
-gt left, new) else // link as new
left leaf T -gt left new else
// insert on the right if (T -gt right !
NULL) insert(T -gt right, new)
else // link as new right leaf T -gt right
new
6Growing 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
7grow function
- Treepointer grow(void)
-
- Treepointer root
- char item21
- 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
8Deletion of a leaf
root of tree
fred
teddy
colin
nick
darryl
thomas
brian
fong
rob
a leaf
Deletion of rob only involves returning NULL to
nicks right link.
9Deletion with a missing branch
root of tree
fred
teddy
colin
nick
darryl
thomas
brian
missing branch
fong
rob
Deletion of darryl only involves returning
right branch (to fong) to colins right link.
10Deletion of a node with two branches find
predecessor
fred
teddy
colin
nick
darryl
thomas
brian
fong
rob
First find the predecessor of target fred.
Go one left and then all the way right.
11Deletion of node with 2 branches using predecessor
fong
teddy
colin
nick
darryl
thomas
brian
fred
rob
Swap target fred with its predecessor
fong. Delete fred as a leaf!
12Result of deletion
fong
teddy
colin
nick
darryl
thomas
brian
rob
fong is has now replaced the deleted item,
fred.
13Design of delete function
Simple Cases CASE 1 LEAF Given
result
return NULL
14Deletion for a Single Branch Node
CASE 2a Left single branch Given
result return left branch
CASE 2b similarly for single right branch
15Deletion for a Double Branch Node
CASE 3 both Non-Null branches
P
Predecessor
L
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.
16P
Result
L
17Treepointer delete(Treepointer T, char
item) Treepointer pred // predecessor if
(T NULL) return NULL else if
(strcmp(item, T -gt data) lt 0) // go left
T -gt left delete(T -gt left, item)
return T else if (strcmp(item, T -gt
data) gt 0) // go right T -gt right
delete(T -gt right,item) return T
18 else // item data, // so delete node
at T if (T -gt left NULL) //
missing left branch // CASE 1 2b
return T -gt right // or leaf else
if (T -gt right NULL) // CASE 2a //
missing right branch return T -gt
left
19 else // CASE 3 find predecessor // go
left one node pred T -gt left while
(pred -gt right ! NULL) // go all the way
right pred pred -gt right // swap
with predecessor strcpy(T -gt data, pred -gt
data) strcpy(pred -gt data, item) //
delete item T -gt left delete(T -gt left,
item) return T // end predecessor
// end delete node at T // end delete