Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
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.
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
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
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.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Chapter 10-A Trees Modified
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 16 Binary.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Starting at Binary Trees
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures TREES.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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 K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview ● 10.1 – Introduces tree terminology – Discusses the implementation of binary trees ● 10.2 – Traversing trees ● 10.3 – General trees

Binary Trees ● Binary tree – Empty or a node with a left and right subtree. – Each of these subtrees is itself a binary tree. ● Children – The nodes directly below a node – In a binary tree, no node can have more than two children. ● Parent – Every node in a binary tree has exactly one parent, except for one a the top (root: no parent). ● Siblings: Nodes with the same parent. ● Root: Node at the top of a tree.

Binary Trees ● Leaves: Nodes with no children. ● Internal nodes: Nodes that are not leaves. ● Depth: – Number of lines along the path back to the root. – Root is a depth 0. ● Level – A level of the tree is the set of nodes at a particular depth. ● Height – The height of a tree is the depth of the deepest node.

Binary Trees

● Descendants – A node's descendants are itself, its children, their children, and so on. ● Every node is a descendant of the root. ● Proper descendants – All of it descendants except itself. ● Ancestors – Itself, its parents, its grandparent, and so on. ● Proper ancestors – All of its ancestors except itself.

Binary Trees ● The number of nodes in a binary tree depends on the height of the tree and on how “skinny” or “busy” the tree is. – Linear tree ● Every internal node has only one child. – Perfect or Complete ● All of the leaves are at the same depth. ● Every internal node has exactly two children.

Binary Trees The number of nodes in a binary tree of height h can be anywhere between h + 1 (for a linear tree) and ● A binary tree with n nodes has height h between log 2 (n + 1) – 1 (for a perfect binary tree) and n-1 (for a linear tree) ● h Θ (log n) for a perfect binary tree. (for a perfect binary tree)

Binary Trees

Binary Tree Program package Tutorial9; import java.util.*; public class BinaryTree { private Object value; //instance variables private BinaryTree left = null; private BinaryTree right = null; public BinaryTree(Object o, BinaryTree l, BinaryTree r) { //constructor value = o; left = l; right = r; }

Binary Tree Program (cont.) // The Vector class implements a growable array of objects. // The Vector class is similar to ArrayList class public Vector getPreorder() { Vector vec = new Vector(); return traversePreorder( this, vec ); } public Vector getInorder() { Vector vec = new Vector(); return traverseInorder( this, vec ); } public Vector getPostorder() { Vector vec = new Vector(); return traversePostorder( this, vec ); }

Binary Tree Program (cont.) private Vector traversePreorder(BinaryTree b, Vector v) { if ( b != null ) { v.addElement( b.value ); // root traversePreorder( b.left, v ); // left traversePreorder( b.right, v ); // right } return v; } private Vector traverseInorder(BinaryTree b, Vector v) { if ( b != null ) { traverseInorder( b.left, v ); // left v.addElement( b.value ); // root traverseInorder( b.right, v ); // right } return v; }

Binary Tree Program (cont.) private Vector traversePostorder(BinaryTree b, Vector v) { if ( b != null ) { traversePostorder( b.left, v ); // left traversePostorder( b.right, v ); // right v.addElement( b.value ); // root } return v; }

Binary Tree Program (cont.) public static void main ( String [] args ) { // construct the binary tree BinaryTree l = new BinaryTree( "D", null, null ); BinaryTree r = new BinaryTree( "E", null, null ); l4 = new BinaryTree( "B", l, r ); BinaryTree l2 = new BinaryTree( "H", null, null ); BinaryTree r2 = new BinaryTree( "I", null, null ); l3 = new BinaryTree( "F", l2, r2 ); r3 = new BinaryTree( "G", null, null ); r4 = new BinaryTree( "C", l3, r3 ); BinaryTree root = new BinaryTree( "A", l4, r4 ); // traverse the binary tree Vector v1 = root.getPreorder(); System.out.println( "Preorder Traverse: " +v1 ); Vector v2 = root.getInorder(); System.out.println( "Inorder Traverse: " +v2 ); Vector v3 = root.getPostorder(); System.out.println( "Postorder Traverse: " +v3 ); } // end of main metohd } // end of binary tree program

Binary Tree Traversal Outputs (from the previous program) A B C D E FG H I Inorder output (v2): Preorder output (v1): Postorder output (v3): ABDECFHIG DBEAHFICG DEBHIFGCA

Tree Traversal ● To output the contents in a binary tree ● Four meaningful orders in which to traverse a binary tree. – Preorder – Inorder – Postorder – Level order

Tree Traversal

Tree Traversal (Preorder)

Tree Traversal (Inorder)

Tree Traversal (Postorder)

Tree Traversal (Preorder, using stack) ● Using the stack to do Preorder traversal iteratively (not recursively) ● Note, we have to push the right child before left one, because of the last-in, first-out policy of a stack.

Tree Traversal (Levelorder, using queue) ● Using the queue to do Levelorder (i.e. breadth-first) traversal iteratively. ● Note, we add the left child before right one, because of the first-in, first-out policy of a queue.

Tree Traversal ● Level order traversal is sometimes called breadth-first. ● The other traversals are called depth-first. ● Traversal takes Θ (n) time in both breadth-first and depth-first. ● Memory usage in a perfect tree is Θ (log n) in depth-first and Θ (n) in breadth-first traversal.

Tree Traversal

Using ArrayQueue to Store Binary Tree index nodeABCDEFGHIJKL Left- child index Right- child index

General Trees ● General trees differ from binary trees in three ways: – A node in a general tree may have more than two children. – A node in a general tree has a (possibly empty) sequence of children, rather than a certain number of “slots” to fill. ● In binary trees – a tree with a left subtree but no right subtree – a tree with a right subtree but no left subtree. ● No such distinction is made for general trees – General trees cannot be empty. This restriction is made to avoid having to distinguish between a node with no subtrees and a node with several empty subtrees, which would be drawn identically.

General Trees ● Inheritance diagrams showing the relationships between classes is a general tree.

General Trees ● Simplest is to represent each node as an item. – Array of children or list of children. – First-child, next-sibling ● A less intuitive but more space-efficient representation has each node keeping track of its first child and its next sibling.

General Trees

● How do we decide which is the best move to make?

General Trees ● If it is not possible to win in a single move, consider how the opponent might reply. ● The value of each leaf is determined by score(). ● The value of an internal node is determined by taking either the minimum (if the node represents a board with O to play) or the maximum (X to play) of the node's children. – Minimax algorithm ● Considering all possible moves out to the end of the game.

General Trees

● Once we determine the value of a child, we no longer need the subtree rooted at that child. ● We invoke a method which determines the value of the root by determining the value of its descendants.

General Trees ● The structure of minimaxForO() is almost identical to that of playBestMove(). – Differences: ● It returns the value of the board on which it is invoked, rather than making the best move. ● It looks for the move leading to the minimum score, rather than the maximum. ● It invokes minimaxForX() instead of score().

General Trees

Summary ● A tree is a branching, hierarchical structure. – A binary tree either is empty or consists of a node, a left subtree, and a right subtree. – A general tree (which cannot be empty) consists of a node and zero or more subtrees. ● In the widest possible binary tree, called a perfect tree, the number of nodes is exponential in the height of the tree. ● The height of such a tree is logarithmic in the number of nodes.

Summary ● Binary trees can be traversed – Preorder – Inorder – Postorder – Level order ● The first three have very elegant recursive algorithms, but level order traversal requires a queue.

Summary ● A binary tree is usually represented by a linked structure. ● General trees, presentations include the array of children representation and the first-child, next-sibling representation. ● Trees may not explicitly be constructed as data structures, but are implicit in the way the program runs.

Chapter 10 Self-Study Homework ● Pages: ● Exercises: 10.1, 10.2, 10.6, 10.7