Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

AV 482 Data Structures and DBMS

Instructor: B. S. Manoj 26th August 2013


These slides are the works of many people who own the copyright for their work. You may use it for your class room purpose at IIST for AV 482 (Data Structures And DBMS) in July-December 2013 semester. Any other form of commercial or Non-commercial public distribution may require prior permissions in writing from the Copyright owners. AV 482 Data Structures and DBMS, JulyDecember 2013

Todays plan
General updates
Quiz-1 updates Topics till last class

Tree ADT
Binary Search Trees Algorithms for Binary Search Trees

AV 482 Data Structures and DBMS, JulyDecember 2013

Binary search trees


A binary tree where
Elements are ordered by linear order E.g., <, >, alphabetical, dictionary order etc Very effective in efficient search [O(log n)] Many applications
Distributed storage Distributed computing Algorithms for Efficient search

AV 482 Data Structures and DBMS, JulyDecember 2013

An example Binary Search Tree


45

24

89

19

32

65

91

13

21
AV 482 Data Structures and DBMS, JulyDecember 2013

Height of a full binary Tree


A binary tree with 2h leaves or nodes 2h+1-1: Full binary tree
N=2h+1-1 N+1 = 2h+1 Log (N+1) = h+1 H= Log (N+1)-1
2 2 1

1
3

h=O(Log N)
5 6 8 9

AV 482 Data Structures and DBMS, JulyDecember 2013

Balanced and Unbalanced Tree costs


Balanced tree
Worst case: O(log N) Average case: O(log N) Best case: O(1)

Skewed binary tree


Worst case: O(N) Average case: O(N/2)=O(N) Best case: O(1)
AV 482 Data Structures and DBMS, JulyDecember 2013

Binary search trees


Examples of Basic operations
INSERT(.) DELETE(.) MEMBER(.), SEARCH(.) or LOCATE(.) FINDMIN(.), DELETEMIN(.) etc

AV 482 Data Structures and DBMS, JulyDecember 2013

Search algorithm on a binary tree


Bool MEMBER(x elementtype, Tree T) {
if (T == NULL)
return FALSE;
45

Locate 21 Locate 64

else if (x == T->element)
return TRUE;

24

89

else if(x < T->element)


return(MEMBER(x, T->leftchild));

19

32

65

91

else
return(MEMBER(x, T->rightchild));

Return False
13 21

Time complexity: O(h: height of tree) Worst case: O(N) Average case: O(Log N) AV 482 Data Structures and DBMS, JulyDecember 2013

Insert algorithm on binary tree


INSERT(elementtype x, Tree T) {
if (T==NULL) {
T = malloc(sizeof(Tree)); T->element=x; T->leftchild = T->leftchild =NULL;

Insert: 25
45

Insert: 26 Insert: 90
89

24

} elseif(x < T->element)


INSERT(x,T->leftchild);

19

32

65

91

elseif(x > T->element)


INSERT(x, T->rightchild);

else
Printf(X is already in the Tree);

13

21

25 90

}
AV 482 Data Structures and DBMS, JulyDecember 2013

26

DELETE algorithm
DELETE(elementtype x, Tree T) {
if (T!=NULL){
If(x < T->element)
DELETE(x, T->leftchild) 45

Delete: 32 Delete: 13 Delete: 21 Delete: 89

elseif(x > T->element)


DELETE(x, T->rightchild)

24

89 90

elseif(T->leftchild == NULL AND T->rightchild == NULL)


T = NULL;

19

32

65

91

elseif(T->leftchild ==NULL)
T = T->rightchild;

elseif(T->rightchild == NULL
T = T->leftchild;

13

21

33

90

else
T->element = DELETEMIN(T>rightchild); 12

}}

AV 482 Data Structures and DBMS, JulyDecember 2013

DELETEMIN(.) algorithm
elementtype DELETEMIN(Tree T) {
if(T->leftchild == NULL)
{ temp=T->element; T=T->rightchild; return temp; }

else {
DELETEMIN(T->leftchild); }

}
AV 482 Data Structures and DBMS, JulyDecember 2013

19 19 24 32 45 89 24 32 45 89 89 45 89 45 32 24 19 24 19 32

45 45 24 19 32 89 19
AV 482 Data Structures and DBMS, JulyDecember 2013

24 32

89

Summary
Binary Trees
Search Trees Algorithms for Insert, delete, search etc.

AV 482 Data Structures and DBMS, JulyDecember 2013

You might also like