Lecture No.14 Data Structures Dr. Sohail Aslam

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

4/17/2017 Section 9.3 Tree Traversal ch9.3.
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.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree Data Structures.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
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.
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.
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.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Saurav Karmakar
Chapter 12 – Data Structures
Data Structure and Algorithms
Expression Tree The inner nodes contain operators while leaf nodes contain operands. a c + b g * d e f Start of lecture 25.
Printing a search tree in order
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
Data Structures Lecture 22 Sohail Aslam.
Data Structures Binary Trees 1.
Binary Search Trees Chapter 7 Objectives
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Lecture No.15 Data Structures Dr. Sohail Aslam
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.
Section 8.1 Trees.
Chapter 20: Binary Trees.
Data Structures Lecture 27 Sohail Aslam.
Chapter 21: Binary Trees.
Binary Trees.
General Trees & Binary Trees
Abstract Data Structures
Trees.
Lecture No.16 Data Structures Dr. Sohail Aslam.
Binary Trees.
Binary Trees.
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Binary Tree Traversals
Operations on Binary Tree
Lecture No.20 Data Structures Dr. Sohail Aslam
Binary Tree Chapter 8 (cont’) Part2.
2018, Fall Pusan National University Ki-Joune Li
Data Structures Lecture 28 Sohail Aslam.
if the tree is empty, do nothing,
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
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:

Lecture No.14 Data Structures Dr. Sohail Aslam

Recursive Call Recall that a stack is used during function calls. The caller function places the arguments on the stack and passes control to the called function. Local variables are allocated storage on the call stack. Calling a function itself makes no difference as far as the call stack is concerned.

Stack Layout during a call Here is stack layout when function F calls function F (recursively): Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) sp sp End of lecture 13. sp At point of call During execution of F After call

Recursion: preorder 14 4 9 7 3 5 15 16 17 18 20 preorder(14) 14 ......preorder(null) ....preorder(9) 9 ......preorder(7) 7 ........preorder(5) 5 ..........preorder(null) ........preorder(null)

Recursion: preorder 14 4 15 3 9 18 7 16 20 5 17 ..preorder(15) 15 ....preorder(null) ....preorder(18) 18 ......preorder(16) 16 ........preorder(null) ........preorder(17) 17 ..........preorder(null) ......preorder(20) 20 4 15 3 9 18 7 16 20 5 17

Recursion: inorder 14 4 15 3 9 18 7 16 20 5 17 inorder(14) ......inorder(null) 3 4 ....inorder(9) ......inorder(7) ........inorder(5) ..........inorder(null) 5 7 ........inorder(null) 9 14 4 15 3 9 18 7 16 20 5 17

Recursion: inorder 14 4 15 3 9 18 7 16 20 5 17 ..inorder(15) ....inorder(null) 15 ....inorder(18) ......inorder(16) ........inorder(null) 16 ........inorder(17) ..........inorder(null) 17 18 ......inorder(20) 20 4 15 3 9 18 7 16 20 5 17

Non Recursive Traversal We can implement non-recursive versions of the preorder, inorder and postorder traversal by using an explicit stack. The stack will be used to store the tree nodes in the appropriate order. Here, for example, is the routine for inorder traversal that uses a stack.

Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack; TreeNode<int>* p; p = root; do while( p != NULL ) stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty 

Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack; TreeNode<int>* p; p = root; do while( p != NULL ) stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty 

Non Recursive Traversal void inorder(TreeNode<int>* root) { Stack<TreeNode<int>* > stack; TreeNode<int>* p; p = root; do while( p != NULL ) stack.push( p ); p = p->getLeft(); } // at this point, left tree is empty 

Non Recursive Traversal  if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << " "; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL );

Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << " "; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL ); 

Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << " "; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL ); 

Non Recursive Traversal if( !stack.empty() ) { p = stack.pop(); cout << *(p->getInfo()) << " "; // go back & traverse right subtree p = p->getRight(); } } while ( !stack.empty() || p != NULL ); 

Nonrecursive Inorder 14 4 9 7 3 5 15 16 17 18 20 push(14) ..push(4)

Traversal Trace recursive inorder nonrecursive inorder  inorder(14) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 

Traversal Trace recursive inorder nonrecursive inorder  inorder(14) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 

Traversal Trace recursive inorder nonrecursive inorder  inorder(14) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 

Traversal Trace recursive inorder nonrecursive inorder  inorder(14) 3 4 ..inorder(9) ....inorder(7) ......inorder(5) 5 7 9 14 inorder(15) 15 inorder(18) ..inorder(16) 16 ..inorder(17) 17 18 inorder(20) 20 nonrecursive inorder push(14) ..push(4) ....push(3) 3 4 ..push(9) ....push(7) ......push(5) 5 7 9 14 push(15) 15 push(18) ..push(16) 16 ..push(17) 17 18 push(20) 20 

Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left-to-right order.

Level-order Traversal 14 4 9 7 3 5 15 18 16 20 17 End of lecture 14 Level-order: 14 4 15 3 9 18 7 16 20 5 17