Tree Traversal.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.
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 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,
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.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Search Trees Data Structures Ananda Gunawardena
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Trees ---- Soujanya.
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees.
Tree.
Section 8.1 Trees.
CSC 172– Data Structures and Algorithms
Binary Trees, Binary Search Trees
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Tree data structure.
Data Structures and Algorithms for Information Processing
Binary Trees.
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Binary Tree Traversal Methods
Drawing Tree wijanarto.
Binary Trees, Binary Search Trees
if the tree is empty, do nothing,
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Trees.
Tree traversals BST properties Search Insertion
Binary Tree Implementation And Applications
Binary Trees, Binary Search Trees
Presentation transcript:

Tree Traversal

Pre Order Tree Traversal Visit the root Traverse the left sub-tree, Traverse the right sub-tree

In Order Tree Traversal Traverse the left sub-tree, Visit the root Traverse the right sub-tree

Post Order Tree Traversal Traverse the left sub-tree, Traverse the right sub-tree Visit the root

Post Order Tree Traversal A*(((B+C)*(D*E))+F) A B C + D E * * F + *

Tree Traversal

Example Print the pre, in and post order form of the following tree DAICBHEGF – in HBDIACEFG – pre ACIDBGFEH – post H E B F D I G A C

Example class Node { private Object value; private Node left, right; public Node (Object value) { this.value = value; } ...

Example class Tree { private Node root; public Tree () { root = null; } ...

In Order traversal public void inOrder () { inOrder (root); } private void inOrder (Node current) { if (current != null) { inOrder(current.getLeft()); visit(current); inOrder(current.getRight());

Pre Order traversal public void preOrder () { preOrder (root); } private void preOrder (Node current) { if (current != null) { visit(current); preOrder(current.getLeft()); preOrder(current.getRight());

Post Order traversal public void postOrder () { postOrder (root); } private void postOrder (Node current) { if (current != null) { postOrder(current.getLeft()); postOrder(current.getRight()); visit(current);

Example Can we reconstruct a tree from it’s InOrder traversal ? Are there two different trees with the same InOrder traversal What about pre and post order ? D A

Example Can we reconstruct a tree given both it’s in order and pre order traversals ? in order - DAICBHEGF pre order - HBDIACEFG (find the root) Left sub tree DAICB BDIAC Right sub tree EGF EFG

Reconstructing a binary tree ReconstructTree (inOrder in[] preOrder pre[]) if (n==0) return null root (T)  pre[1] find index i s.t in[i] = pre[1] left(T)  Reconstruct (in[1…i-1], pre [2…i]) right(T)  Reconstruct (in[i+1..n], pre [i+1…n]) return T

Reconstructing a binary tree Time complexity This is the same recurrence relation of quick sort

Binary Search Trees In a BST the in order output is a sorted list of the tree nodes. If we obtain a pre order path of a BST, we can sort the nodes and use the output to reconstruct the tree using the previous algorithm

Reconstructing a binary tree ReconstructTree (inOrder in[] postOrder post[]) if (n==0) return null root (T)  post[n] find index i s.t in[i] = post[n] left(T)  Reconstruct (in[1…i-1], post [1…i-1]) right(T)  Reconstruct (in[i+1..n], post[i…n-1]) return T

Level traversal (BFS) public void BFS () { Queue q = new Queue (); if {root != null) q.enqueue (root); while (! q.isEmpty()) { Node current = (Node)queue.dequeue(); visit (current); if (current.getLeft() != null) q.enqueue(current.getLeft()); if (current.getRight() != null) q.enqueue(current.getRight()); }

Depth private int depth (Node node) { if (current == null) { return 0; } else { return Math.max(depth(node.getLeft()), depth(node.getRight())) + 1;

private void printLevel (Node current, int level) { if (current != null) { if (level == 0) { visit (current); } else { printLevel (current.getLeft(), level - 1); printLevel (current.getRight(), level - 1); public void printAllLevels () { int depth = depth (root); for (int i = 0; i < depth; i++); printLevel(root, i);

Questions 1. Draw a single Binary Tree such that each node contains a single character and:   a.  The pre-order traversal results in EXAMFUN b.  The in-order traversal results in MAFXUEN

Questions 2. An in-order traversal of a Binary search tree yields a sorted list of elements in O(n) time. Does this fact contradict our comparison-based lower bound of nlogn time? Can something similar be done using a binary heap?

Questions 3. Is there a heap T storing seven distinct elements such that a preorder traversal of T yields the elements of T in sorted order? How about an in-order traversal? How about a post-order traversal? How about a post-order traversal to yield the elements in reverse sorted order?

Questions 4. Let T be a binary search tree with more than a single node. Is it possible that the preorder traversal of T visits the nodes in the same order as the post-order traversal of T? If so, give an example; otherwise, argue why this cannot occur. Likewise, is it possible that the preorder traversal of T visits the nodes in the reverse order of the post-order traversal of T? If so, give an example; otherwise, argue why this cannot occur.

Questions 5. Given the following binary tree, print out the order of the nodes for an in-order traversal, preorder traversal, and post-order traversal.   / (division operator) / \ / \ - * / \ / \ / \ 2 a ^ * / \ / \ b 2 * c / \ 4 a

Questions 6. Write a method that given a TreeNode N , returned the number of elements contained at the tree rooted by N.