Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Advertisements

Trees. Terminology Trees are hierarchical –“parent-child” relationship A is the parent of B B is a child of A B and C are siblings Generalized to ancestor.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Marc Smith and Jim Ten Eyck
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 10-A Trees Modified
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Trees A non-linear implementation for collection classes.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
CS 302 Data Structures Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
Chapter 16 Tree Implementations
Chapter 20: Binary Trees.
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
Binary Trees.
Binary Trees.
Chapter 16 Tree Implementations
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees Chapter 10.
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
CSC 205 – Java Programming II
General Tree Concepts Binary Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved

Examples © 2011 Pearson Addison-Wesley. All rights reserved

Examples © 2011 Pearson Addison-Wesley. All rights reserved

Terminology Balanced binary trees A binary tree is balanced if the height of any node’s right subtree differs from the height of the node’s left subtree by no more than 1 Full binary trees are complete Complete binary trees are balanced © 2011 Pearson Addison-Wesley. All rights reserved

Examples © 2011 Pearson Addison-Wesley. All rights reserved

Terminology Summary of tree terminology General tree Parent of node n A set of one or more nodes, partitioned into a root node and subsets that are general subtrees of the root Parent of node n The node directly above node n in the tree Child of node n A node directly below node n in the tree Root The only node in the tree with no parent © 2011 Pearson Addison-Wesley. All rights reserved

Terminology Summary of tree terminology (Continued) Leaf Siblings A node with no children Siblings Nodes with a common parent Ancestor of node n A node on the path from the root to n Descendant of node n A node on a path from n to a leaf Subtree of node n A tree that consists of a child (if any) of n and the child’s descendants © 2011 Pearson Addison-Wesley. All rights reserved

Terminology Summary of tree terminology (Continued) Height Binary tree The number of edges longest path from the root to a leaf Binary tree A set of nodes that is either empty or partitioned into a root node and one or two subsets that are binary subtrees of the root Each node has at most two children, the left child and the right child Left (right) child of node n A node directly below and to the left (right) of node n in a binary tree © 2011 Pearson Addison-Wesley. All rights reserved

Terminology Summary of tree terminology (Continued) Left (right) subtree of node n In a binary tree, the left (right) child (if any) of node n plus its descendants Binary search tree A binary tree where the value in any node n is greater than the value in every node in n’s left subtree, but less than the value of every node in n’s right subtree Empty binary tree A binary tree with no nodes © 2011 Pearson Addison-Wesley. All rights reserved

Terminology Summary of tree terminology (Continued) Full binary tree A binary tree of height h with no missing nodes All leaves are at level h and all other nodes each have two children Complete binary tree A binary tree of height h that is full to level h – 1 and has level h filled in from left to right Balanced binary tree A binary tree in which the left and right subtrees of any node have heights that differ by at most 1 © 2011 Pearson Addison-Wesley. All rights reserved

The ADT Binary Tree: Basic Operations of the ADT Binary Tree The operations available for a particular ADT binary tree depend on the type of binary tree being implemented Basic operations of the ADT binary tree createBinaryTree() createBinaryTree(rootItem) makeEmpty() isEmpty() getRootItem() throws TreeException © 2011 Pearson Addison-Wesley. All rights reserved

General Operations of the ADT Binary Tree createBinaryTree (rootItem, leftTree, rightTree) setRootItem(newItem) attachLeft(newItem) throws TreeException attachRight(newItem) throws TreeException attachLeftSubtree(leftTree) throws TreeException attachRightSubtree(rightTree) throws TreeException detachLeftSubtree() throws TreeException detachRightSubtree() throws TreeException © 2011 Pearson Addison-Wesley. All rights reserved

Traversals of a Binary Tree A traversal algorithm for a binary tree visits each node in the tree Recursive traversal algorithms Preorder traversal ROOT - LEFT - RIGHT Inorder traversal LEFT – ROOT - RIGHT Postorder traversal LEFT - RIGHT - ROOT Traversal is O(n) © 2011 Pearson Addison-Wesley. All rights reserved

Example Traverse the following tree using Postorder, Preorder and Inorder traversals: Preorder ROOT - LEFT - RIGHT Inorder LEFT – ROOT - RIGHT Postorder LEFT - RIGHT - ROOT © 2011 Pearson Addison-Wesley. All rights reserved

Traversal of a Binary Tree Figure 11-10 Traversals of a binary tree: a) preorder; b) inorder; c) postorder © 2011 Pearson Addison-Wesley. All rights reserved

Tree traversals In java BinaryTree<Integer> tree = new BinaryTree<Integer>(70); tree.attachLeft(10); tree.attachLeft(80); TreeInterator<Integer> iter = new TreeInterator<Integer>(tree); Iter.setInorder(); while (iter.hasNext()){ System.out.println(iter.next()) } © 2011 Pearson Addison-Wesley. All rights reserved

Possible Representations of a Binary Tree An array-based representation A Java class is used to define a node in the tree A binary tree is represented by using an array of tree nodes Each tree node contains a data portion and two indexes (one for each of the node’s children) Requires the creation of a free list which keeps track of available nodes © 2011 Pearson Addison-Wesley. All rights reserved

Possible Representations of a Binary Tree Figure 11-11a a) A binary tree of names Figure 11-11b b) its array-based implementations © 2011 Pearson Addison-Wesley. All rights reserved

Possible Representations of a Binary Tree An array-based representation of a complete tree If the binary tree is complete and remains complete A memory-efficient array-based implementation can be used © 2011 Pearson Addison-Wesley. All rights reserved

Possible Representations of a Binary Tree Figure 11-12 Level-by-level numbering of a complete binary tree Figure 11-13 An array-based implementation of the complete binary tree in Figure 10-12 © 2011 Pearson Addison-Wesley. All rights reserved

Array-Based Binary Tree Example © 2011 Pearson Addison-Wesley. All rights reserved

Possible Representations of a Binary Tree A reference-based representation Java references can be used to link the nodes in the tree Figure 11-14 A reference-based implementation of a binary tree © 2011 Pearson Addison-Wesley. All rights reserved

A Reference-Based Implementation of the ADT Binary Search Tree TreeIterator Can be used with BinarySearchTree public class TreeNode(){ int item; TreeNode leftNode; TreeNode rightNode; public TreeNode(int newData) { item = newData; leftNode = Null; rightNode= NUll; } © 2011 Pearson Addison-Wesley. All rights reserved

A Reference-Based Implementation of the ADT Binary Tree Classes that provide a reference-based implementation for the ADT binary tree TreeNode Represents a node in a binary tree TreeException An exception class BinaryTreeBasis An abstract class of basic tree operation BinaryTree Provides the general operations of a binary tree Extends BinaryTreeBasis © 2011 Pearson Addison-Wesley. All rights reserved

Tree Traversals Using an Iterator TreeIterator Implements the Java Iterator interface Provides methods to set the iterator to the type of traversal desired Uses a queue to maintain the current traversal of the nodes in the tree Nonrecursive traversal (optional) An iterative method and an explicit stack can be used to mimic actions at a return from a recursive call to inorder © 2011 Pearson Addison-Wesley. All rights reserved

Tree traversals In java BinaryTree<Integer> tree = new BinaryTree<Integer>(70); tree.attachLeft(10); tree.attachRight(80); TreeIterator<Integer> iter = new TreeIterator<Integer>(tree); Iter.setInorder(); while (iter.hasNext()){ System.out.println(iter.next()) } © 2011 Pearson Addison-Wesley. All rights reserved