Printing a search tree in order

Slides:



Advertisements
Similar presentations
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms Post-order Traversal: Left Child - Right Child - Root Depth-First Search.
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:
Tree Traversal. Traversal Algorithms preorder inorder postorder.
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,
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
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.
Traversing a Binary Tree 10/23/ Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps:
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
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.
CPS 100, Spring Trees: no data structure lovelier?
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.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Tree Data Structures.
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.
Tree Traversal.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
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 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
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.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
From doubly-linked lists to binary trees
Lecture No.14 Data Structures Dr. Sohail Aslam
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
PFTFBH Binary Search trees Fundamental Data Structure
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Data Structures Binary Trees 1.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
What’s the Difference Here?
Tree.
Section 8.1 Trees.
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Binary Trees.
common code “abstracted out” same code written several times: don’t!
Principles of Computing – UFCFA3-30-1
Binary Tree Traversal Methods
CS-240 Dick Steflik D.J. Foreman
Section 9.3 by Andrew Watkins
2018, Fall Pusan National University Ki-Joune Li
Drawing Tree wijanarto.
ALLIGATOR © Copyright:
if the tree is empty, do nothing,
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
A Binary Tree is a tree in which each node has at most 2 children
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Printing a search tree in order When is root printed? After left subtree, before right subtree. void Visit(Node * t) { if (t != 0) Visit(t->left); cout << t->info << endl; Visit(t->right); } Inorder traversal “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe” “pig” “hippo” “leopard”

Tree traversals Different traversals useful in different contexts Inorder prints search tree in order Visit left-subtree, process root, visit right-subtree Preorder useful for reading/writing trees Process root, visit left-subtree, visit right-subtree Postorder useful for destroying trees Visit left-subtree, visit right-subtree, process root “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe”

Printing a tree in pre order When is root printed? before left subtree, before right subtree. void PreVisit(Node * t) { if (t != 0) cout << t->info << endl; PreVisit(t->left); PreVisit(t->right); } preorder traversal “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe” “pig” “hippo” “leopard”

How high is stack of calls (non-null t)? void PreVisit(Node * t) { if (t != 0) { cout << t->info << endl; PreVisit(t->left); PreVisit(t->right); } void PreVisit(Node * t) { if (t != 0) { cout << t->info << endl; PreVisit(t->left); PreVisit(t->right); } void PreVisit(Node * t) { if (t != 0) { cout << t->info << endl; PreVisit(t->left); PreVisit(t->right); } void PreVisit(Node * t) { if (t != 0) { cout << t->info << endl; PreVisit(t->left); PreVisit(t->right); } PreVisit(root) “jaguar” “elephant” “giraffe” “hippo” “leopard” void PreVisit(Node * t) { if (t != 0) { cout << t->info << endl; PreVisit(t->left); PreVisit(t->right); }

Printing a tree in post order When is root printed? after left subtree, after right subtree. void PostVisit(Node * t) { if (t != 0) PostVisit(t->left); PostVisit(t->right); cout << t->info << endl; } preorder traversal “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe” “pig” “hippo” “leopard”