Docsity

Binary Search Trees: Data Structure and Operations, Slides of Computer Science

Partial preview of the text

Download Binary Search Trees: Data Structure and Operations and more Slides Computer Science in PDF only on Docsity!

Algorithms

Go over exam

Binary Search Trees

Exam

● Hand back, go over exam

Review: Binary Search Trees

● Binary Search Trees (BSTs) are an important

data structure for dynamic sets

● In addition to satellite data, eleements have:

■ key : an identifying field inducing a total ordering ■ left : pointer to a left child (may be NULL) ■ right : pointer to a right child (may be NULL) ■ p : pointer to a parent node (NULL for root)

Review: Binary Search Trees

● BST property:

key[leftSubtree(x)] ≤ key[x] ≤ key[rightSubtree(x)]

● Example:

F

B H

A D K

Inorder Tree Walk

● Example:

● How long will a tree walk take?

● Prove that inorder walk prints in

monotonically increasing order

F

B H

A D K

Operations on BSTs: Search

● Given a key and a pointer to a node, returns an

element with that key or NULL:

TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k);

Operations on BSTs: Search

● Here’s another function that does the same:

TreeSearch(x, k) while (x != NULL and k != key[x]) if (k < key[x]) x = left[x]; else x = right[x]; return x;

● Which of these two functions is more efficient?

Operations of BSTs: Insert

● Adds an element x to the tree so that the binary

search tree property continues to hold

● The basic algorithm

■ Like the search procedure above ■ Insert x in place of NULL ■ Use a “trailing pointer” to keep track of where you came from (like inserting into singly linked list)

BST Search/Insert: Running Time

● What is the running time of TreeSearch() or

TreeInsert()?

● A: O( h ), where h = height of tree

● What is the height of a binary search tree?

● A: worst case: h = O( n ) when tree is just a

linear string of left or right children

■ We’ll keep all analysis in terms of h for now ■ Later we’ll see how to maintain h = O(lg n )

Sorting With Binary Search Trees

● Informal code for sorting array A of length n :

BSTSort(A) for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root);

● Argue that this is Ω (n lg n)

● What will be the running time in the

■ Worst case? ■ Average case? (hint: remind you of anything?)

Sorting with BSTs

● Same partitions are done as with quicksort, but

in a different order

■ In previous example ○ Everything was compared to 3 once ○ Then those items < 3 were compared to 1 once ○ Etc. ■ Same comparisons as quicksort, different order! ○ Example: consider inserting 5

Sorting with BSTs

● Since run time is proportional to the number of

comparisons, same time as quicksort: O(n lg n)

● Which do you think is better, quicksort or

BSTsort? Why?

More BST Operations

● BSTs are good for more than sorting. For

example, can implement a priority queue

● What operations must a priority queue have?

■ Insert ■ Minimum ■ Extract-Min

BST Operations: Minimum

● How can we implement a Minimum() query?

● What is the running time?