Chapter 24
Chapter 24
5th Edition
Chapter 24
Trees
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Hierarchical Organizations
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary trees
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Trees
FIGURE 24-8 The number of nodes in a full binary tree as a function of the tree’s height
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Tree Height (Part 2)
FIGURE 24-8 The number of nodes in a full binary tree as a function of the tree’s height
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Traversals of A Tree
• Traversal:
– Visit, or process, each data item exactly once
• We will say that traversal can pass through a node without
visiting it at that moment.
• Order in which we visit items is not unique
• Traversals of a binary tree are somewhat easy to understand
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Traversals of a Binary Tree
• We use recursion
• To visit all the nodes in a binary tree, we must
– Visit the root
– Visit all the nodes in the root’s left subtree
– Visit all the nodes in the root’s right subtree
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Traversals of a Binary Tree
• Preorder traversal
– Visit root before we visit root’s subtrees
• Inorder traversal
– Visit root of a binary tree between visiting nodes in
root’s subtrees.
• Postorder traversal
– Visit root of a binary tree after visiting nodes in root’s
subtrees
• Level-order traversal
– Begin at root and visit nodes one level at a time
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Traversals of a Binary Tree
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Traversals of a General Tree
package TreePackage;
/** An interface of basic methods for the ADT tree. */
public interface TreeInterface<T>
{
public T getRootData();
public int getHeight();
public int getNumberOfNodes();
public boolean isEmpty();
public void clear();
} // end TreeInterface
Java statements that build a tree and then display some of its characteristics:
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Expression Trees
FIGURE 24-20 Two binary search trees containing the same data as
the tree in Figure 24-19
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Binary Search Tree
Algorithm bstSearch(binarySearchTree, desiredObject)
// Searches a binary search tree for a given object.
//Returns true if the object is found.
if (binarySearchTree is empty)
return false
else if (desiredObject == object in the root of binarySearchTree)
return true
else if (desiredObject < object in the root of binarySearchTree)
return bstSearch(left subtree of binarySearchTree, desiredObject)
else
return bstSearch(right subtree of binarySearchTree, desiredObject)
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Heaps
• Complete binary tree whose nodes contain Comparable
objects and are organized as follows:
– Each node contains an object no smaller/larger than
objects in its descendants
– Maxheap: object in node greater than or equal to its
descendant objects
– Minheap: object in node less than or equal to its
descendant objects
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Heaps
Chapter 24
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved