Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element,

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
CS 171: Introduction to Computer Science II
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
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.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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 ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Tree Traversal.
Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH.
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.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Discrete Structures Trees (Ch. 11)
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Lab 4 Due date: March 29. Linked Representation Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
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. 
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Chapter 15.
Paul Tymann and Andrew Watkins
Trees Another Abstract Data Type (ADT)
Tree.
Algorithms and Data Structures Recursion and Binary Tree
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Section 8.1 Trees.
CSC 172– Data Structures and Algorithms
עצים root הגדרת עץ חופשי: גרף לא מכוון ללא מעגלים
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Binary Trees.
Paul Tymann, Andrew Watkins,
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
CS-240 Dick Steflik D.J. Foreman
Section 9.3 by Andrew Watkins
Paul Tymann, Andrew Watkins,
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

Binary Tree Traversal Methods Preorder Inorder Postorder Level order

Preorder Traversal template void PreOrder(TreeNode *t) { if (t != NULL) { Visit(t); PreOrder(t->leftChild); PreOrder(t->rightChild); }

Preorder Example (Visit = print) a bc abc

a bc d e f g hi j abdgheicfj

Preorder Of Expression Tree + a b - c d + e f * / Gives prefix form of expression! /*+ab-cd+ef

Inorder Traversal template void InOrder(TreeNode *t) { if (t != NULL) { InOrder(t->leftChild); Visit(t); InOrder(t->rightChild); }

Inorder Example (Visit = print) a bc bac

a bc d e f g hi j gdhbeiafjc

Inorder By Projection (Squishing) a bc d e f g hi j gdhbeia f jc

Inorder Of Expression Tree + a b - c d + e f * / Gives infix form of expression (sans parentheses)! ea+b*cd/+f-

Postorder Traversal template void PostOrder(TreeNode *t) { if (t != NULL) { PostOrder(t->leftChild); PostOrder(t->rightChild); Visit(t); }

Postorder Example (Visit = print) a bc bca

a bc d e f g hi j ghdiebjfca

Postorder Of Expression Tree + a b - c d + e f * / Gives postfix form of expression! ab+cd-*ef+/

Traversal Applications a bc d e f g hi j Make a clone. Determine height. Determine number of nodes.

Level Order Let t be the tree root. while (t != NULL) { visit t and put its children on a FIFO queue; if FIFO queue is empty, set t = NULL; otherwise, pop a node from the FIFO queue and call it t; }

Level-Order Example (Visit = print) a bc d e f g hi j abcdefghij

Binary Tree Construction Suppose that the elements in a binary tree are distinct. Can you construct the binary tree from which a given traversal sequence came? When a traversal sequence has more than one element, the binary tree is not uniquely defined. Therefore, the tree from which the sequence was obtained cannot be reconstructed uniquely.

Some Examples preorder = ab a b a b inorder = ab b a a b postorder = ab b a b a level order = ab a b a b

Binary Tree Construction Can you construct the binary tree, given two traversal sequences? Depends on which two sequences are given.

Preorder And Postorder preorder = ab a b a b postorder = ba Preorder and postorder do not uniquely define a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example).

Inorder And Preorder inorder = g d h b e i a f j c preorder = a b d g h e i c f j Scan the preorder left to right using the inorder to separate left and right subtrees. a is the root of the tree; gdhbei are in the left subtree; fjc are in the right subtree. a gdhbeifjc

Inorder And Preorder preorder = a b d g h e i c f j b is the next root; gdh are in the left subtree; ei are in the right subtree. a gdhbeifjc a gdh fjc b ei

Inorder And Preorder preorder = a b d g h e i c f j d is the next root; g is in the left subtree; h is in the right subtree. a gdh fjc b ei a g fjcb eid h

Inorder And Postorder Scan postorder from right to left using inorder to separate left and right subtrees. inorder = g d h b e i a f j c postorder = g h d i e b j f c a Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

Inorder And Level Order Scan level order from left to right using inorder to separate left and right subtrees. inorder = g d h b e i a f j c level order = a b c d e f g h i j Tree root is a; gdhbei are in left subtree; fjc are in right subtree.