Skip to content

Commit e86f2da

Browse files
authored
Update binarytree.java
This will check the overall height of the binary tree. These additions will make the tree more functional for operations like deletion, level-order traversal, and balance checking
1 parent 369d434 commit e86f2da

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Binary tree/binarytree.java

+75
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ public void postOrder() {
7474
postOrderRec(root);
7575
}
7676

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+
7792
private void postOrderRec(Node root) {
7893
if (root != null) {
7994
postOrderRec(root.left);
@@ -82,6 +97,56 @@ private void postOrderRec(Node root) {
8297
}
8398
}
8499

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+
85150
// Search for a value in the binary tree
86151
public boolean search(int nodedata) {
87152
return searchRec(root, nodedata) != null;
@@ -126,6 +191,16 @@ public static void main(String[] args) {
126191
System.out.println("\n\nPost-order traversal:");
127192
tree.postOrder(); // Output: 20 40 30 60 80 70 50
128193

194+
System.out.println("\n\nLevel-order traversal:");
195+
tree.levelOrder(); // Output: 50 30 70 20 40 60 80
196+
System.out.println("\n\nTree height: " + tree.height()); // Expected output: Height of tree
197+
198+
199+
System.out.println("\n\nDeleting 20:");
200+
tree.delete(20);
201+
tree.inOrder(); // Expected output (without 20): 30 40 50 60 70 80
202+
203+
129204
// Searching for an element in the tree
130205
System.out.println("\n\nSearch for 60: " + tree.search(60)); // Output: true
131206
System.out.println("Search for 90: " + tree.search(90)); // Output: false

0 commit comments

Comments
 (0)