© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Advertisements

EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 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.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
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
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
COSC2007 Data Structures II
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Course: Programming II - Abstract Data Types Slide Number 1 The ADT Binary Tree Definition The ADT Binary Tree is a finite set of nodes which is either.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
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.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
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)
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.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees Chapter 10.
Trees.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
General Tree Concepts Binary Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees

© 2006 Pearson Addison-Wesley. All rights reserved11 A-2 Indexed Data So far, data has always looked like this: –datum 1, datum 2, datum 3,.... Difference between ADTs is just how to get the data and how to add the data: –list: by index –stack: from top / to top –queue: from front / to back

© 2006 Pearson Addison-Wesley. All rights reserved11 A-3 Hard to Index Data It is not always easy to characterize data this way Consider: –John was born in 1993 –His dad was born in 1963, his mom in 1964 –His brother was born in 1999 –His uncle was born.....

© 2006 Pearson Addison-Wesley. All rights reserved11 A-4 Hard to Index Data Index based storage: –(John, 1993), (his dad, 1963),... Problem 1: –The order is meaningless Why is John first? Problem 2: –Where does new data go? Adding an uncle to the end of the list?

© 2006 Pearson Addison-Wesley. All rights reserved11 A-5 Non-Indexed Data Types Solution –Don’t store data in a list –Store it in a way that captures the “structure” of the data In this case, a family tree –Note: there is no “3 rd ” element or 10 th element, etc. –It is not-indexed

© 2006 Pearson Addison-Wesley. All rights reserved11 A-6 Terminology Definition of a general tree –A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called subtrees of r Definition of a binary tree –A binary tree is a set T of nodes such that either T is empty, or T is partitioned into three disjoint subsets: –A single node r, the root –Two possibly empty sets that are binary trees, called left and right subtrees of r

© 2006 Pearson Addison-Wesley. All rights reserved11 A-7 Terminology Figure 11-4 Binary trees that represent algebraic expressions

© 2006 Pearson Addison-Wesley. All rights reserved11 A-8 Terminology A binary search tree –A binary tree that has the following properties for each node n n’s value is greater than all values in its left subtree T L n’s value is less than all values in its right subtree T R Both T L and T R are binary search trees Figure 11-5 A binary search tree of names

© 2006 Pearson Addison-Wesley. All rights reserved11 A-9 Terminology The height of trees –Level of a node n in a tree T If n is the root of T, it is at level 1 If n is not the root of T, its level is 1 greater than the level of its parent –Height of a tree T defined in terms of the levels of its nodes If T is empty, its height is 0 If T is not empty, its height is equal to the maximum level of its nodes

© 2006 Pearson Addison-Wesley. All rights reserved11 A-10 Terminology Figure 11-6 Binary trees with the same nodes but different heights

© 2006 Pearson Addison-Wesley. All rights reserved11 A-11 Terminology Full, complete, and balanced binary trees –Recursive definition of a full binary tree If T is empty, T is a full binary tree of height 0 If T is not empty and has height h > 0, T is a full binary tree if its root’s subtrees are both full binary trees of height h – 1 Figure 11-7 A full binary tree of height 3

© 2006 Pearson Addison-Wesley. All rights reserved11 A-12 Terminology Complete binary trees –A binary tree T of height h is complete if All nodes at level h – 2 and above have two children each, and When a node at level h – 1 has children, all nodes to its left at the same level have two children each, and When a node at level h – 1 has one child, it is a left child Figure 11-8 A complete binary tree

© 2006 Pearson Addison-Wesley. All rights reserved11 A-13 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-14 Terminology Summary of tree terminology –General tree 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-15 Terminology Summary of tree terminology (Continued) –Leaf 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-16 Terminology Summary of tree terminology (Continued) –Height The number of nodes on the 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-17 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-18 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-19 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-20 General Operations of the ADT Binary Tree 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

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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-22 Traversal of a Binary Tree Figure Traversals of a binary tree: a) preorder; b) inorder; c) postorder

© 2006 Pearson Addison-Wesley. All rights reserved11 A-23 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

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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-25 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-26 Possible Representations of a Binary Tree Figure An array-based implementation of the complete binary tree in Figure Figure Level-by-level numbering of a complete binary tree

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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-28 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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-29 Example Pre-order, inorder, post-order? M L JIH E FG D BC A

© 2006 Pearson Addison-Wesley. All rights reserved11 A-30 Example Pre-order: A, B, D, H, I, E, J, C, F, L, M, G M L JIH E FG D BC A

© 2006 Pearson Addison-Wesley. All rights reserved11 A-31 Example In-order: H, D, I, B, E, J, A, M, L, F, C, G M L JIH E FG D BC A

© 2006 Pearson Addison-Wesley. All rights reserved11 A-32 Example Post-order: H, I, D, J, E, B, M, L, F, G, C, A M L JIH E FG D BC A

© 2006 Pearson Addison-Wesley. All rights reserved11 A-33 Example Draw the binary tree t3: BinaryTree t1 = new BinaryTree (7); t1.attachLeft(1); t1.attachRight(3); BinaryTree t2 = new BinaryTree (1); t2.setRootItem(6); BinaryTree t3 = new BinaryTree(2, t1, t2);

© 2006 Pearson Addison-Wesley. All rights reserved11 A-34 Example 7 t t2 6 2 t3

© 2006 Pearson Addison-Wesley. All rights reserved11 A-35 Example What is the number of leafs in a full binary tree with height n? Prove by induction.

© 2006 Pearson Addison-Wesley. All rights reserved11 A-36 Example looks like 2 height-1

© 2006 Pearson Addison-Wesley. All rights reserved11 A-37 Example Let n be the height of the tree. Base case (n=1) Leaves(1) = 1 = 2 0 Inductive hypothesis: Leaves(n-1) = 2 n-2 Let T have height n. Number of leaves is 2*Leaves(n-1) = 2 * 2 n-2 = 2 n-1