+ All Categories
Home > Documents > Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Date post: 31-Dec-2015
Category:
Upload: jessica-moore
View: 220 times
Download: 1 times
Share this document with a friend
Popular Tags:
46
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java
Transcript
Page 1: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Chapter 19Implementing Trees and Priority

Queues

Fundamentals of Java

Page 2: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 2

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.

Page 3: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 3

Objectives (cont.)

Understand the basic tree traversals. Use binary search trees to implement sorted

sets and sorted maps. Use heaps to implement priority queues.

Page 4: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 4

Vocabulary

Binary search tree Binary tree Expression tree General tree Heap Heap property

Page 5: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 5

Vocabulary (cont.)

Interior node Leaf Left subtree Parse tree Right subtree Root

Page 6: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 6

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

Page 7: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 7

An Overview of Trees (cont.)

Figure 19-1: Parse tree for a sentence

Page 8: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 8

An Overview of Trees (cont.)

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

Page 9: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 9

An Overview of Trees (cont.)

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

Page 10: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 10

An Overview of Trees (cont.)

Figure 19-2: Tree and some of its properties

Page 11: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 11

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

Page 12: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 12

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 TNode 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 subtree and a right subtree (binary trees)

Page 13: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 13

An Overview of Trees (cont.)

Figure 19-4: Different types of binary trees

Page 14: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 14

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.

Page 15: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 15

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

Page 16: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 16

An Overview of Trees (cont.)

Expression tree: For evaluating expressions

Figure 19-6: Some expression trees

Page 17: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 17

An Overview of Trees: Binary Search Trees

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

Page 18: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 18

An Overview of Trees: Binary Search Trees (cont.)

Figure 19-8: Binary search tree

Page 19: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 19

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:

Page 20: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 20

An Overview of Trees: Binary Search Trees (cont.)

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

Page 21: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 21

Binary Tree Traversals

Figure 19-11: Inorder traversal

Figure 19-10: Preorder traversal

Page 22: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 22

Binary Tree Traversals (cont.)

Figure 19-13: Level-order traversal

Figure 19-12: Postorder traversal

Page 23: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 23

Linked Implementation of Binary Trees

Table 19-2: Methods of the BSTPT interface

Page 24: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 24

Linked Implementation of Binary Trees (cont.)

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

Page 25: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 25

Linked Implementation of Binary Trees (cont.)

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

Page 26: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 26

Linked Implementation of Binary Trees (cont.)

Example 19.1: Interface for binary search tree prototypes

Page 27: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 27

Linked Implementation of Binary Trees (cont.)

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

Page 28: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 28

Linked Implementation of Binary Trees (cont.)

add method

Page 29: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 29

Linked Implementation of Binary Trees (cont.)

add method (cont.)

Page 30: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 30

Linked Implementation of Binary Trees (cont.)

Pseudocode for searching a binary tree:

Page 31: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 31

Linked Implementation of Binary Trees (cont.)

Inorder traversal code:

Page 32: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 32

Linked Implementation of Binary Trees (cont.)

Pseudocode for level-order traversal:

Page 33: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 33

Linked Implementation of Binary Trees (cont.)

Steps for removing a node:

Page 34: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 34

Linked Implementation of Binary Trees (cont.)

Expanded step 4 for removing a node from a binary tree:

Page 35: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 35

Array Implementation of a Binary Tree

Figure 19-16: Complete binary tree

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

Page 36: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 36

Array Implementation of a Binary Tree (cont.)

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

Page 37: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 37

Array Implementation of a Binary Tree (cont.)

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

Page 38: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 38

Implementing Heaps

Table 19-5: Methods in the interface HeapPT

Page 39: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 39

Implementing Heaps (cont.)

add method:

Page 40: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 40

Implementing Heaps (cont.)

pop method:

Page 41: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 41

Implement Heaps (cont.)

pop method (cont.):

Page 42: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 42

Using a Heap to Implement a Priority Queue

Example 19.3: Heap implementation of a priority queue

Page 43: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 43

Using a Heap to Implement a Priority Queue (cont.)

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

Page 44: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 44

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.

Page 45: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 45

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.

Page 46: Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.

Fundamentals of Java 46

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.


Recommended