CS 225 Data Structures Feb. 23 BST Rem emove Wad ade Fag - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Feb. 23 BST Rem emove Wad ade Fag - - PowerPoint PPT Presentation

CS 225 Data Structures Feb. 23 BST Rem emove Wad ade Fag agen-Ulm lmschneid ider template<typename K, typename V> 1 2 3 void BST::_insert(TreeNode *& root, K & key, V & value) { 4 TreeNode *t = _find(root, key);


slide-1
SLIDE 1

CS 225

Data Structures

  • Feb. 23 – BST Rem

emove

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2
slide-3
SLIDE 3

template<typename K, typename V> void BST::_insert(TreeNode *& root, K & key, V & value) { TreeNode *t = _find(root, key); t = new TreeNode(key, value); } 13 10 25 12 37 38 51 40 84 89 66 95 1 2 3 4 5 6

root_

slide-4
SLIDE 4

template<typename K, typename V> void BST::_insert(TreeNode *& root, K & key, V & value) { TreeNode *t = _find(root, key); t = new TreeNode(key, value); } 13 10 25 12 37 38 51 40 84 89 66 95 1 2 3 4 5 6

root_

slide-5
SLIDE 5

13 10 25 12 37 38 51 40 84 89 66 95

slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13
slide-14
SLIDE 14
slide-15
SLIDE 15

template<typename K, typename V> ________________________ _remove(TreeNode *& root, const K & key) { }

13 10 25 12 37 38 51 40 84 89 66 95

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

root

slide-16
SLIDE 16

13 10 25 12 37 38 51 40 84 89 66 95

remove(40);

slide-17
SLIDE 17

13 10 25 12 37 38 51 40 84 89 66 95

remove(25);

slide-18
SLIDE 18

13 10 25 12 37 38 51 40 84 89 66 95

remove(10);

slide-19
SLIDE 19

13 10 25 12 37 38 51 40 84 89 66 95

remove(13);

slide-20
SLIDE 20

BST Analysis – Running Time

Operation

BST Worst Case find insert delete traverse

slide-21
SLIDE 21

BST Analysis

Every operation that we have studied on a BST depends on the height of the tree: O(h). …what is this in terms of n, the amount of data? We need a relationship between h and n: f(n) ≤ h ≤ g(n)

slide-22
SLIDE 22

BST Analysis

Q: What is the maximum number of nodes in a tree of height h?

A X S 2

22

2 5

slide-23
SLIDE 23

BST Analysis

Q: What is the minimum number of nodes in a tree of height h? What is the maximum height for a tree

  • f n nodes?

A X S 2

22

2 5

slide-24
SLIDE 24

BST Analysis

Therefore, for all BST: Lower bound: Upper bound:

slide-25
SLIDE 25

BST Analysis

The height of a BST depends on the order in which the data is inserted into it. ex: 1 3 2 4 5 7 6 vs. 4 2 3 6 7 1 5 Q: How many different ways are there to insert keys into a BST? Q: What is the average height of all the arrangements?

slide-26
SLIDE 26

BST Analysis

Q: How many different ways are there to insert keys into a BST? Q: What is the average height of all the arrangements?

slide-27
SLIDE 27

BST Analysis – Running Time

Operation

BST Average case BST Worst case Sorted array Sorted List find insert delete traverse

slide-28
SLIDE 28

Height-Balanced Tree

What tree makes you happier? Height balance: b = height(TL) - height(TR) A tree is height balanced if:

9 5

7

7

5

9