Download presentation
Presentation is loading. Please wait.
Published byNeil Phillips Modified over 8 years ago
1
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
2
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’
3
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
4
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
5
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
6
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
7
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 }
8
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 }
9
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; }
10
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
11
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
12
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
13
IIT Bombay Output Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay13 Pre-Order 60 40 20 25 23 90 80 85 95 In-Order 20 23 25 40 60 80 85 90 95 Post-Order 23 25 20 40 85 80 95 90 60
14
IIT Bombay Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.