IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.

Slides:



Advertisements
Similar presentations
Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Computer Science C++ High School Level By Guillermo Moreno.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Tuesday, 11/13/01 Data Structures Makeup Lecture Linked Lists.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015.
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.
1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree.
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Trees and Recursive Algorithms. Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F?
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CSED101 INTRODUCTION TO COMPUTING TREE 2 Hwanjo Yu.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Binary Search Trees (BST) Let’s look at some pics …and some code.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Recursive Objects (Part 4)
Data Structures and Algorithms
Data Structures and Algorithms
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Data Structures and Algorithms
Tree.
Data Structures & Algorithm Design
Data Structures and Algorithms
Data Structures and Algorithms
Chapter 20: Binary Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Data Structures and Algorithms
Chapter 21: Binary Trees.
Tree data structure.
Trees.
Tree data structure.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
2018, Fall Pusan National University Ki-Joune Li
if the tree is empty, do nothing,
Chapter 20: Binary Trees.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Data Structures Using C++ 2E
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Presentation transcript:

IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Binary Tree (Program) 1Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

IIT Bombay Data Structures Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay2 Structure ‘Node’: Represent each node as Data Left Index Right Index Vector ‘bt’: Represents binary tree It is of type structure ‘Node’

IIT Bombay Functions Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay3 Function 1: Insert Inserts the data by calling respective functions Making a node (If tree is empty) At left of the parent At right of the parent

IIT Bombay Functions Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay4 Function 2: makeNode Inserts data at the root Function 3: insertLeft Inserts data at the left of the parent Function 4: insertRight Inserts data at the right of the parent

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay5 struct Node{ int data; int leftIndex; int rightIndex; }; //End of structure class BinaryTree{ vector bt; public: void makeNode(int data); void insertLeft(int index, int data); void insertRight(int index, int data); void insert(int data); void traverse(int index, int order); }; //End of class

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay6 void BinaryTree::makeNode(int data){ struct Node b = { data, -1, -1}; bt.push_back(b); } //End of function void BinaryTree::insertLeft(int index, int data){ struct Node b = { data, -1, -1 }; bt[index].leftIndex = bt.size();; bt.push_back(b); } //End of function void BinaryTree::insertRight(int index, int data){ struct Node b = { data, -1, -1 }; bt[index].rightIndex = bt.size();; bt.push_back(b); } //End of function

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay7 void BinaryTree::insert(int data){ //Code if tree is empty // Code if tree is not empty }

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay8 //Code if tree is empty if(bt.size() == 0){ makeNode(data); } //Code if tree is not empty else { int index = 0; while ( index < bt.size() ){ // Code to insert data at the Left of parent // Code to insert data at the Right of parent } //End of while } //End of else }

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay9 // Code to insert data at the Left of parent if(data <= bt[index].data){ if( bt[index].leftIndex == -1){ insertLeft(index, data); break; } else index = bt[index].leftIndex; }

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay10 // Code to insert data at the Right of parent else{ if(bt[index].rightIndex == -1){ insertRight(index, data); break; } else index = bt[index].rightIndex; } //End of else

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay11 void BinaryTree::traverse(int index, int order){ if(order == 1) cout << bt[index].data << " "; if(bt[index].leftIndex != -1) traverse(bt[index].leftIndex, order); if(order == 2) cout<<bt[index].data << " "; if( bt[index].rightIndex != -1) traverse(bt[index].rightIndex, order); if(order == 3) cout<<bt[index].data << " "; } //End of function

IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay12 int main() { BinaryTree b1; b1.insert(60); b1.insert(40); b1.insert(20); b1.insert(25); b1.insert(90); b1.insert(23); b1.insert(95); b1.insert(80); b1.insert(85); cout << "Pre-Order\n"; b1.traverse(0,1); cout << "\n\nIn-Order\n"; b1.traverse(0,2); cout << "\n\nPost-Order\n"; b1.traverse(0,3); return 0; } //End of main() function

IIT Bombay Output Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay13 Pre-Order In-Order Post-Order

IIT Bombay Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay14