trees chapter 8. chapter objectives to learn how to use a tree to represent a hierarchical...

42
Trees Chapter 8

Post on 18-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

TreesTrees

Chapter 8Chapter 8

Page 2: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Chapter ObjectivesChapter Objectives

To learn how to use a tree to represent a hierarchical organization of information

To learn how to use recursion to process trees To understand the different ways of traversing a tree To understand the difference between binary trees,

binary search trees, and heaps To learn how to implement binary trees, binary

search trees, and heaps using linked data structures and arrays

To learn how to use a tree to represent a hierarchical organization of information

To learn how to use recursion to process trees To understand the different ways of traversing a tree To understand the difference between binary trees,

binary search trees, and heaps To learn how to implement binary trees, binary

search trees, and heaps using linked data structures and arrays

Page 3: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Chapter Objectives (continued)

Chapter Objectives (continued)

To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner

To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced storage requirements

To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner

To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced storage requirements

Page 4: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Tree TerminologyTree Terminology

A tree consists of a collection of elements or nodes, with each node linked to its successors

The node at the top of a tree is called its root The links from a node to its successors are called

branches The successors of a node are called its children The predecessor of a node is called its parent

A tree consists of a collection of elements or nodes, with each node linked to its successors

The node at the top of a tree is called its root The links from a node to its successors are called

branches The successors of a node are called its children The predecessor of a node is called its parent

Page 5: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Tree Terminology (continued)Tree Terminology (continued)

Each node in a tree has exactly one parent except for the root node, which has no parent

Nodes that have the same parent are siblings A node that has no children is called a leaf node A generalization of the parent-child relationship is

the ancestor-descendent relationship

Each node in a tree has exactly one parent except for the root node, which has no parent

Nodes that have the same parent are siblings A node that has no children is called a leaf node A generalization of the parent-child relationship is

the ancestor-descendent relationship

Page 6: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Tree Terminology (continued)Tree Terminology (continued)

A subtree of a node is a tree whose root is a child of that node

The level of a node is a measure of its distance from the root

A subtree of a node is a tree whose root is a child of that node

The level of a node is a measure of its distance from the root

Page 7: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Binary TreesBinary Trees

In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either of the

following is true T is empty Its root node has two subtrees, TL and TR, such

that TL and TR are binary trees

In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either of the

following is true T is empty Its root node has two subtrees, TL and TR, such

that TL and TR are binary trees

Page 8: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Some Types of Binary TreesSome Types of Binary Trees

Expression tree Each node contains an operator or an operand

Huffman tree Represents Huffman codes for characters that

might appear in a text file Huffman code uses different numbers of bits to

encode letters as opposed to ASCII or Unicode Binary search trees

All elements in the left subtree precede those in the right subtree

Expression tree Each node contains an operator or an operand

Huffman tree Represents Huffman codes for characters that

might appear in a text file Huffman code uses different numbers of bits to

encode letters as opposed to ASCII or Unicode Binary search trees

All elements in the left subtree precede those in the right subtree

Page 9: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Some Types of Binary Trees (continued)

Some Types of Binary Trees (continued)

Page 10: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Fullness and CompletenessFullness and Completeness

Trees grow from the top down Each new value is inserted in a new leaf node A binary tree is full if every node has two children

except for the leaves

Trees grow from the top down Each new value is inserted in a new leaf node A binary tree is full if every node has two children

except for the leaves

Page 11: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

General TreesGeneral Trees

Nodes of a general tree can have any number of subtrees

A general tree can be represented using a binary tree

Nodes of a general tree can have any number of subtrees

A general tree can be represented using a binary tree

Page 12: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Tree TraversalsTree Traversals

Often we want to determine the nodes of a tree and their relationship Can do this by walking through the tree in a

prescribed order and visiting the nodes as they are encountered This process is called tree traversal

Three kinds of tree traversal Inorder Preorder Postorder

Often we want to determine the nodes of a tree and their relationship Can do this by walking through the tree in a

prescribed order and visiting the nodes as they are encountered This process is called tree traversal

Three kinds of tree traversal Inorder Preorder Postorder

Page 13: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Tree Traversals (continued)Tree Traversals (continued)

Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

Page 14: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Visualizing Tree TraversalsVisualizing Tree Traversals

You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree If the mouse always keeps the tree to the left, it

will trace a route known as the Euler tour Preorder traversal if we record each node as

the mouse first encounters it Inorder if each node is recorded as the mouse

returns from traversing its left subtree Postorder if we record each node as the

mouse last encounters it

You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree If the mouse always keeps the tree to the left, it

will trace a route known as the Euler tour Preorder traversal if we record each node as

the mouse first encounters it Inorder if each node is recorded as the mouse

returns from traversing its left subtree Postorder if we record each node as the

mouse last encounters it

Page 15: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Visualizing Tree Traversals (continued)

Visualizing Tree Traversals (continued)

Page 16: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Traversals of Binary Search Trees and Expression TreesTraversals of Binary Search Trees and Expression Trees

An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value

An inorder traversal of an expression tree inserts parenthesis where they belong (infix form)

A postorder traversal of an expression tree results in postfix form

An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value

An inorder traversal of an expression tree inserts parenthesis where they belong (infix form)

A postorder traversal of an expression tree results in postfix form

Page 17: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

The Node<E> ClassThe Node<E> Class

Just as for a linked list, a node consists of a data part and links to successor nodes

The data part is a reference to type E A binary tree node must have links to both its left

and right subtrees

Just as for a linked list, a node consists of a data part and links to successor nodes

The data part is a reference to type E A binary tree node must have links to both its left

and right subtrees

Page 18: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

The BinaryTree<E> ClassThe BinaryTree<E> Class

Page 19: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

The BinaryTree<E> Class (continued)

The BinaryTree<E> Class (continued)

Page 20: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Overview of a Binary Search Tree

Overview of a Binary Search Tree

Binary search tree definition A set of nodes T is a binary search tree if either of

the following is true T is empty Its root has two subtrees such that each is a

binary search tree and the value in the root is greater than all values of the left subtree but less than all values in the right subtree

Binary search tree definition A set of nodes T is a binary search tree if either of

the following is true T is empty Its root has two subtrees such that each is a

binary search tree and the value in the root is greater than all values of the left subtree but less than all values in the right subtree

Page 21: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Overview of a Binary Search Tree (continued)

Overview of a Binary Search Tree (continued)

Page 22: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Searching a Binary TreeSearching a Binary Tree

Page 23: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Class TreeSet and Interface Search Tree

Class TreeSet and Interface Search Tree

Page 24: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

BinarySearchTree ClassBinarySearchTree Class

Page 25: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Insertion into a Binary Search Tree

Insertion into a Binary Search Tree

Page 26: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Removing from a Binary Search Tree

Removing from a Binary Search Tree

Page 27: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Removing from a Binary Search Tree (continued)Removing from a Binary Search Tree (continued)

Page 28: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Heaps and Priority QueuesHeaps and Priority Queues

In a heap, the value in a node is les than all values in its two subtrees

A heap is a complete binary tree with the following properties The value in the root is the smallest item in the

tree Every subtree is a heap

In a heap, the value in a node is les than all values in its two subtrees

A heap is a complete binary tree with the following properties The value in the root is the smallest item in the

tree Every subtree is a heap

Page 29: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Inserting an Item into a HeapInserting an Item into a Heap

Page 30: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Removing an Item from a HeapRemoving an Item from a Heap

Page 31: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Implementing a HeapImplementing a Heap

Because a heap is a complete binary tree, it can be implemented efficiently using an array instead of a linked data structure

First element for storing a reference to the root data Use next two elements for storing the two children

of the root Use elements with subscripts 3, 4, 5, and 6 for

storing the four children of these two nodes and so on

Because a heap is a complete binary tree, it can be implemented efficiently using an array instead of a linked data structure

First element for storing a reference to the root data Use next two elements for storing the two children

of the root Use elements with subscripts 3, 4, 5, and 6 for

storing the four children of these two nodes and so on

Page 32: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Inserting into a Heap Implemented as an ArrayList

Inserting into a Heap Implemented as an ArrayList

Page 33: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Inserting into a Heap Implemented as an ArrayList

(continued)

Inserting into a Heap Implemented as an ArrayList

(continued)

Page 34: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Priority QueuesPriority Queues

The heap is used to implement a special kind of queue called a priority queue

The heap is not very useful as an ADT on its own Will not create a Heap interface or code a class

that implements it Will incorporate its algorithms when we

implement a priority queue class and Heapsort Sometimes a FIFO queue may not be the best way to

implement a waiting line A priority queue is a data structure in which only the

highest-priority item is accessible

The heap is used to implement a special kind of queue called a priority queue

The heap is not very useful as an ADT on its own Will not create a Heap interface or code a class

that implements it Will incorporate its algorithms when we

implement a priority queue class and Heapsort Sometimes a FIFO queue may not be the best way to

implement a waiting line A priority queue is a data structure in which only the

highest-priority item is accessible

Page 35: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Insertion into a Priority QueueInsertion into a Priority Queue

Page 36: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

The PriorityQueue ClassThe PriorityQueue Class Java provides a PriorityQueue<E> class that implements

the Queue<E> interface given in Chapter 6. Peek, poll, and remove methods return the smallest item in

the queue rather than the oldest item in the queue.

Java provides a PriorityQueue<E> class that implements the Queue<E> interface given in Chapter 6.

Peek, poll, and remove methods return the smallest item in the queue rather than the oldest item in the queue.

Page 37: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Design of a KWPriorityQueue Class

Design of a KWPriorityQueue Class

Page 38: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Huffman TreesHuffman Trees

A Huffman tree can be implemented using a binary tree and a PriorityQueue

A straight binary encoding of an alphabet assigns a unique binary number to each symbol in the alphabet Unicode for example

The message “go eagles” requires 144 bits in Unicode but only 38 using Huffman coding

A Huffman tree can be implemented using a binary tree and a PriorityQueue

A straight binary encoding of an alphabet assigns a unique binary number to each symbol in the alphabet Unicode for example

The message “go eagles” requires 144 bits in Unicode but only 38 using Huffman coding

Page 39: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Huffman Trees (continued)Huffman Trees (continued)

Page 40: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Huffman Trees (continued)Huffman Trees (continued)

Page 41: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Chapter ReviewChapter Review

A tree is a recursive, nonlinear data structure that is used to represent data that is organized as a hierarchy

A binary tree is a collection of nodes with three components: a reference to a data object, a reference to a left subtree, and a reference to a right subtree

In a binary tree used for arithmetic expressions, the root node should store the operator that is evaluated last

A binary search tree is a tree in which the data stored in the left subtree of every node is less than the data stored in the root node, and the data stored in the right subtree is greater than the data stored in the root node

A tree is a recursive, nonlinear data structure that is used to represent data that is organized as a hierarchy

A binary tree is a collection of nodes with three components: a reference to a data object, a reference to a left subtree, and a reference to a right subtree

In a binary tree used for arithmetic expressions, the root node should store the operator that is evaluated last

A binary search tree is a tree in which the data stored in the left subtree of every node is less than the data stored in the root node, and the data stored in the right subtree is greater than the data stored in the root node

Page 42: Trees Chapter 8. Chapter Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion

Chapter Review (continued)Chapter Review (continued)

A heap is a complete binary tree in which the data in each node is less than the data in both its subtrees

Insertion and removal in a heap are both O(log n) A Huffman tree is a binary tree used to store a code

that facilitates file compression

A heap is a complete binary tree in which the data in each node is less than the data in both its subtrees

Insertion and removal in a heap are both O(log n) A Huffman tree is a binary tree used to store a code

that facilitates file compression