binary search trees - github pages · 2018-06-10 · binary search tree – what is it? 42 32 12 45...

24
BINARY SEARCH TREES Problem Solving with Computers-II

Upload: others

Post on 19-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

BINARY SEARCH TREES

Problem Solving with Computers-II

Page 2: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Sorted arrays, Balanced Binary Search Trees

Operations Sorted Array Balanced BSTMinMaxSuccessorPredecessorSearchInsert DeletePrint elements in order

Page 3: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Trees

3

A tree has following general properties: • One node is distinguished as a root; • Every node (exclude a root) is connected

by a directed edge from exactly one other node;

A direction is: parent -> children

Page 4: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Which of the following is/are a tree?

A. B.

C.

D. A & B

E. All of A-C

4

Page 5: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Binary Search Tree – What is it?

42

32

12

45

41 50

5

Do the keys have to be integers?

• Each node: • stores a key (k) • has a pointer to left child, right child

and parent (optional) • Satisfies the Search Tree Property

Page 6: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

class BSTNode {

public: BSTNode* left; BSTNode* right; BSTNode* parent; int const data;

BSTNode( const int & d ) : data(d) { left = right = parent = 0; } };

6

A node in a BST

Page 7: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Which of the following is/are a binary search tree?

42

32

12

42

3212

42

3212 65

30 38

A. B.

42

32

12

56

45

D.

C.

E. More than one of these

Page 8: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

BSTs allow efficient search!

42

32

12

45

41 50

8

• Start at the root; trace down a path by comparing k with the key of the current node x:

• If the keys are equal: we have found the key

• If k < key[x] search in the left subtree of x

• If k > key[x] search in the right subtree of x

Search for 41, then search for 53

Page 9: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

9

SearchHow many edges need to be traversed to search for 40? A. One B. Two C. Three D. Four

42

32

12

45

41 50

Page 10: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

How fast is the BST search algorithm?

10

Many different BSTs are possible for the same set of keys Examples for keys: 12, 32, 41, 42, 45

Page 11: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

How fast is BST search algorithm?

42

32

12

45

41

11

The complexity of search depends on the HEIGHT of the BST!

Height of a node: the height of a node is the number of edges on the longest path from the node to a leaf Height of a tree: the height of the root of the tree Height of this tree is 2.

Page 12: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Worst case analysisAre binary search trees really faster than linked lists for finding elements? • A. Yes • B. No

data:next:

1 data:next:

2 data:next:

3

12

Page 13: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Balanced BSTs

42

32

12

45

41 50

13

Page 14: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

14

Insert• Insert 40 • Search for the key • Insert at the spot you expected to find it

42

32

12

45

41 50

Page 15: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

15

3

2 4

6

7

13

15

18

17 20

9

Maximum = 20

MaxGoal: find the maximum value in a BST Following right child pointers from the root, until a leaf node is encountered

Alg: int BST::max()

Page 16: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

16

Predecessor: Next smallest element42

32

23

4520

50

• What is the predecessor of 32? • What is the predecessor of 45?

Page 17: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

17

Successor: Next largest element42

32

23

4520

50

• What is the successor of 45? • What is the successor of 48? • What is the successor of 60?

80

70

60

90

48

Page 18: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

18

Delete: Case 1: Node is a leaf node42

32

23

4520

50

• Set parent’s appropriate child pointer to null • Delete the node

80

70

60

90

48

Page 19: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

19

Delete: Case 2 Node has only one child42

32

23

4520

50

• Replace the node by its only child

80

70

60

90

48

Page 20: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

20

Delete: Case 3 Node has two children42

32

23

4520

50

• Can we still replace the node by one of its children? Why or Why not?

80

70

60

90

48

Page 21: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Completely filled BSTs

21

Page 22: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Relating H (height) and N (#nodes)find is O(H), we want to find a f(N) = H

Level 0

Level 1

Level 2

……

How many nodes are on level L in a completely filled binary search tree? A.2 B.L C.2*L D.2L

22

Page 23: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Relating H (height) and N (#nodes)find is O(H), we want to find a f(N) = H

Level 0

Level 1

Level 2

……Finally, what is the height (exactly) of the tree in terms of N?

And since we knew finding a node was O(H), we now know it is O(log2 N)

23

Page 24: BINARY SEARCH TREES - GitHub Pages · 2018-06-10 · Binary Search Tree – What is it? 42 32 12 45 41 50 5 Do the keys have to be integers? • Each node: • stores a key (k) •

Sorted arrays, linked-lists, Balanced Binary Search Trees

Operations Sorted Array Balanced BST Linked listMinMaxSuccessorPredecessorSearchInsert DeletePrint elements in order