Skip to content

Commit fcaa064

Browse files
committed
updated checks bianry
1 parent 9b18ee3 commit fcaa064

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ public boolean remove(int value) {
7272
if (temp == null || temp.data != value) return false;
7373

7474
if (temp.left == null && temp.right == null) {
75-
if (temp == root) root = null;
76-
else if (temp.parent.data < temp.data) temp.parent.right = null;
77-
else temp.parent.left = null;
75+
if (temp == root)
76+
root = null;
77+
else if (temp.parent.data < temp.data)
78+
temp.parent.right = null;
79+
else
80+
temp.parent.left = null;
7881
return true;
7982
} else if (temp.left != null && temp.right != null) {
8083
Node successor = findSuccessor(temp);
@@ -84,7 +87,8 @@ public boolean remove(int value) {
8487
if (successor.right != null) {
8588
successor.right.parent = successor.parent;
8689
successor.parent.left = successor.right;
87-
} else successor.parent.left = null;
90+
} else
91+
successor.parent.left = null;
8892
successor.right = temp.right;
8993
successor.right.parent = successor;
9094
}
@@ -93,17 +97,22 @@ public boolean remove(int value) {
9397
successor.parent = null;
9498
} else {
9599
successor.parent = temp.parent;
96-
if (temp.parent.data < temp.data) temp.parent.right = successor;
97-
else temp.parent.left = successor;
100+
if (temp.parent.data < temp.data)
101+
temp.parent.right = successor;
102+
else
103+
temp.parent.left = successor;
98104
}
99105
return true;
100106
} else {
101107
Node child = (temp.right != null) ? temp.right : temp.left;
102-
if (temp == root) root = child;
108+
if (temp == root)
109+
root = child;
103110
else {
104111
child.parent = temp.parent;
105-
if (temp.data < temp.parent.data) temp.parent.left = child;
106-
else temp.parent.right = child;
112+
if (temp.data < temp.parent.data)
113+
temp.parent.left = child;
114+
else
115+
temp.parent.right = child;
107116
}
108117
return true;
109118
}

0 commit comments

Comments
 (0)