chapter 9 binary tree and general tree. overview ● two-way decision making is one of the...

83
Chapter 9 Binary Tree and General Tree

Upload: mervyn-adam-bradley

Post on 13-Jan-2016

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Chapter 9

Binary Tree and General Tree

Page 2: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Overview

● Two-way decision making is one of the fundamental concepts in computing. A binary tree models two-way decisions. A hierarchy represents multi-way choices. The general tree is an extension of the binary tree.

Page 3: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Learning Objectives

● Describe a binary tree in terms of its structure and components, and learn recursive definitions of the binary tree and its properties.

● Study standard tree traversals in depth.● Develop a binary tree class interface based on

its recursive definition.● Learn about the signature of a binary tree and

understand how to build a binary tree given its signature.

Page 4: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Learning Objectives

● Understand Huffman coding, a binary tree-based text compression application, and use the binary tree class to implement Huffman coding.

● Implement the binary tree class.● Study how tree traversals may be implemented

non-recursively using a stack.● Describe the properties of a general tree.

Page 5: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Learning Objectives

● Learn the natural correspondence of a general tree with an equivalent binary tree, and the signature of a general tree.

Page 6: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.1 Components

Page 7: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.1 Components

● A binary tree consists of nodes and branches. A node is a place in the tree where data is stored.

● There is a special node called the root. “starting point” of the tree.

● The nodes are connected to each other by links or branches.

A left branch or right branch.

Binary means that there are at most two choices.● A node is said to have at most two children.● A node that does not have any children is called a leaf.● Non-leaf nodes are called internal nodes.

Page 8: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.1 Components

● There is a single path from any node to any other node in the tree.

Page 9: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.2 Position as Meaning● If-then-else tree

Represents an if-then-else construct in a program.

● Every node in this tree is conditional expression that evaluates to yes or no.

● If it evaluates to yes, the left branch (if any) is taken and if evaluates to no, the right branch (if any) is taken.

Page 10: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.2 Position as Meaning

● Expression tree (f + ((a * b) - c))

Double-click to add graphics

Page 11: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Create Expression tree● 1- If the current token is a '(', add a new node as the

left child of the current node, and descend to the left child.

● 2- If the current token is in the list ['+','-','/','*'], set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child.

● 3- If the current token is a number, set the root value of the current node to the number and return to the parent.

● 4- If the current token is a ')', go to the parent of the current node.

Page 12: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

● let’s look at an example of the rules outlined above in action. We will use the expression (3+(4 5)). ∗

● We will parse this expression into the following list of character tokens['(', '3', '+', '(', '4', '*', '5' ,')',')'].

● Initially we will start out with a parse tree that consists of root node.

Page 13: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

(3+(4 5))∗

Page 14: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.3 Structure

● Structure Two trees with the same number of nodes may not

have the same structure.

Page 15: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.3 Structure

● Depth is the distance from the root. Nodes at the same

depth are said to be at the same level, with the root being at level zero.

The height of a tree is the maximum level (or depth) at which there is a node.

Page 16: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.3 Structure

• Full Binary Tree: A binary tree in which all of the leaves are on the same level and every nonleaf node has two children

Page 17: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.3 Structure

● (a), first three are strictly binary, but the fourth is not. first two are FULL binary tree

● (b), first two are complete and the last two are not.

● At level i, there can be at most 2i nodes. Maximum number of nodes over all the levels.

Page 18: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.4 Recursive Definitions

Page 19: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.1.4 Recursive Definitions

Page 20: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Traversal Definitions

● Preorder traversal: Visit the root, visit the left subtree, visit the right subtree

● Inorder traversal: Visit the left subtree, visit the root, visit the right subtree

● Postorder traversal: Visit the left subtree, visit the right subtree, visit the root

Page 21: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Visualizing Binary Tree Traversals

Page 22: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Three Binary Tree Traversals

Page 23: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.2 Binary Tree Traversals

Page 24: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.2 Binary Tree Traversals

Page 25: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Binary Search Tree

Page 26: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Overview

● A binary tree possesses ordering property that maintains the data in its nodes in sorted order. Since the search tree is a linked structure, entries

may be inserted and deleted without having to move other entries over, unlike ordered lists in which insertions and deletions require data movement.

The AVL tree is a height-balanced binary search tree that delivers guaranteed worst-case search, insert, and delete times that are all O(log n)

Page 27: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Learning Objectives

● Explore the motivation for binary search trees by learning about the comparison tree for binary search.

● Use the comparison tree as an analytical tool to determine the running time of binary search.

● Describe the binary search tree structure and properties.

● Study the primary binary search tree operations of search, insert, and delete, and analyze their running times.

Page 28: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Learning Objectives

● Understand a binary search tree class interface and use it in application examples.

● Implement the binary search tree class with a binary tree class as the reused storage component.

● Study the AVL tree structure properties, the search, insert, and delete operations, and their running times.

Page 29: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.2 Binary Search Tree Properties

Page 30: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.2 Binary Search Tree Properties

● All three trees have the same set of keys. ● Their structures are different, depending on the

sequence of insertion or deletion.

Page 31: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3 Binary Search Tree Operations

● Three foundational operations Search Insert Delete

Page 32: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3.1 Search

The tree nodes are for real.● The target key is compared against the key at the root of

the tree.● If they are equal, sucess.● If not, recusively search the appropriate child.● Search terminates with failure if an empty subtree is

reached.

Page 33: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3.1 Search

Page 34: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3.2 Insert

● To insert a value, search must force a failure.● Item in inserted in the failed location.● A newly inserted node always becomes a leaf

node in the search tree.

Page 35: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3.2 Insert

Page 36: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3.3 Delete

● The value to be deleted is first located in the binary search tree.

● Three possible cases. Case a: X is a leaf node.

Page 37: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3.3 Delete

Case b: X has one child● Replace the deleted node with the child.

Page 38: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.3.3 Delete

Case c: X has two children● Find the inorder predecessor, Y, of X.● Copy the entry at Y into X.● Apply deletion on Y.

Applying deletion on Y will revert to either case b or a since Y is guaranteed to not have a right subtree.

Page 39: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Running Times

● Search: worst case Tied to the worst possible shape a tree can attain. Such a tree degenerates into sequential search.

● O(n).

Page 40: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Running Times

Page 41: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Running Times

● Insertion: worst case O(n)

● Deletion: worst case O(n)

Page 42: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

Balancing

● Keeping a binary search tree balanced allows the height never to exceed O(log n).

● There are two popular ways of maintaining and constructing balanced binary search trees. AVL tree. red-black tree.

Page 43: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.4 A BinarySearchTree Class

Page 44: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.4 A BinarySearchTree Class

Page 45: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.5.1 Example Treesort

● inOrder traversal method invokes visitor.visit() when a node is visited.

Page 46: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.5.1 Example: Treesort

Page 47: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.5.2 Example: Counting Keys

● Count the number of keys in a binary search tree that are less than a given key.

● It is possible to simply examine every node of the tree.

Page 48: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6 BinarySearchTree Class Implementation

Page 49: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6 BinarySearchTree Class Implementation

Page 50: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.1 Search Implementation

Page 51: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.2 Insert Implementation

Page 52: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.2 Insert Implementation

Page 53: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.2 Insert Implementation

Page 54: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.2 Insert Implementation

● left, right, and parent fields are directly accessed, instead of calling attachLeft and attachRight.

● Clients of BinaryTree that are not in its package are required to use attachLeft or attachRight.

Page 55: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.3 Delete Implementation

● findPredecessor helper method. Case c requires finding the inorder predeccessor.

Page 56: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.3 Delete Implementation

● deleteHere helper method.

Page 57: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.3 Delete Implementation

Page 58: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.3 Delete Implementation

Page 59: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.3 Delete Implementation

Page 60: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.4 Convenience Methods and Traversals

● minValue needs to find the "leftmost" node.● maxValue finds the "rightmost".

Page 61: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.4 Convenience Methods and Traversals

Page 62: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.6.4 Convenience Methods and Traversals

● This implementation hides the tree structure from its clients.

● All clients need to see are the search, insert, and delete operation.

● "preorder", "inorder", and "postorder" provide a small window into the implementation structure.

Page 63: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.9 Summary

● A comparison tree for binary search on an array is a binary tree that depicts all possible search paths.

● A failure node in a comparison tree catches a range of values that lie between its inorder predecessor and its in order successor in the tree.

● A comparison tree is simply a conceptual tool to analyze the time taken by binary search, and is therefore often referred to as an implicit search tree.

Page 64: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.9 Summary

● The shape of the comparison tree is independent of the data entries in the array; it only depends on the length of the array.

● The worst-case number of comparisons for a successful search in 2h-1, and for unsuccessful search is 2h.

● Binary search on an ordered array is O(log n).● A binary search tree is a binary tree whose

entries are arranged in order.

Page 65: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.9 Summary

● An inorder traversal of a binary search tree will visit the nodes in ascending order of values.

● The values stored in a binary search tree must lend themselves to being arranged in order.

● For any given number of values, n, there is only one binary search comparison tree. There are many binary search trees possible as there are different binary trees that can be constructed out of n nodes.

Page 66: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.9 Summary

● The worst possible binary tree structure is one that is completely skewed either to the left or right – O(n).

● The worst-case running times for search, insert, and delete in a balanced binary search tree are all O(log n).

● Treesort is an algorithm to sort a set of values by inserting them one by one into a binary search tree, and then visiting them in inorder sequence -- O(n2).

Page 67: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.9 Summary

● The AVL tree and red-black tree are two of the many types of balanced binary search trees that guarantee a worst case search / insert / delete time of O(log n).

● An AVL tree is a binary search tree in which the heights of the left and right subtrees of every node differ by at most 1.

● Recursive definition: An AVL tree is a binary search tree in which the left and right subtrees of the root are AVL trees whose heights differ by at most 1.

Page 68: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

10.9 Summary

● Rotation about a link in an AVL tree takes O(1) time.

● Insertion in an AVL tree starts with a regular binary search tree insertion, followed by rebalancing.

● Deletion in an AVL tree starts with a regular binary search tree deletion, followed by rebalancing.

Page 69: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

Page 70: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

Page 71: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

Page 72: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

Page 73: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

● Delete a single node from a tree.

Page 74: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

● Recursive traversal procedures.

Page 75: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

● Running times of methods makeRoot, setData, and getData involve reading or

writing data once. Checking for whether the pointer is null. isEmpty O(1). clear simply have to set the root pointer to null.

● O(1) attachLeft and attachRight

● Three pointer settings O(1)● detachLeft and detachRight O(1)

Page 76: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.3 A Binary Tree Class

● Root called on a leaf node that is at the greatest possible depth in the tree. O(h) h is the height of the tree. The height could be as much as n – 1 in the case

where the tree is entirely lopsided. O(n)

Page 77: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.9 Summary

● Binary trees model two-way decision making systems.

● A binary tree consists of nodes and branches. There is a special node called the root.

● Every node in a binary tree has at most two children.

● Nodes that have no children are called leaf nodes, others are called internal nodes.

Page 78: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.9 Summary

● There is a single path between any pair of nodes in a binary tree.

● A binary tree is defined by the relative positions of the data in its nodes, and the tree as a whole carries a meaning that would change if the relative positions of the data in the tree were to change.

● The depth of a node in a binary tree is the number of branches (distance) from the root to that node.

Page 79: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.9 Summary

● Nodes at the same depth in a binary level are said to be at the same level.

● The height of a binary tree is the maximum level at which there is a node.

● A strictly binary tree is one in which every node has either no child or two children.

● In a complete binary tree, every level but the last must have the maximum number of nodes possible at that level.

Page 80: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.9 Summary

● The maximum possible number of nodes at level i in a binary tree is 2i.

● The maximum possible number of nodes in a binary tree of height h is 2h+1 – 1.

● If Nmax

is the maximum number of nodes in a binary tree, its height is log(N

max + 1) – 1.

● Recursive definition: A binary tree is either empty, or it consists of a special node called root that has a left subtree and a right subtree that are mutually disjoint binary trees.

Page 81: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.9 Summary

● The number of nodes in an empty binary tree is zero. Otherwise, the number of nodes is one plus the

number of nodes each in the left and right subtrees of the root.

● The height of an empty binary tree is -1. Otherwise, the height is one plus the maximum of

the heights of the left and right subtrees of the root.

Page 82: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.9 Summary

● Recursive definition of inorder traversal type T: first recursively traverse the Left subtree of T, then Visit the root of T, then recursively traverse the Right subtree of T.

● Recursive definition of preorder traversal of tree T: first Visit the root of T, then recursively traverse the Left subtree of T, then recursively traverse the Right subtree of T.

Page 83: Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way

9.9 Summary

● Recursive definition of postorder traversal of treeT: first recursively traverse the Left subtree of T, then recursively traverse the Right subtree of T, then Visit the root of T.

● The recursive traversals may be written in short form as follows: inorder is LVR, preorder is VLR, and postorder is LRV.

● Level-order traversal of treeT: starting at the root level, go level by level in T, visiting the nodes at any level in left to right order.