Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.

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, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
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,
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Topic 17 Introduction to Trees
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded 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.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
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.
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.
Trees Saurav Karmakar
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.
Fundamentals of Programming II Introduction to Trees
Data Structures Binary Trees 1.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees, Binary Search Trees
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Chapter 20: Binary Trees.
Binary Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures

2 Trees A recursively defined data structure – Each node has links to two or more subtrees/children-nodes. – Each node itself can be the beginning of a new tree. – Nodes with no children nodes are called leaves or leaf nodes. – The root node has no parent. Example class BinaryNode { private AnyType element; private BinaryNode left; private BinaryNode right; … }

Trees CSCI 3333 Data Structures3 Depth of a node: the length of the path from the root to the node. Height of a node: the length of the path from the node to the deepest leaf. Nodes in a tree are linked by edges. Sibling nodes: nodes with the same parent. Ancestor nodes vs descendent nodes The size of a node is the number of descendents plus itself.

More Tree Properties CSCI 3333 Data Structures4 The depth of any node is 1 more than the depth of its parent. The height of any node is 1 more than the height of its maximum-height child. The height of a tree is the height of the root node. The size of a tree is the size of its root. A tree with N nodes has N-1 edges. (Except for the root, each of the other nodes has one incoming edge.)

Implementation of Trees Each node is allocated space for data plus a number of references to other nodes (children, siblings). Exercise: Define the tree shown below as a class. Q: What’s the space allocation for each of the nodes in the following tree? CSCI 3333 Data Structures5

Implementation of Trees An alternative implementation Exercise: Define a class to represent such a tree. Q: What’s the space allocation for each of the nodes in this tree? CSCI 3333 Data Structures6

Applications of Trees Many applications in computing systems Typically a tree is used as an index to the stored data. Examples: – The file system directory/folder structure – a secondary index to a primary file – An index to a database – etc. CSCI 3333 Data Structures7

Binary Trees A tree in which no node has more than two children. Many applications: – expression trees (in compiler design), – Huffman coding tree (in data compression), – binary search tree – Priority queues CSCI 3333 Data Structures8

9 BinaryNode.java

CSCI 3333 Data Structures10 BinaryTree.java

CSCI 3333 Data Structures11

Recursion and Trees Most methods in trees are defined as recursive methods. Example: find the size of a tree CSCI 3333 Data Structures12

Recursion and Trees Example: find the height of a tree CSCI 3333 Data Structures13

Tree Traversal Preorder traversal: the node itself + the left child + the right child Postorder traversal: the left child + the right child + the node Inorder traversal: the left child + the node + the right child CSCI 3333 Data Structures14

CSCI 3333 Data Structures15

Tree Traversals and Stacks The task: Use a stack to maintain the states of a tree when traversing the nodes Example: postorder traversal Each node in the stack is associated with one of the three counters to represent its state: 1.If we are about to process the node’s left child 2.If we are about to process the node’s right child 3.If we are about to process the node itself Default counter value = 0; The counter value represents the number of times popped. CSCI 3333 Data Structures16

Example program: TestTreeIterators.java public static void testItr( String type, TreeIterator itr ) { try { System.out.print( type + ":" ); for( itr.first( ); itr.isValid( ); itr.advance( ) ) System.out.print( " " + itr.retrieve( ) ); System.out.println( ); itr.advance( ); } catch( NoSuchElementException e ) { System.out.println( e + " (as expected)" ); } } CSCI 3333 Data Structures17

CSCI 3333 Data Structures18

CSCI 3333 Data Structures19

CSCI 3333 Data Structures20

Tree Traversals using Stacks Example: postorder traversal CSCI 3333 Data Structures21

Tree Traversals using Stacks Exercise 18.4: Show the stack operations when a preorder traversal is applied to the tree shown in Figure Inorder traversal ? Lab 5. CSCI 3333 Data Structures22

Level-order traversal Nodes in the tree are processed starting at the root and going from top to bottom, left to right. – Level 0 nodes – Level 1 nodes – Level 2 nodes – … Exercise: What are the order of printed data if the following trees are level-order traversed? CSCI 3333 Data Structures23