Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Basic Operations on Binary Tree Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Binary Tree 60 40 20 25 23 90 80 95 85 Nodes Parent Left Child Right Child Can have either left, right, or both Leaf Node Does not have left and right child 60 40 20 25 23 90 80 95 85 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Represented Internally as Each node in the tree is represented with Data: The element leftIndex: Index of the left child rightIndex: Index of the right child Data Left Index Right Index Data Left Index Right Index Data Left Index Right Index Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Represented Internally as Data of the left child is less than the data of the parent Data of the right child is greater than the data of the parent Value of -1 in leftIndex or rightIndex denotes that it does not have a left or right child respectively At Index: 10 1 2 10 8 12 1 2 At Index: 1 At Index: 2 8 -1 12 -1 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Basic Operations Making a node (Root) Inserting Insert Left node Insert Right node Traversing Pre-Order In-Order Post-Order Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Making a Node (60) 60 Initialized only when the tree is empty Make node with data as ‘60’ Data: 60 Left Index: -1 Right Index: -1 60 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (40) ‘40 < 60’, and there is no left child of ‘60’, so insert ‘40’ as left child of ‘60’ Left Index of 60: 1 Data: 40 Left Index: -1 Right Index: -1 60 1 40 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (20) 60 40 20 Traverse from root ‘20 < 60’, ‘20 < 40’, and there is no left child of ‘40’, so insert ‘20’ as left child of ‘40’ Left Index of 40: 2 Data: 20 Left Index: -1 Right Index: -1 60 1 40 2 20 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (25) 60 40 20 25 Traverse from root ‘25 < 60’, ‘25 < 40’, Now, ‘25 > 20’, and there is no right child of ‘20’, so insert ‘25’ as right child of ‘20’ Right Index of 20: 3 Data: 25 Left Index: -1 Right Index: -1 60 1 40 2 20 3 25 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (90) 60 40 90 20 25 Traverse from root ‘90 > 60’, and there is no right child of ‘60’, so insert ‘90’ as right child of ‘60’ Right Index of 60: 4 Data: 90 Left Index: -1 Right Index: -1 60 1 4 40 90 2 20 3 25 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (23) 60 40 90 20 25 23 Traverse from root ‘23 < 60’, ‘23 < 40’, ‘23 > 20’, but right child (25) is present, so continue comparing. ‘23 < 25’, and there is no left child of ‘25’, so insert ‘23’ as left child of ‘25’ Left Index of 25: 5 Data: 23 Left Index: -1 Right Index: -1 60 1 4 40 90 2 20 3 25 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (95) 60 40 90 20 95 25 23 Traverse from root ‘95 > 60’, ‘95 > 90’, and there is no right child of ‘90’, so insert ‘95’ as right child of ‘90’ Right Index of 90: 6 Data: 95 Left Index: -1 Right Index: -1 60 1 4 40 90 2 6 20 95 3 25 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (80) 60 40 90 20 80 95 25 23 Traverse from root ‘80 > 60’, But, ‘80 < 90’, and there is no left child of ‘90’, so insert ‘80’ as left child of ‘90’ Left Index of 90: 7 Data: 80 Left Index: -1 Right Index: -1 60 1 4 40 90 2 7 6 20 80 95 3 25 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Insert (85) 60 40 90 20 80 95 25 85 23 Traverse from root ‘85 > 60’, ‘85 < 90’, but left child of ‘90’ is present, so continue ‘85 > 80’ and there is no right child of ‘80’, so insert ‘85’ as right child of ‘80’ Right Index of 80: 8 Data: 85 Left Index: -1 Right Index: -1 60 1 4 40 90 2 7 6 20 80 95 3 8 25 85 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay