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