CS187 Programming with Data Structures 9' Trees all kinds - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS187 Programming with Data Structures 9' Trees all kinds

Description:

deleted node has one child (easy) deleted node has two children ... Properties of Heaps. Always complete (why?) How can we find an element? ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 20
Provided by: robotics5
Category:

less

Transcript and Presenter's Notes

Title: CS187 Programming with Data Structures 9' Trees all kinds


1
CS187 Programming with Data Structures9. Trees
(all kinds)
  • Oliver Brock
  • UMass Amherst
  • Spring 2009

2
Overview (mostly for people who missed class)
  • In this class (or classes) we will do the
    following
  • Repeat what we know about trees
  • Traverse trees
  • Learn about the following data structures
  • Binary Search Trees
  • Heaps
  • Priority Queues
  • http//webpages.ull.es/users/jriera/Docencia/AVL/A
    VL20tree20applet.htm

3
(No Transcript)
4
Terminology
  • gtgtDefinition?ltlt
  • Root
  • Leaf
  • Node
  • Child
  • Parent
  • Sibling
  • arity
  • full
  • complete
  • pre/in/post order traversal

5
From previous classes
  • Implementation of binary tree
  • Different traversals
  • (Remember the relationship with stacks and
    queues?)

6
Binary Search Tree Example
How does the definition of a binary tree change
for a binary search tree?
7
Search Tree Interface from the textbook
8
Binary Search Tree
9
Methods of BST
  • find
  • insert (called add in the textbook)
  • delete
  • deleted node has no children (easy)
  • deleted node has one child (easy)
  • deleted node has two children
  • Left child has no right child use child to
    overwrite deleted node
  • Otherwise find largest node in right subtree
    (how?) and move it to deleted node

10
Removing the Rat
11
Heap
12
Methods of Heaps
  • insert
  • remove (only highest priority item)
  • find? lets see

13
Inserting an Item into a Heap
14
Removing an Item from a Heap
15
Implementing a Heap as an ArrayList
16
Properties of Heaps
  • Always complete (why?)
  • How can we find an element?
  • How do we have to change the definition of BST?
  • if node has index p then left child is at 2p1
    and right child is at 2p2 (why?)

17
Inserting into a Heap Implemented as an ArrayList
(continued)
18
Priority Queue
19
Questions
  • When would you use a BST?
  • When would you use a PQ?
  • Can you implement a PQ using a BST?
Write a Comment
User Comments (0)
About PowerShow.com