chapter 19 implementing trees and priority queues n.
Download
Skip this Video
Download Presentation
Chapter 19 Implementing Trees and Priority Queues

Loading in 2 Seconds...

play fullscreen
1 / 46

Chapter 19 Implementing Trees and Priority Queues - PowerPoint PPT Presentation


  • 139 Views
  • Uploaded on

Chapter 19 Implementing Trees and Priority Queues. Fundamentals of Java. Objectives. Use the appropriate terminology to describe trees. Distinguish different types of hierarchical collections such as general trees, binary trees, binary search trees, and heaps. Objectives (cont.).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 19 Implementing Trees and Priority Queues' - dimaia


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
objectives
Objectives
  • Use the appropriate terminology to describe trees.
  • Distinguish different types of hierarchical collections such as general trees, binary trees, binary search trees, and heaps.

Fundamentals of Java

objectives cont
Objectives (cont.)
  • Understand the basic tree traversals.
  • Use binary search trees to implement sorted sets and sorted maps.
  • Use heaps to implement priority queues.

Fundamentals of Java

vocabulary
Vocabulary
  • Binary search tree
  • Binary tree
  • Expression tree
  • General tree
  • Heap
  • Heap property

Fundamentals of Java

vocabulary cont
Vocabulary (cont.)
  • Interior node
  • Leaf
  • Left subtree
  • Parse tree
  • Right subtree
  • Root

Fundamentals of Java

an overview of trees
An Overview of Trees
  • Tree: Data structure in which each item can have multiple successors
    • All items have exactly one predecessor.
      • Except a privileged item called the root
  • Parse tree: Describes the syntactic structure of a sentence in terms of its component parts
    • Noun phrases and verb phrases

Fundamentals of Java

an overview of trees cont
An Overview of Trees (cont.)

Figure 19-1: Parse tree for a sentence

Fundamentals of Java

an overview of trees cont1
An Overview of Trees (cont.)

Table 19-1: Summary of terms used to describe trees

Fundamentals of Java

an overview of trees cont2
An Overview of Trees (cont.)

Table 19-1: Summary of terms used to describe trees (cont.)

Fundamentals of Java

an overview of trees cont3
An Overview of Trees (cont.)

Figure 19-2: Tree and some of its properties

Fundamentals of Java

an overview of trees cont4
An Overview of Trees (cont.)
  • General trees: Trees with no restrictions on number of children
  • Binary trees: Each node has at most two children: left child and right child.

Figure 19-3: Two unequal binary trees that have equal sets of nodes

Fundamentals of Java

an overview of trees cont5
An Overview of Trees (cont.)
  • Recursive processing of trees is common, so useful to have recursive definitions of trees
    • General tree: Either empty or consists of a finite set of nodes T
      • Node r is the root.
      • Set T - {r} partitioned into disjoint subsets (general trees)
    • Binary tree: Either empty or consists of a root plus a left subtreeand a right subtree (binary trees)

Fundamentals of Java

an overview of trees cont6
An Overview of Trees (cont.)

Figure 19-4: Different types of binary trees

Fundamentals of Java

an overview of trees cont7
An Overview of Trees (cont.)
  • Full binary tree: Contains maximum number of nodes for its height
    • Fully balanced
    • If height is d, 2d-1 nodes
    • Level n has up to 2n nodes.
    • Height of a fully balanced tree of n nodes is log2n.

Fundamentals of Java

an overview of trees cont8
An Overview of Trees (cont.)
  • Heap: Binary tree in which the item in each node is less than or equal to the items in both of its children
    • Heap property

Figure 19-5: Examples of heaps

Fundamentals of Java

an overview of trees cont9
An Overview of Trees (cont.)
  • Expression tree: For evaluating expressions

Figure 19-6: Some expression trees

Fundamentals of Java

an overview of trees binary search trees
An Overview of Trees: Binary Search Trees

Figure 19-7: Call tree for the binary search of an array

Fundamentals of Java

an overview of trees binary search trees cont
An Overview of Trees: Binary Search Trees (cont.)

Figure 19-8: Binary search tree

Fundamentals of Java

an overview of trees binary search trees cont1
An Overview of Trees: Binary Search Trees (cont.)
  • Binary search tree: Each node is greater than or equal to left child and less than or equal to right child.
  • Recursive search process:

Fundamentals of Java

an overview of trees binary search trees cont2
An Overview of Trees: Binary Search Trees (cont.)

Figure 19-9: Three binary tree shapes with the same data

Fundamentals of Java

binary tree traversals
Binary Tree Traversals

Figure 19-10: Preorder traversal

Figure 19-11: Inorder traversal

Fundamentals of Java

binary tree traversals cont
Binary Tree Traversals (cont.)

Figure 19-12: Postorder traversal

Figure 19-13: Level-order traversal

Fundamentals of Java

linked implementation of binary trees
Linked Implementation of Binary Trees

Table 19-2: Methods of the BSTPT interface

Fundamentals of Java

linked implementation of binary trees cont
Linked Implementation of Binary Trees (cont.)

Table 19-2: Methods of the BSTPT interface (cont.)

Fundamentals of Java

linked implementation of binary trees cont1
Linked Implementation of Binary Trees (cont.)

Figure 19-14: Interfaces and classes used in the binary search tree prototype

Fundamentals of Java

linked implementation of binary trees cont2
Linked Implementation of Binary Trees (cont.)

Example 19.1: Interface for binary search tree prototypes

Fundamentals of Java

linked implementation of binary trees cont3
Linked Implementation of Binary Trees (cont.)

Example 19.1: Interface for binary search tree prototypes (cont.)

Fundamentals of Java

linked implementation of binary trees cont4
Linked Implementation of Binary Trees (cont.)

add method

Fundamentals of Java

linked implementation of binary trees cont5
Linked Implementation of Binary Trees (cont.)

add method (cont.)

Fundamentals of Java

linked implementation of binary trees cont6
Linked Implementation of Binary Trees (cont.)
  • Pseudocode for searching a binary tree:

Fundamentals of Java

linked implementation of binary trees cont7
Linked Implementation of Binary Trees (cont.)
  • Inorder traversal code:

Fundamentals of Java

linked implementation of binary trees cont8
Linked Implementation of Binary Trees (cont.)
  • Pseudocode for level-order traversal:

Fundamentals of Java

linked implementation of binary trees cont9
Linked Implementation of Binary Trees (cont.)
  • Steps for removing a node:

Fundamentals of Java

linked implementation of binary trees cont10
Linked Implementation of Binary Trees (cont.)
  • Expanded step 4 for removing a node from a binary tree:

Fundamentals of Java

array implementation of a binary tree
Array Implementation of a Binary Tree

Figure 19-16: Complete binary tree

Figure 19-17: Array representation of a complete binary tree

Fundamentals of Java

array implementation of a binary tree cont
Array Implementation of a Binary Tree (cont.)

Table 19-3: Locations of given items in an array representation of a complete binary tree

Fundamentals of Java

array implementation of a binary tree cont1
Array Implementation of a Binary Tree (cont.)

Table 19-4: Relatives of a given item in an array representation of a complete binary tree

Fundamentals of Java

implementing heaps
Implementing Heaps

Table 19-5: Methods in the interface HeapPT

Fundamentals of Java

implementing heaps cont
Implementing Heaps (cont.)
  • add method:

Fundamentals of Java

implementing heaps cont1
Implementing Heaps (cont.)
  • pop method:

Fundamentals of Java

implement heaps cont
Implement Heaps (cont.)
  • pop method (cont.):

Fundamentals of Java

using a heap to implement a priority queue
Using a Heap to Implement a Priority Queue

Example 19.3: Heap implementation of a priority queue

Fundamentals of Java

using a heap to implement a priority queue cont
Using a Heap to Implement a Priority Queue (cont.)

Example 19.3: Heap implementation of a priority queue (cont.)

Fundamentals of Java

summary
Summary
  • There are various types of trees or hierarchical collections such as general trees, binary trees, binary search trees, and heaps.
  • The terminology used to describe hierarchical collections is borrowed from biology, genealogy, and geology.

Fundamentals of Java

summary cont
Summary (cont.)
  • Tree traversals: preorder, inorder, postorder, and level-order traversal
  • A binary search tree preserves a natural ordering among its items and can support operations that run in logarithmic time.
  • Binary search trees are useful for implementing sorted sets and sorted maps.

Fundamentals of Java

summary cont1
Summary (cont.)
  • Heap
    • Useful for ordering items according to priority
    • Guarantees logarithmic insertions and removals
    • Useful for implementing priority queues
  • Binary search trees typically have a linked implementation.
  • Heaps typically have an array representation.

Fundamentals of Java