@@ -74,6 +74,21 @@ public void postOrder() {
74
74
postOrderRec (root );
75
75
}
76
76
77
+ // Level-order traversal: Root -> Level by Level
78
+ public void levelOrder () {
79
+ if (root == null ) return ;
80
+ Queue <Node > queue = new LinkedList <>();
81
+ queue .add (root );
82
+
83
+ while (!queue .isEmpty ()) {
84
+ Node currentNode = queue .poll ();
85
+ System .out .print (currentNode .nodedata + " " );
86
+ if (currentNode .left != null ) queue .add (currentNode .left );
87
+ if (currentNode .right != null ) queue .add (currentNode .right );
88
+ }
89
+ }
90
+
91
+
77
92
private void postOrderRec (Node root ) {
78
93
if (root != null ) {
79
94
postOrderRec (root .left );
@@ -82,6 +97,56 @@ private void postOrderRec(Node root) {
82
97
}
83
98
}
84
99
100
+ // Delete a node with the given data from the tree
101
+ public void delete (int nodedata ) {
102
+ root = deleteRec (root , nodedata );
103
+ }
104
+
105
+ private Node deleteRec (Node root , int nodedata ) {
106
+ if (root == null ) return root ;
107
+
108
+ // Traverse the tree to find the node
109
+ if (nodedata < root .nodedata ) {
110
+ root .left = deleteRec (root .left , nodedata );
111
+ } else if (nodedata > root .nodedata ) {
112
+ root .right = deleteRec (root .right , nodedata );
113
+ } else {
114
+ // Node with only one child or no child
115
+ if (root .left == null ) return root .right ;
116
+ else if (root .right == null ) return root .left ;
117
+
118
+ // Node with two children: get the inorder successor
119
+ root .nodedata = minValue (root .right );
120
+ root .right = deleteRec (root .right , root .nodedata );
121
+ }
122
+ return root ;
123
+ }
124
+
125
+ // Helper method to find the minimum value in the subtree
126
+ private int minValue (Node root ) {
127
+ int minValue = root .nodedata ;
128
+ while (root .left != null ) {
129
+ minValue = root .left .nodedata ;
130
+ root = root .left ;
131
+ }
132
+ return minValue ;
133
+ }
134
+
135
+ // Calculate the height of the tree
136
+ public int height () {
137
+ return height (root );
138
+ }
139
+
140
+
141
+ private int height (Node node ) {
142
+ if (node == null ) return 0 ;
143
+ int leftHeight = height (node .left );
144
+ int rightHeight = height (node .right );
145
+ return Math .max (leftHeight , rightHeight ) + 1 ;
146
+ }
147
+
148
+
149
+
85
150
// Search for a value in the binary tree
86
151
public boolean search (int nodedata ) {
87
152
return searchRec (root , nodedata ) != null ;
@@ -126,6 +191,16 @@ public static void main(String[] args) {
126
191
System .out .println ("\n \n Post-order traversal:" );
127
192
tree .postOrder (); // Output: 20 40 30 60 80 70 50
128
193
194
+ System .out .println ("\n \n Level-order traversal:" );
195
+ tree .levelOrder (); // Output: 50 30 70 20 40 60 80
196
+ System .out .println ("\n \n Tree height: " + tree .height ()); // Expected output: Height of tree
197
+
198
+
199
+ System .out .println ("\n \n Deleting 20:" );
200
+ tree .delete (20 );
201
+ tree .inOrder (); // Expected output (without 20): 30 40 50 60 70 80
202
+
203
+
129
204
// Searching for an element in the tree
130
205
System .out .println ("\n \n Search for 60: " + tree .search (60 )); // Output: true
131
206
System .out .println ("Search for 90: " + tree .search (90 )); // Output: false
0 commit comments