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

An overview of binary search trees (bsts), a dynamic set data structure used for maintaining sorted elements with efficient search, insert, and delete operations. The properties of bsts, inorder tree walk, search algorithm, insertion, and deletion. It also discusses the use of bsts for sorting and implementing priority queues.

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Go over exam
Binary Search Trees
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

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?