58
DATA STRUCTURES AND ALGORITHMS MODULE 4 Binary search trees BST insertion

2sharon4-2

  • Upload
    robinpt

  • View
    213

  • Download
    0

Embed Size (px)

DESCRIPTION

2sharon4-2

Citation preview

DATA STRUCTURES AND ALGORITHMS MODULE 4

DATA STRUCTURES AND ALGORITHMSMODULE 4Binary search treesBST insertion

BINARY SEARCH TREEA Binary search tree is a binary tree-> It may be empty-> If it is not empty, then it satisfies the following properties:Every element has a key and no two elements have the same key (no duplicates)The keys (if any) in the left subtree are smaller than the key in the rootThe keys (if any) in the right subtree are larger than the key in the rootThe left and right subtrees are also binary search trees

Eg: BINARY SEARCH TREE7root2143859Construction of binary search treeInsert 7,2,1,4,3,8,5,97rootConstruction of binary search treeInsert 7,2,1,4,3,8,5,97root2Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Insert 3Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Insert 3Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Insert 3Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Insert 3Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Insert 3Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214Insert 3Construction of binary search treeInsert 7,2,1,4,3,8,5,97root2143Insert 3Construction of binary search treeInsert 7,2,1,4,3,8,5,97root2143Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21438Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21438Insert 5Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21438Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21438Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21438Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21438Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root21438Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214385Insert 4Construction of binary search treeInsert 7,2,1,4,3,8,5,97root214385Construction of binary search treeInsert 7,2,1,4,3,8,5,97root2143859Construct a binary search treeBST : INSERT(5)Newnode=GETSPACE(newnode)Newnode->data=itemNewnode->lchild=NULLNewnode->rchild=NULL

X 5 XRoot =NULLnewnodeBST : INSERT(5)If(root=NULL)Root=newnode

X 5 XRoot =NULLnewnodeRoot BST : INSERT(2)Newnode=GETSPACE(newnode)Newnode->data=itemNewnode->lchild=NULLNewnode->rchild=NULL

X 5 XnewnodeRoot X 2 XBST : INSERT(2)If(root !=NULL)ptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchildElseptr=ptr->lchildEndIfEndWhile

X 5 XnewnodeRoot X 2 Xptr BST : INSERT(2)If(root !=NULL)ptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchildElseptr=ptr->lchildEndIfEndWhile

X 5 XnewnodeRoot X 2 Xptr parentBST : INSERT(2)If(root !=NULL)ptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchildElseptr=ptr->lchildEndIfEndWhile

X 5 XnewnodeRoot X 2 Xptr= NULL parentBST : INSERT(2)If(item>parent->data)Parent->rchild=newnodeElseParent->lchild=newnodeEndIf

5 XnewnodeRoot X 2 Xptr= NULL parentBST : INSERT(7)Newnode=GETSPACE(newnode)Newnode->data=itemNewnode->lchild=NULLNewnode->rchild=NULL

5 XnewnodeRoot X 2 XX 7 XBST : INSERT(7)If(root !=NULL)ptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchildElseptr=ptr->lchildEndIfEndWhile

5 XnewnodeRoot X 2 XX 7 XptrparentBST : INSERT(7)If(root !=NULL)ptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchildElseptr=ptr->lchildEndIfEndWhile

5 XnewnodeRoot X 2 XX 7 Xptr= NULLparentBST : INSERT(7)If(item>parent->data)Parent->rchild=newnodeElseParent->lchild=newnodeEndIf

5 newnodeRoot X 2 XX 7 XptrparentInsert an element into a binary search treeBST : Algorithm10Root6133X 8 XX 12 XX 1 XX 5 XX 15 XBST : insert (9)Where to insert 9?10Root6133X 12 XX 1 XX 5 XX 15 XX 9 XX 8 XBST : insert (9)Where to insert 9?

10Root6133 X 8 X 12 XX 1 XX 5 XX 15 XX 9 XnewnodeparentBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 XNewnode=GETSPACE(newnode)Newnode->data=itemNewnode->lchild=NULLNewnode->rchild=NULLX 9 XnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 XIf(root=NULL)Root=newnode

X 9 XnewnodeBST : insert (9)Find the position10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchildElse If(itemdata)ptr=ptr->lchild Else exitEndIfEndWhileX 9 XnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchild Else If(itemdata)ptr=ptr->lchild Else exitEndIfendwhileX 9 XptrnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchild Else If(itemdata)ptr=ptr->lchild Else exitEndIfendwhileX 9 XptrparentnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchild Else If(itemdata)ptr=ptr->lchild Else exitEndIfendwhileX 9 XptrparentptrnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchild Else If(itemdata)ptr=ptr->lchild Else exitEndIfendwhileX 9 XparentptrparentnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchild Else If(itemdata)ptr=ptr->lchild Else exitEndIfendwhileX 9 XptrparentptrnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchild Else If(itemdata)ptr=ptr->lchild Else exitEndIfendwhileX 9 XparentptrparentnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 Xptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchild Else If(itemdata)ptr=ptr->lchild Else exitEndIfendwhileX 9 Xparentptr=NULLnewnodeBST : insert (9)10Root6133X 8 XX 12 XX 1 XX 5 XX 15 XIf(item>parent->data)Parent->rchild=newnodeElseParent->lchild=newnodeEndIfX 9 Xparentptr=NULLnewnodeBST : 9 inserted10Root6133 X 8 X 12 XX 1 XX 5 XX 15 XX 9 XBST : INSERTIONAlgorithm BST_INSERT(item)Input: Item is the data component of a node that has to be inserted->Output: Item inserted in to the proper place of the tree->Data structure: Linked structureSteps:Newnode=GETSPACE(newnode)Newnode->data=itemNewnode->lchild=NULLNewnode->rchild=NULLIf(root=NULL)Root=newnode

Elseptr=rootWhile(ptr!=NULL)Parent=ptrIf(item>ptr->data)ptr=ptr->rchildElse If(itemdata)ptr=ptr->lchildElsePrint(duplicate node);exitEndIfEndWhileIf(item>parent->data)Parent->rchild=newnodeElseParent->lchild=newnodeEndIfEndIfStop

58