binary search trees cse 331 section 2 james daly

21
Binary Search Trees CSE 331 Section 2 James Daly

Upload: alexander-may

Post on 16-Dec-2015

232 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Binary Search Trees CSE 331 Section 2 James Daly

Binary Search Trees

CSE 331Section 2James Daly

Page 2: Binary Search Trees CSE 331 Section 2 James Daly

Homework 1

• Homework 1 is due on today• Leave on the front table

Page 3: Binary Search Trees CSE 331 Section 2 James Daly

Review: SampleTrees

…T1 T2 Tk

Page 4: Binary Search Trees CSE 331 Section 2 James Daly

Review: Terminology

• Parent• Child• Ancestor• Descendent• Root• Leaf• Internal

A

B C

D

Page 5: Binary Search Trees CSE 331 Section 2 James Daly

Review: Terminology

• Path• Depth• Height

A

B C

D

Page 6: Binary Search Trees CSE 331 Section 2 James Daly

Review: Binary Tree

• Every node has at most 2 children• Left and right child

• Variation: n-ary trees have at most n children

Page 7: Binary Search Trees CSE 331 Section 2 James Daly

Binary Search Tree

• For every node• Left descendents smaller (l ≤ k)• Right descendents bigger (r ≥ k)

k

<k >k

Page 8: Binary Search Trees CSE 331 Section 2 James Daly

Traversal

• Three types• Pre-order

• Visit the parent before either of its children

• In-order• Visit the left children before the parent• Visit the right children after

• Post-order• Visit the children before the parent

Page 9: Binary Search Trees CSE 331 Section 2 James Daly

Eval Tree

*

+ +

a b c -

d 3

Pre-order: * +ab +c -d3In-order: a + b*c + d – 3Post-order: ab+ cd3- + *

Page 10: Binary Search Trees CSE 331 Section 2 James Daly

Search Tree

4

2 8

1 3 6 9

5 7

Page 11: Binary Search Trees CSE 331 Section 2 James Daly

Search Tree

• All keys in left subtree <= root• All keys in right subtree >= root• In-order traversal => non-decreasing list

• Tree-sort• O(n log n) build time for balanced trees• O(n) time to traverse tree

• Can define functions to find particular items or the largest or smallest item

Page 12: Binary Search Trees CSE 331 Section 2 James Daly

Find(t, x)

If (t = null)null

Else If (x < t.data)Find(t.left, x)

Else If (x > t.data)Find(t.right, x)

Elset

4

3

2

8

6 9

5 7

7?

Page 13: Binary Search Trees CSE 331 Section 2 James Daly

FindMin(t) [Recursive]

If (t.left != null)t

ElseFindMin(t.left)

Page 14: Binary Search Trees CSE 331 Section 2 James Daly

Insert(t, x)

If (t = null)t = new Node(x)

Else If (x < t.data)Insert(t.left, x)

Else If (x > t.data)Insert(t.right, x)

5

3 9

2

Construct a BST for 5, 3, 9, 2

Page 15: Binary Search Trees CSE 331 Section 2 James Daly

Delete: 1st Case

• Leaf Node

6

5 7

6

5

Page 16: Binary Search Trees CSE 331 Section 2 James Daly

Delete: 2nd Case

• One Child

6

7

88

7

Page 17: Binary Search Trees CSE 331 Section 2 James Daly

Delete: 3rd Case

• Two children• Swap with least successor (or greatest

predecessor)• Then delete from the right (or left) subtree

Page 18: Binary Search Trees CSE 331 Section 2 James Daly

Delete: 3rd Case

4

2 8

1 3 6 9

5 7

Page 19: Binary Search Trees CSE 331 Section 2 James Daly

Delete: 3rd Case

5

2 8

1 3 6 9

4 7

Page 20: Binary Search Trees CSE 331 Section 2 James Daly

Delete: 3rd Case

5

2 8

1 3 6 9

4 7

Page 21: Binary Search Trees CSE 331 Section 2 James Daly

Next Time

• Balanced binary search trees• AVL trees• Maybe Red-Black trees