cse373: data structures & algorithms lecture 6: binary search trees lauren milne summer 2015 1

Post on 24-Dec-2015

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

CSE373: Data Structures & Algorithms

Lecture 6: Binary Search Trees

Lauren MilneSummer 2015

2

Announcements

• HW2 due 10:59 PM Friday• Going to try to rearrange session times.

3

Previously on CSE 373

– Dictionary ADT• stores (key, value) pairs• find, insert, delete

– Trees• Terminology• Binary Trees

4

Reminder: Tree terminology

A

E

B

D F

C

G

IH

LJ MK N

Node / Vertex

Edges

Root

Leaves

Left subtreeRight subtree

5

Example Tree Calculations

A

E

B

D F

C

G

IH

LJ MK N

A

Recall: Height of a tree is the maximum number of edges from the root to a leaf.

Height = 0

A

B Height = 1

What is the height of this tree?

What is the depth of node G?

What is the depth of node L?

Depth = 2

Depth = 4

Height = 4

6

Binary Trees• Binary tree: Each node has at most 2 children (branching factor 2)

• Binary tree is• A root (with data)• A left subtree (may be empty) • A right subtree (may be empty)

• Special Cases

7

Tree TraversalsA traversal is an order for visiting all the nodes of a tree

• Pre-order: root, left subtree, right subtree

• In-order: left subtree, root, right subtree

• Post-order: left subtree, right subtree, root

+

*

2 4

5

(an expression tree)

+ * 2 4 5

2 * 4 + 5

2 4 * 5 +

Binary Search Tree (BST) Data Structure

4

121062

115

8

14

13

7 9

• Structure property (binary tree)– Each node has 2 children– Result: keeps operations simple

• Order property– All keys in left subtree smaller

than node’s key– All keys in right subtree larger

than node’s key– Result: easy to find any given key

8

A binary search tree is a type of binary tree (but not all binary trees are binary search trees!)

Are these BSTs?

3

1171

84

5

4

181062

115

8

20

21

7

15

9

Find in BST, Recursive

2092

155

12

307 1710

Data find(Key key, Node root){ if(root == null) return null; if(key < root.key) return find(key,root.left); if(key > root.key) return find(key,root.right); return root.data;}

10

Worst case running time is O(n).- Happens if the tree is very lopsided (e.g. list)

321 4

What is the running time?

Find in BST, Iterative

2092

155

12

307 1710

Data find(Key key, Node root){ while(root != null && root.key != key) { if(key < root.key) root = root.left; else(key > root.key) root = root.right; } if(root == null) return null; return root.data;}

11

Worst case running time is O(n).- Happens if the tree is very lopsided (e.g. list)

Bonus: Other BST “Finding” Operations

• FindMin: Find minimum node– Left-most node

• FindMax: Find maximum node– Right-most node 2092

155

12

307 1710

12

Insert in BST

2092

155

12

307 17

insert(13)insert(8)insert(31)

(New) insertions happen only at leaves – easy!10

8 31

13

13

Again… worst case running time is O(n), which may happen if the tree is not balanced.

Deletion in BST

2092

155

12

307 17

Why might deletion be harder than insertion?

10

14

Deletion in BST

• Basic idea: find the node to be removed, then “fix” the tree so that it is still a binary search tree

• Three potential cases to fix:– Node has no children (leaf)– Node has one child– Node has two children

15

Deletion – The Leaf Case

2092

155

12

307 17

delete(17)

10

16

Deletion – The One Child Case

2092

155

12

307 10

17

delete(15)

Deletion – The One Child Case

2092

5

12

307 10

18

delete(15)

Deletion – The Two Child Case

3092

205

12

7

What can we replace 5 with?

10

19

delete(5)

4

Deletion – The Two Child Case

What can we replace the node with?

Options:• successor minimum node from right subtree

findMin(node.right)

• predecessor maximum node from left subtree findMax(node.left)

20

Deletion: The Two Child Case (example)

21

3092

235

12

7 10

18

1915 3225

delete(23)

Deletion: The Two Child Case (example)

22

3092

235

12

7 10

18

1915 3225

delete(23)

Deletion: The Two Child Case (example)

23

3092

195

12

7 10

18

1915 3225

delete(23)

Deletion: The Two Child Case (example)

24

3092

195

12

7 10

18

15 3225

Success!

delete(23)

Lazy Deletion

• Lazy deletion can work well for a BST– Simpler– Can do “real deletions” later as a batch– Some inserts can just “undelete” a tree node

• But– Can waste space and slow down find operations– Make some operations more complicated:

• e.g., findMin and findMax?

25

BuildTree for BST• Let’s consider buildTree

– Insert all, starting from an empty tree

• Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST

– If inserted in given order, what is the tree?

– What big-O runtime for buildTree on this sorted input?

– Is inserting in the reverse order

any better?

1

2

3O(n2)

26

BuildTree for BST• Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST

• What we if could somehow re-arrange them– median first, then left median, right median, etc.– 5, 3, 7, 2, 1, 4, 8, 6, 9

– What tree does that give us?

– What big-O runtime?

– So the order the values

come in is important!

842

73

5

9

6

1

O(n log n), definitely better

27

top related