Binary Search Tree, Heap and Priority Queue - Notes | EECS 281, Study notes of Algorithms and Programming

Material Type: Notes; Professor: Jamin; Class: Data Struct&Algor; Subject: Electrical Engineering And Computer Science; University: University of Michigan - Ann Arbor; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-a4v
koofers-user-a4v 🇺🇸

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Outline
Last time:
Tree Terminology
Tree Traversal
N-ary Tree
Today:
Binary Search Tree (BST)
Binary Space Partition (BSP) Tree
Heap and Priority Queue
Sugih Jamin ([email protected])
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Binary Search Tree, Heap and Priority Queue - Notes | EECS 281 and more Study notes Algorithms and Programming in PDF only on Docsity!

Outline

Last time:

  • Tree Terminology
  • Tree Traversal
  • N -ary Tree

Today:

  • Binary Search Tree (BST)
  • Binary Space Partition (BSP) Tree
  • Heap and Priority Queue

Binary Search Tree (BST)

What is the difference between a binary tree and a binary search tree?

Definition of BST:

  • all values in Tl is < value on root
  • all values in Tr is > value on root
  • where ‘<’ and ‘>’ can be user defined

Search algorithm, search for key starting from T.root:

  • if T.root is null, not found
  • if key == T.root.value found
  • if key > T.root.value search T.Tr
  • if key < T.root.value search T.Tl

BST Insertion

Insertion of new element:

  • starting from root, find “appropriate” place for new
  • insert new
  • example: build a BST consisting of k, b, m, f, v, z, o, p, a
  • will the “appropriate” place always be a leaf node?

Given a BST, where are the smallest and largest elements?

How do you remove an element from a BST? Remove z, b, m from the above BST

BST Removal

After removal of a node, tree must remain a BST

Removal of an element elt:

  • search for elt
  • if elt is a leaf node, just remove it
  • what if elt is non-leaf?

o swap with either the smallest element of Tr or the largest element of Tl

o if elt is now leaf, remove it

o if non-leaf, it should have only one child, replace parent’s pointer to this node with pointer to child and remove the node

Binary Space Partitioning (BSP) Tree

What is a BSP?

A spatial data structure used to help organize geometry in n-dimensional space

Roughly: a BST that sorts objects in space by their coordinates

Used to speed up queries about whether geometric objects overlap or about the spatial ordering of geometric objects

Example uses:

  • computer graphics: whether an object is behind another, to determine display
  • robot motion planning: whether there is obstruction in a path
  • computer games: collision detection
  • in general: occlusion culling and collision detection

BSP Partitions Space

Blue wants to go to object e. Which objects are in the way?

a

i

m

d c e

b

h

k

j

g

n

l

x=30 1

(^4) x=

(^2) y=

x=52 3 A B

E

C

D

f

BSP Creation

a

i

m

d c e

b

h

k

j

g

n

l

x=30 1

(^4) x=

(^2) y=

x=52 3 A B

E

C

D

f

x=30^1

A a, b , gh, i, j, k y=30^2

E ln x=52^3

B d

D ce m

y=45^4

C f

BSP Found Colliding Object

Blue wants to go to object e. Which objects are in the way?

a

i

m

d c e

b

h

k

j

g

n

l

x=30 1

(^4) x=

(^2) y=

x=52 3 A B

E

C

D

f

x=30^1

A a, b , gh, i, j, k y=30^2

E ln x=52^3

B d

D ce m

y=45^4

C f

Next check for collision against items ’c’ and ’m’

MinHeap

MinHeap: an ordered tree with the following properties:

  • every subtree of T is a heap
  • the key in the root of T is ≤ the key in every subtree of T

Binary heap: must be a complete binary tree (see Preiss Fig. 11.4, note that it is not a representation of the ternary tree in Fig. 11.3)

A complete binary tree can be efficiently stored as an array

Binary MinHeap

findmin(): time complexity O( )

enqueue(): must maintain binary heap properties (Fig. 7.6 GTM)

  • insert new item as the rightmost leaf of the complete tree
  • percolate up: swap new with parent as long as (new’s key < parent’s key)

time complexity: O( )

dequeuemin(): (Fig. 7.7 GTM)

  • remove root
  • move rightmost item (elt) of lowest level to root
  • percolate down: swap elt with children with the smallest key until elt’s key is smaller than children’s or elt is a leaf node.
  • how to find the rightmost item?

time complexity: O( )

Discrete Event Simulations

Most common operations in discrete event simulations (for example, video games):

  • dequeuemin(): O(log N ) for all kinds of heap
  • enqueue(): Θ(log N ) for all kinds of heap except FibHeap (Θ(1))
  • “hold”, dequeuemin() followed immediately by enqueue(): O(log N ) for all kinds of heap