introduction to algorithms and data structures lesson 8 ...uehara/course/2019/myanmar/pdf/08.pdf ·...

21
Introduction to Algorithms and Data Structures Lesson 8: Data Structure (2) Operations on linked lists, and Binary Search Tree Professor Ryuhei Uehara, School of Information Science, JAIST, Japan. [email protected] http://www.jaist.ac.jp/~uehara 1

Upload: others

Post on 28-Oct-2019

28 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to Algorithms and Data Structures

Lesson 8: Data Structure (2)Operations on linked lists,

and Binary Search Tree Professor Ryuhei Uehara,

School of Information Science, JAIST, [email protected]

http://www.jaist.ac.jp/~uehara1

Example of Data structures × Algorithms

• Usually, we can choose some data structure, e.g.,• array• linked listfor the implementation of the same algorithm.

• Efficiency depends on “data structure” vs “basic operations” you will use on the data.• When we “add” and “remove” data, linked list is much

better than array, and tree structure is much better than linked list (I’ll explain, say, at the last lesson?)

• We will show some simple examples

typedef struct{double data;struct list_t *next;

} list_t;p = head;while(p != NULL && p->data != x)

p = p->next;return p;

Sequential search by linked list

• Find x in the linked list from the top of linked list– It contains x address of the record– It doesn’t contain x NULL

Is this correct?

Satisfied?

YES

YES

p==NULL or p->data == x it exits

Binary search method

2 5 6 19 33 54 67 72 78

• Search, divide into halves, and repeat to find

– Key issue: Divide at the center point.

2 5 6 19 54 67 72 78

2 5 78

Find 5 Less than 33

Less than 6

Found!

Find 78Greater than 33

Greater than 72

Found!

Binary search tree:data structure of binary search

• When data size is fixed, we can compute the central positions beforehand

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14s 10 12 18 28 30 38 40 45 47 49 53 67 70 75 82

745

3 1128 67

1 5 9 1312 38 49 75

0 2 4 6 8 10 12 1410 18 30 40 47 53 70 82

745

3 1128 67

1 5 9 1312 38 49 75

0 2 4 6 8 10 12 1410 18 30 40 47 53 70 82

Left subtree:All element is less than 45

Right subtree:All elements are greater than 45

Property of binary search tree

• In general, for a node n,– All elements in right subtree are greater than (or equal

to) n– All elements in left subtree are less than (or equal to) n

Each record has two pointers to left child and right child.

Search in binary search tree

BSTnode *root, *v;x=/*some value*/;v = root;while( v ){

if(v->data == x)break;

if(v->data > x)v = v->lson;

elsev = v->rson;

}return v;

typedef struct{int data;struct BSTnode

*lson, *rson;}BSTnode;

Left if small

Right if large

Consider: binary search tree• On a binary search tree, it holds for each vertex v;

– data in v > each data in left subtree of v– data in v < each data in right subtree of v

25

12 29

7 20 42

3 9 15

17

35

32 37

8

Search in binary search tree: case v=15

25

12 29

7 20 42

3 9 15

17

35

32 37

9

Search in binary search tree: case v=34

25

12 29

7 20 42

3 9 15

17

35

32 37

10

• Perform binary search from the root• If it reach to the leaf, store data on it

insert(x,tree){v root(tree);while(v is not a leaf){

if( x <= data(v) ) then v left child of v;

else v right child of v;

}make a node v at the leaf;data(v) x;

}

Add a data to binary search tree

11

Add a data to binary search treeExample: add x=34

25

12 29

7 20 42

3 9 15

17

35

32 37

12

Add a data to binary search treeExample: add x=34

25

12 29

7 20 42

3 9 15

17

35

32 37

34

13

void insert(tree *p, int x){ if(p == NULL){

p = (tree*) malloc( sizeof(tree) );p->key = x;p->lchild =NULL; p->rchild = NULL;

}else if( p->key < x )

insert( p->rchild, x);else

insert( p->lchild, x);}

Add a data to binary search tree (cnt’d)

How to call: insert(root,x)14

Remove a data to binary search tree :find a vertex of data x, and remove it!

• Case analysis based on the vertex v that has data x– Case 0. v has two leaves;

• This is easy; just remove v!

– Case 1. v has one leaf

– Case 2. v has no leaves

x

A

B

15

Remove a data to binary search tree:Case 1. v has one leaf

(1a) v is left child of parent p: update the left child of p by the nonempty child of v

x

A

B

AB

16

Remove a data to binary search tree:Case 1. v has one leaf

(1b) v is right child of parent p: update the rightchild of p by the nonempty child of v

x

A

B

AB

17

Remove a data to binary search tree :Case 2. v has no leaves

• Let u be the left child of v.

• Find the vertex w that has the maximum value w in the subtree rooted at u.– Right child of w should

be a leaf• Value y in w is copied

to v, and remove w.– As same as case 1

x

A

y

y

A18

Remove a data to binary search tree :Remove x=25

25

12 29

7 20 42

3 9 15

17

35

32 37

19

Remove a data to binary search tree :Remove x=25

20

12 29

7 20 42

3 9 15

17

35

32 37

20

Some comments

• The shape of binary search tree depends on– Initial sequence of data– Ordering of adding/removing data

• So, it may be a quite unbalanced tree if these ordering is not good…– If you can hope that it is “random”, the expected level

of tree is O(log n).– If you may have quite unbalanced data, the level can

be Θ(n). (In this case, it is almost the same as a linked list.)