binary search trees - stanford university · binary search tree designed to keep the height of the...

123
Binary Search Trees

Upload: others

Post on 05-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Binary Search Trees

Page 2: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Friday Four Square!4:15PM, Outside Gates

Page 3: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Implementing Set

● On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively.

● Let's now turn our attention to the Set.

● Major operations:● Insert● Remove● Contains

Page 4: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

An Inefficient Implementation

● We could implement the Set as a list of all the values it contains.

● To add an element:

● Check if the element already exists.

● If not, append it.

● To remove an element:

● Find and remove it from the list.

● To see if an element exists:

● Search the list for the element.

Page 5: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Using Hashing

● If we have a hash function for the elements being stored, we can implement a Set using a hash table.

● What is the expected time to insert a value?

● Answer: O(1).

● What is the expected time to remove a value?

● Answer: O(1).

● What is the expected time to check if a value exists?

● Answer: O(1).

● However, writing a good hash function for a set of elements can be very tricky!

Page 6: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Using Tries

● If our keys are strings, we can store the set using a trie.

● Looking up or inserting a string with L letters takes time O(L).

● Doesn't work for arbitrary values.

Page 7: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

An Entirely Different Approach

Page 8: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

4

3

0

-2

6

Page 9: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

4

3

0

-2

6

Page 10: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

4

3

0

-2

6

Page 11: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

4

3

0

-2

6

Page 12: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

40

-2

6

3

Page 13: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

40

-2

6

3

Page 14: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

40

-2

6

3

Page 15: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

40

-2

6

3

Page 16: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

40

-2

6

-1

3

Page 17: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

40-2 6

-1

3

Page 18: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

40-2 6

-1

3

Page 19: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

40-2 6

-1

3

Page 20: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

40-2 6

-1

3

Page 21: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

4

3

0-2 6

-1

Page 22: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

3

0-2 6

-1 4

Page 23: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

30-2 6

-1 4

Page 24: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

30-2 6

-1 4

Page 25: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

30-2 6

-1 4

Page 26: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

22

30-2 6

-1 4

30-2 6

-1 4

Page 27: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

4

22

30-2 6

-1 4

30-2 6

-1

Page 28: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

4

3

4

22

30-2 6

-1

0-2 6

-1

Page 29: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

3

4

3

4

22

0-2 6

-1

0-2 6

-1

Page 30: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

3

4

3

4

2

0-2 6

-1

0-2 6

-1

Page 31: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

2

3

4

3

4

0-2 6

-1

0-2 6

Page 32: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

-1

-2

2

-1

2

3

4

3

4

0-2 60 6

Page 33: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

-2

-1

-2

2

-1

2

3

4

3

4

0 60 6

Page 34: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Binary Search Trees

● The data structure we have just seen is called a binary search tree (or BST).

● Uses comparisons between elements to store elements efficiently.

● No need for a complex hash function, or the ability to work one symbol at a time.

Page 35: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

Page 36: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

Page 37: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Page 38: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Page 39: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Page 40: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Values less than two Values greater than two

Page 41: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Values less than two Values greater than two

Page 42: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Page 43: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Values lessthan -1

Values greaterthan -1

Page 44: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Values lessthan -1

Values greaterthan -1

Page 45: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

0

Page 46: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Tree Terminology

● As with a trie, a BST is a collection of nodes.

● The top node is called the root node.● Nodes with no children are called

leaves.2

-1 4

-2 0 63

Page 47: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

A Recursive View of BSTs

2

-1 4

-2 0 63

Page 48: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

A Recursive View of BSTs

2

-1 4

-2 0 63

Page 49: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

A Recursive View of BSTs

Page 50: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Implementing Lookups

Page 51: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Inserting into a BST

2

30-2 6

-1 4

Page 52: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

30-2 6

-1 4

Inserting into a BST

2

30-2 6

-1 4

1

Page 53: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

22

30-2 6

-1 4

Inserting into a BST

30-2 6

-1 4

1

Page 54: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

2

-1

2

30-2 6

-1 4

Inserting into a BST

30-2 6

4

1

Page 55: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

-1

0

2

-1

2

30-2 6

4

Inserting into a BST

3-2 6

4

1

Page 56: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

0

-1

0

2

-1

2

3-2 6

4

Inserting into a BST

3-2 6

4

1

Page 57: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Let's Code it Up!

Page 58: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Insertion Order Matters

● Suppose we create a BST of numbers in this order:

4, 2, 1, 3, 6, 5, 7

4

2

1 3

6

5 7

Page 59: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Insertion Order Matters

● Suppose we create a BST of numbers in this order:

1, 2, 3, 4, 5, 6, 7

12

34

56

7

Page 60: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Tree Terminology

● The height of a tree is the number of nodes in the longest path from the root to a leaf.

4

2

1 3

6

5 7

Page 61: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Tree Terminology

● The height of a tree is the number of nodes in the longest path from the root to a leaf.

12

34

56

7

Page 62: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Efficiency of Insertion

● What is the big-O complexity of adding a node to a tree?

● Depends on the height of a tree!● Worst-case: have to take the longest path

down to find where the node goes.● Time is O(h), where h is the height of the

tree.

Page 63: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Tree Heights

● What are the maximum and minimum heights of a tree with n nodes?

● Maximum height: all nodes in a chain. Height is O(n).

● Minimum height: Tree is as complete as possible. Height is O(log n).

4

2

1 3

6

5 7

Page 64: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Tree Heights

● What are the maximum and minimum heights of a tree with n nodes?

● Maximum height: all nodes in a chain. Height is O(n).

● Minimum height: Tree is as complete as possible. Height is O(log n).

4

2

1 3

6

5 7

Page 65: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Keeping the Height Low

● There are many modifications of the binary search tree designed to keep the height of the tree low (usually O(log n)).

● A self-balancing binary search tree is a binary search tree that automatically adjusts itself to keep the height low.

● Details next time.

Page 66: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking Trees

http://blog.pro-ecuador.com/wp-content/uploads/2010/03/floating-tree.jpghttp://media.tumblr.com/tumblr_lp2fs6CgA11qejdya.jpg

Page 67: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

Page 68: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

Page 69: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

Page 70: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

1 2 3

Page 71: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

1 2 3

Page 72: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

1 2 3 4

Page 73: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

1 2 3 4

Page 74: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a BST

● One advantage of a BST is that elements are stored in sorted order.

● We can iterate over the elements of a BST in sorted order by walking the tree recursively.

4

2

1 3

6

5 7

1 2 3 4 5 6 7

Page 75: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Tree Traversals

● There are three general types of tree traversals:

● Preorder: Visit the node, then visit the children.

● Inorder: Visit the left child, then the node, then the right child.

● Postorder: Visit the children, then visit the node.

Page 76: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Walking a Tree

4

2

1 3

6

5 7

1 2 3 4 5 6 7

4 2 1 3 6 5 7

1 3 2 5 7 6 4

Inorder

Preorder

Postorder

Page 77: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Getting Rid of Trees

http://www.tigersheds.com/garden-resources/image.axd?picture=2010%2F6%2Fdeforestation1.jpg

Page 78: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

1 3

6

5 7

Page 79: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

1 3

6

5 7

Page 80: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

1 3

6

5 7

Page 81: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

1 3

6

5 7

Page 82: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

1 3

6

5 7

Page 83: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

3

6

5 7

Page 84: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

3

6

5 7

Page 85: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2

3

6

5 7

Page 86: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2 6

5 7

Page 87: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

2 6

5 7

Page 88: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

5 7

Page 89: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

5 7

Page 90: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

5 7

Page 91: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

5 7

Page 92: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

7

Page 93: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

7

Page 94: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

7

Page 95: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

Page 96: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

6

Page 97: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

Page 98: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

4

Page 99: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

Page 100: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

Page 101: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Freeing a Tree

● Once we're done with a tree, we need to free all of its nodes.

● As with a linked list, we have to be careful not to use any nodes after freeing them.

● This is done as follows:

● Base case: There is nothing to delete in an empty tree.

● Recursive step: Delete both subtrees, then delete the current node.

● What kind of tree traversal is this?

Page 102: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Range Searches

● We can use BSTs to do range searches, in which we find all values in the BST within some range.

● For example:● If values in a BST are dates, can find all

events that occurred within some time window.

● If values in a BST are samples of a random variable, can find everything within one and two standard deviations above the mean.

Page 103: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

Page 104: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

-1 +5

Page 105: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

-1 +5

Page 106: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

-1 +5

Page 107: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

-1 +5

Page 108: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

-1 +5

Page 109: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

-1 +5

Page 110: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Intuition

2

-1 4

-2 0 63

-1 +5

Page 111: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

The Logic

● Base case:● The empty tree has no nodes within any range.

● Recursive step:● If this node is below the lower bound, recursively

search the right subtree.● If this node is above the upper bound, recursively

search the left subtree.● If this node is within bounds:

– Search the left subtree.

– Add this node to the output.

– Search the right subtree.

Page 112: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 113: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 114: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 115: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 116: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 117: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 118: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 119: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 120: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 121: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

Page 122: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Complexity of Range Searches

● How do we get a runtime for a range search?

● Depends on how many nodes we find.

● If there are k nodes within the range, we do at least O(k) work finding them.

● In addition, we have two “border sets” of nodes that are immediately outside that range. Each set has size O(h), where h is the height of the tree.

● Total work done is O(k + h).

● This is an output-sensitive algorithm.

Page 123: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search

Next Time

● Fun With Data Structures.● Balanced binary search trees.● Ternary search trees.● DAWGs.