Definitions Binary Search Tree: n Tree: hierarchical structure made - - PDF document

definitions
SMART_READER_LITE
LIVE PREVIEW

Definitions Binary Search Tree: n Tree: hierarchical structure made - - PDF document

Advanced Programming Binary Search Tree Binary Search Tree Introduction Binary Search Tree, o BST implement dictionaries or ordered queues elements stored are ordered according to a key Efficient (log n) operations S EARCH , M INIMUM , M


slide-1
SLIDE 1

Advanced Programming Binary Search Tree 1

Binary Search Tree

2

Introduction

Binary Search Tree, o BST implement dictionaries or ordered queues – elements stored are ordered according to a key Efficient (log n) operations SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, DELETE.

slide-2
SLIDE 2

Advanced Programming Binary Search Tree 2

3

Definitions

Binary Search Tree:

n Tree: hierarchical structure made of nodes

with father-son relations. One node has no father (root) and is unique. The path from the root to any node is unique

n Binary: each node has at most 2 sons (left

e right) and only one father (p) – except the root that has no father

n Search: nodes have a key field and are

  • rdered according to it.

4

Ordering relations (I)

For each node x :

n For all nodes y in left subtree of x,

key[y]≤key[x]

n For all nodes y in right subtree of x,

key[y]≥key[x]

slide-3
SLIDE 3

Advanced Programming Binary Search Tree 3

5

Ordering relations (II)

x

≤ x ≥ x

6

Ex I

5 3 7 2 5 8

This tree is a BST

slide-4
SLIDE 4

Advanced Programming Binary Search Tree 4

7

Ex

5 3 7 2 5 8

BST

8

Ex

9 3 7 2 5 8

NO BST

slide-5
SLIDE 5

Advanced Programming Binary Search Tree 5

9

Balanced BST

A BST is balanced if, for each node, the height of its left subtree = the height of its right subtree +- 1

10

Height of a tree

The height of a tree is the number of levels below the root node. h = 0: only root node h = 1: root node and at least a child h = 2: root, children and grandchild … In a balanced tree: h = ⎣ log2(n) ⎦

slide-6
SLIDE 6

Advanced Programming Binary Search Tree 6

11

Ex

5 3 7 2 5 8

Balanced BST

12

Ex II

6 3 7 2 5 8

Not Balanced BST

slide-7
SLIDE 7

Advanced Programming Binary Search Tree 7

13

Complexity

Operations have complexity proportional to the depth h of the tree For a balanced tree (n nodes) complexity = Θ(log n) For a completely unbalanced tree, the worst case, complexity = O(n). Average case Θ(log n)

14

Log vs. Log2

Complexity proportional to depth of tree (h) Since h ~ log2(n) then complexity should be Θ( log2(n) ) But, remember that: So Θ( log2(n) ) ⟺ Θ( log(n) ) logb(x) logb(a) loga(x) =

constant

slide-8
SLIDE 8

Advanced Programming Binary Search Tree 8

15

Traversals

Going through all nodes can be done in 3 ways

n Preorder: node, subtree left, subtree right n Inorder: subtree left, node, subtree right n Postorder: subtree left, subtree right, node

16

Preorder

Preorder-Tree-Walk(x) 1 if x ≠ NIL 2 then print key[x] 3 Preorder-Tree-Walk(left[x]) 4 Preorder-Tree-Walk(right[x])

slide-9
SLIDE 9

Advanced Programming Binary Search Tree 9

17

Inorder

Inorder-Tree-Walk(x) 1 if x ≠ NIL 2 then Inorder-Tree-Walk(left[x]) 3 print key[x] 4 Inorder-Tree-Walk(right[x])

18

Postorder

Postorder-Tree-Walk(x) 1 if x ≠ NIL 2 then Postorder-Tree-Walk(left[x]) 3 Postorder-Tree-Walk(right[x]) 4 print key[x]

slide-10
SLIDE 10

Advanced Programming Binary Search Tree 10

19

Notes

Inorder trasversal (see Inorder-Tree- Walk(root[T])) returns the elements in ascending order of key All traversal are Θ(n) (all nodes are passed

  • nce)

20

  • Ex. Trasversals?

15 6 18 17 20 3 7 2 4 13 9

slide-11
SLIDE 11

Advanced Programming Binary Search Tree 11

21

Inorder

15 6 18 17 20 3 7 2 4 13 9 1 2 3 4 5 6 7 8 9 10 11

22

Preorder

15 6 18 17 20 3 7 2 4 13 9 1 2 3 4 5 6 7 8 9 10 11

slide-12
SLIDE 12

Advanced Programming Binary Search Tree 12

23

Postorder

15 6 18 17 20 3 7 2 4 13 9 1 2 3 4 5 6 7 8 9 10 11

24

Search operations

Search, Minimum/Maximum, Predecessor/ Successor. Are all O(h)

slide-13
SLIDE 13

Advanced Programming Binary Search Tree 13

25

Tree-Search

Tree-Search(x, k) 1 if x = NIL or k = key[x] 2 then return x 3 if k < key[x] 4 then return Tree-Search(left[x], k) 5 else return Tree-Search(right[x], k)

26

Ex

15 6 18 17 20 3 7 2 4 13 9 Tree-Search(13)

slide-14
SLIDE 14

Advanced Programming Binary Search Tree 14

27

Tree-Search (iterative)

Tree-Search-iterative(x, k) 1 while x ≠ NIL and k ≠ key[x] 2 do if k < key[x] 3 then x ← left[x] 4 else x ← right[x] 5 return x

28

Ex

15 6 18 17 20 3 7 2 4 13 9 Tree-Search-iterative(13)

slide-15
SLIDE 15

Advanced Programming Binary Search Tree 15

29

Min / Max (iterative)

Tree-Minimum(x) 1 while left[x] ≠ NIL 2 do x ← left[x] 3 return x Tree-Maximum(x) 1 while right[x] ≠ NIL 2 do x ← right[x] 3 return x

15 6 18 17 20 3 7 2 4 13 9 15 6 18 17 20 3 7 2 4 13 9 15 6 18 17 20 3 7 2 4 13 9 15 6 18 17 20 3 7 2 4 13 9

30

Successor

Given a node, find the closest - 2 cases:

x p[x] x p[x] Min of right subtree First ancestor, of which the node is left heir

slide-16
SLIDE 16

Advanced Programming Binary Search Tree 16

31

Successor

Tree-Successor(x) 1 if right[x] ≠ NIL 2 then return Tree-Minimum(right[x]) 3 y ← p[x] 4 while y ≠ NIL and x = right[y] 5 do x ← y 6 y ← p[y] 7 return y

32

Ex

15 6 18 17 20 3 7 2 4 13 9

Tree-Successor(7)

slide-17
SLIDE 17

Advanced Programming Binary Search Tree 17

33

Ex

15 6 18 17 20 3 7 2 4 13 9

Tree-Successor(7)

34

Ex

15 6 18 17 20 3 7 2 4 13 9

Tree-Successor(4)

slide-18
SLIDE 18

Advanced Programming Binary Search Tree 18

35

Ex

15 6 18 17 20 3 7 2 4 13 9

Tree-Successor(4)

36

Ex

15 6 18 17 20 3 7 2 4 13 9

Tree-Successor(17)

slide-19
SLIDE 19

Advanced Programming Binary Search Tree 19

37

Ex

15 6 18 17 20 3 7 2 4 13 9

Tree-Successor(4)

38

Predecessor

Tree-Predecessor(x) 1 if left[x] ≠ NIL 2 then return Tree-Maximum(left[x]) 3 y ← p[x] 4 while y ≠ NIL and x = left[y] 5 do x ← y 6 y ← p[y] 7 return y

slide-20
SLIDE 20

Advanced Programming Binary Search Tree 20

39

Insert Delete

Must maintain the BST properties.

40

Insert

To insert node z with key v:

n Create node z with left[z]=right[z]=NIL n Search key[z] n Set pointers

The new node is always a leaf

slide-21
SLIDE 21

Advanced Programming Binary Search Tree 21

41

Tree-Insert (I)

Tree-Insert(T, z) 1 y ← NIL 2 x ← root[T] 3 while x ≠ NIL 4 do y ← x 5 if key[z]<key[x] 6 then x ← left[x] 7 else x ← right[x] search key[z]

y z x=NIL

42

Tree-Insert (II)

8 p[z] ← y 9 if y = NIL 10 then root[T] ← z 11 else if key[z] < key[y] 12 then left[y] ← z 13 else right[y] ← z

y z x=NIL

insert z as son

  • f y
slide-22
SLIDE 22

Advanced Programming Binary Search Tree 22

43

Ex

12 5 18 15 20 2 9 17 13

Tree-Insert(13)

z

44

Ex

12 5 18 15 20 2 9 17

Tree-Insert(13)

13 z y x 13

slide-23
SLIDE 23

Advanced Programming Binary Search Tree 23

45

Ex

12 5 18 15 20 2 9 17

Tree-Insert(13)

z y 13

46

Delete

Three cases, zero, 1, 2 sons

slide-24
SLIDE 24

Advanced Programming Binary Search Tree 24

47

Delete: 0 sons

12 15 5 16 3 12 20 10 13 6 7 18 23 12 15 5 16 3 12 20 10 18 23 z 6 7 Just cancel z

48

Delete: 1 son

12 15 5 16 3 12 20 10 13 18 23 12 15 5 3 12 20 10 18 23 z 6 7 6 7 Son(z) becomes new son of father(z) 13

slide-25
SLIDE 25

Advanced Programming Binary Search Tree 25

49

Delete: 2 sons

12 15 5 16 3 12 20 10 13 18 23 12 15 5 16 3 12 20 10 18 23 z Copy succ (z) in position of z z 6 7 6 7 13

50

Tree-Delete (I)

Tree-Delete(T, z) 1 if left[z]=NIL or right[z]=NIL 2 then y ← z 3 else y ← Tree-Successor(z) 4 if left[y] ≠ NIL 5 then x ← left[y] 6 else x ← right[y]

y: to be canceled x: unique son of y 12 y x

slide-26
SLIDE 26

Advanced Programming Binary Search Tree 26

51

Tree-Delete (II)

7 if x ≠ NIL 8 then p[x] ← p[y] 9 if p[y] = NIL 10 then root[T] = x 11 else if y = left[p[y]] 12 then left[p[y]] ← x 13 else right[p[y]] ← x

  • therwise, link x to

father(y) Update father(x) Y is root? x becomes root 12 y x

52

Tree-Delete (III)

14 if y ≠ z 15 then key[z] ← key[y] 16 fields[z] ← fields[y] 17 return y

If needed, copy info of successor in node to be canceled

slide-27
SLIDE 27

Advanced Programming Binary Search Tree 27

53

Complexity

For insert and cancel: O(h) If tree is balanced: O(log(n))

54

Balancing

The insert/cancel proposed do not guarantee to maintain the tree balanced. There are variants of BST that maintain the tree balanced when inserting and deleting nodes: Red-Black tree AA tree

slide-28
SLIDE 28

Advanced Programming Binary Search Tree 28

55

Ex

Build a BST with numbers 0 to 9

n Define an insert sequence that mantains

the tree balanced

n Define an insert sequence that mantains

the tree unbalanced

56

Solution (I)

6 3 8 1 5 7 9 2 4 Insert(): 6, 3, 1, 0, 2, 5, 4, 8, 7, 9

slide-29
SLIDE 29

Advanced Programming Binary Search Tree 29

57

Solution (II)

9 Insert() : 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 8 7 6 5 4 3 2 1

58

C: node

typedef struct btnode *P_NODE; typedef struct btnode{ int key; P_NODE left, right; }NODE;

slide-30
SLIDE 30

Advanced Programming Binary Search Tree 30

59

inorder

void inorder( P_NODE head) { if( head == NULL) return; inorder( head->left); printf( "%d ", head->key); inorder( head->right); }

60

search

P_NODE search( int key, P_NODE head) { if((head == NULL)!!(head->key == key)) return( head); else if( head->key < key) return head->right; else return head->left; }

slide-31
SLIDE 31

Advanced Programming Binary Search Tree 31

61

insert (I)

int insert( int key, P_NODE *phead) { if( *phead == NULL) { if((*phead=(P_NODE)malloc (sizeof(NODE)))== NULL) return( 0); else { (*phead)->key = key; (*phead)->left = NULL; (*phead)->right = NULL; return( 1); } }

62

insert (II)

else if( (*phead)->key == key) { printf("Element already present\n"); return( 0); } else if( (*phead)->key < key) return( insert( key, &((*phead)->right))); else return( insert( key, &((*phead)->left))); }

slide-32
SLIDE 32

Advanced Programming Binary Search Tree 32

63

Main

P_NODE head=NULL; … if( !insert( key, &head)) printf( ”Insert error!\n"); … if( search( key, head)) printf( ”Found\n"); else printf( ”Not found\n"); … inorder( head);