Trees CS 105. 10/02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Trees COMP53 Oct 31, What is a Tree? A tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation.
Trees1 Part-C Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
COMP20010: Algorithms and Imperative Programming Lecture 1 Trees.
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.
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.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Lecture11: Tree I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Data Structures TREES.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2013 Goodrich, Tamassia, Goldwasser.
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.
CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
(c) University of Washington20-1 CSC 143 Java Trees.
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.
Trees 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
University of Palestine
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
Trees CSE 373 Data Structures.
Trees.
Week nine-ten: Trees Trees.
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
General Trees & Binary Trees
Binary Trees, Binary Search Trees
CSC 143 Java Trees.
CS210- Lecture 9 June 20, 2005 Announcements
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

Trees CS 105

10/02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations include accessing the parent or children of a given node A tree T is a set of nodes such that there is a distinguished node r (called the root) of T that has no parent each node v of T except r has a parent node

10/02/05 L7: Trees Slide 3 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Visualizing a Tree N AP T M I O R G Root Child

10/02/05 L7: Trees Slide 4 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Sample Uses A company’s organizational structure Family tree The Java class hierarchy O/S directory structure Book (parts, chapters, sections) Arithmetic expressions Web page links (?)

10/02/05 L7: Trees Slide 5 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Tree Terminology Root the only node that has no parent Leaf (External node) node that has no children Internal node node that has at least one child Siblings nodes that have a common parent

10/02/05 L7: Trees Slide 6 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved More Tree Terminology Ancestor recursive definition: ancestors of a node v are v itself and the ancestors of its parent proper ancestors: ancestors excluding itself Descendant v is a descendant of u if u is an ancestor of v Subtree of T rooted at v set of all descendants of v

10/02/05 L7: Trees Slide 7 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Even More Terminology Ordered Tree children of a node have a strict linear order Binary Tree an ordered tree where nodes have at most two children (left and right) Depth and Height of a node depth: distance from node to root height: from node to its farthest descendant

10/02/05 L7: Trees Slide 8 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Tree Implementations Array-based implementation elements (or references to them) are stored in an array parent-child relationships derived from indices Linked implementation elements stored in nodes nodes contain pointers to parent and children

10/02/05 L7: Trees Slide 9 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Tree Operations Get the root node of the tree Go to parent or children from a given node Add a root to an empty tree Add a child to a node Remove a node (can impose that the node be a leaf, for simplicity) Get the element associated to a node Replace the element associated to a node Others …

10/02/05 L7: Trees Slide 10 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree operations For a binary tree, a node has at most two children and are ordered Could distinguish between the left and right child of a node Implementations are simpler for binary trees but many concepts apply to general trees

10/02/05 L7: Trees Slide 11 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree methods size(): returns number of nodes in tree isEmpty(): check if tree is empty isInternal(v): check if node v is an internal node isExternal(v): check if node v is a leaf isRoot(v): check if node v is the root hasLeft(v): check if node v has a left child hasRight(v): check if node v has a right child

10/02/05 L7: Trees Slide 12 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree methods root(): returns the root node parent(v): return the parent of node v left(v): return the left child of node v right(v): return the right child of node v sibling(v): return the only sibling of v getElement(v): return the element associated to node v

10/02/05 L7: Trees Slide 13 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree methods replace(v,e): set element value of node v to e addRoot(e): add a root node to an empty tree containing element e insertLeft(v,e): add left child containing element e to node v insertRight(v,e): add right child containing element e to node v remove(v): remove node v, return current element in that node

10/02/05 L7: Trees Slide 14 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Implementation considerations Distinguish nodes (or positions) from the elements they contain For array implementation, nodes are referred to by integers For linked implementation, there are explicit node objects Exceptions Non-existent node for check, traversal, update, and removal operations Previously existing node for insertion operation

10/02/05 L7: Trees Slide 15 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Array limitations Capacity restrictions Disproportional space required when tree is skewed Consider a tree where most of its nodes have right children and no left children Not really easier to implement Choose array implementation only when tree is complete or nearly complete (all levels are filled up or almost filled up with nodes) Technique not as practical for non-binary trees

10/02/05 L7: Trees Slide 16 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Traversals Traversal systematic way of accessing or visiting the nodes of a tree Preorder visit root first, then traverse its subtrees (recursive) Postorder traverse subtrees first (recursive), then root Inorder (for binary trees) visit left subtree, then root, then right subtree

10/02/05 L7: Trees Slide 17 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Preorder Traversal Algorithm preorder( T, v ) Input: Tree T, Node v Output: Depends on application perform the “visit” action for node v // example: print T.getElement(v) for each child w of v do preorder( T, w )

10/02/05 L7: Trees Slide 18 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Postorder Traversal Algorithm postorder( T, v ) Input: Tree T, Node v Output: Depends on application for each child w of v do postorder( T, w) perform the “visit” action for node v // example: print T.getElement(v)

10/02/05 L7: Trees Slide 19 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Traversals Example Paper Chap 2.3 Chap 1 Chap 2.2 Chap 1.2Chap 2.1Chap 1.1 Chap 2 TitleAbstract References Chap 3.2Chap 3.1 Chap 3

10/02/05 L7: Trees Slide 20 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Preorder Traversal Paper Chap 2.3 Chap 1 Chap 2.2 Chap 1.2Chap 2.1Chap 1.1 Chap 2 TitleAbstract References Chap 3.2Chap 3.1 Chap 3 Paper, Title, Abstract, Chap 1, Chap 1.1, Chap 1.2, Chap 2, …, Chap 3.2, References preorder( t, t.root() )

10/02/05 L7: Trees Slide 21 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Paper Chap 2.3 Chap 1 Chap 2.2 Chap 1.2Chap 2.1Chap 1.1 Chap 2 TitleAbstract References Chap 3.2Chap 3.1 Chap 3 Postorder Traversal Title, Abstract, Chap 1.1, Chap 1.2, Chap 1, Chap 2.1, …, Chap 3, References, Paper postorder( t, t.root() )

10/02/05 L7: Trees Slide 22 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree Preorder Traversal Algorithm preorder( T, v ) Input: Binary Tree T, Node v Output: Depends on application perform the “visit” action for node v // example: print T.getElement( v ) if T.hasLeft( v ) then preorder ( T, T.left( v ) ) if T.hasRight( v ) then preorder ( T, T.right( v ) )

10/02/05 L7: Trees Slide 23 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree Postorder Traversal Algorithm postorder( T, v ) Input: Binary Tree T, Node v Output: Depends on application if T.hasLeft( v ) then postorder ( T, T.left( v ) ) if T.hasRight( v ) then postorder ( T, T.right( v ) ) perform the “visit” action for node v // example: print T.getElement(v)

10/02/05 L7: Trees Slide 24 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree Inorder Traversal Algorithm inorder( T, v ) Input: Binary Tree T, Node v Output: Depends on application if T.hasLeft( v ) then inorder ( T, T.left( v ) ) perform the “visit” action for node v // example: print T.getElement(v) if T.hasRight( v ) then inorder ( T, T.right( v ) )

10/02/05 L7: Trees Slide 25 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Inorder Traversal Example - 3x / 8 13 ( (3 X 1) / 3 ) - 8 inorder( t, t.root() )

10/02/05 L7: Trees Slide 26 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Programming considerations When programming stacks and queues in Java, we started with an interface as a way to standardize the different implementations Array and linked implementations of trees pose an interesting challenge For trees, the concept of a node or position needs to be exposed Arrays use ints to denote positions, the linked implementation uses BTNode objects We need a way to standardize positions so that ints and BTNodes are both applicable

10/02/05 L7: Trees Slide 27 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Tree positions Consider the signature of the insertLeft method for a binary tree For ArrayBinaryTree public int insertLeft( int v, Object o ) For LinkedBinaryTree public BTNode insertLeft( BTNode v, Object o ) If we had a BinaryTree interface, how do we declare this method? public ??? insertLeft( ??? v, Object o );

10/02/05 L7: Trees Slide 28 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved The Position interface Position IndexBTNode public class BTNode implements Position { // define as before } public interface Position { // empty interface // or have a getElement() method } public class Index implements Position { private int indexValue; public Index( int val ) { indexValue = val; } public int getIndex() { return indexValue; } }

10/02/05 L7: Trees Slide 29 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved The BinaryTree interface BinaryTree ArrayBinaryTreeLinkedBinaryTree public interface BinaryTree { public int size(); public boolean isEmpty(); public boolean isInternal( Position p ); … public Position root(); public Position left( Position p ); … public Object getElement( Position p ); … public Position insertLeft( Position p, Object e ); … }

10/02/05 L7: Trees Slide 30 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Impact on implementing classes BTNode and LinkedBinaryTree require little revision For ArrayBinaryTree, there is a need to revise the methods so that positions are Index objects instead of ints Example: public Index left( Index v ) { int num = v.getIndex(); checkOccupiedPosition( num ); checkPosition( 2*num + 1 ); return new Index( 2*num + 1 ); } actually, these types should be Position, not Index

10/02/05 L7: Trees Slide 31 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved About trees in general Concepts discussed so far also apply to general (non-binary) trees Array implementation possible but less practical Need to impose a maximum on number of children so that arithmetic on ints still computes positions of parent and children Linked implementation In a TNode class, there is an array or list of TNodes to represent children