Skip to content

Commit 6fdfbc3

Browse files
author
Swapnil Gaikwad
committed
Reimplemented remove operation with remove root element
1 parent 9b745c1 commit 6fdfbc3

File tree

2 files changed

+35
-66
lines changed

2 files changed

+35
-66
lines changed

src/main/java/com/datastructures/Tree.java

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,90 +33,59 @@ public void add( E data ) {
3333
}
3434
}
3535

36-
public boolean find( E data ) {
37-
return find( data, root );
36+
public boolean contains( E data ) {
37+
return contains( data, root );
3838
}
3939

40-
private boolean find( E data, Node root ) {
40+
private boolean contains( E data, Node root ) {
4141
if ( root == null ) {
4242
return false;
4343
}
4444
if ( root.data == data ) {
4545
return true;
4646
}
4747
if ( data.compareTo( root.data ) < 0 ) {
48-
return find( data, root.left );
48+
return contains( data, root.left );
4949
} else {
50-
return find( data, root.right );
50+
return contains( data, root.right );
5151
}
5252
}
5353

5454
public boolean remove( E data ) {
55-
if ( root.data == data ) {
56-
return false;
57-
}
58-
Node parent = findParent( root, data );
59-
if ( parent == null ) {
60-
return false;
61-
}
62-
Node current;
63-
if ( parent.left != null && parent.left.data == data ) {
64-
current = parent.left;
65-
} else {
66-
current = parent.right;
67-
}
68-
remove( parent, current );
69-
return true;
55+
final boolean isPresent = contains( data );
56+
root = remove( root, data );
57+
return isPresent;
7058
}
7159

72-
private void remove( Node parent, Node current ) {
73-
if ( current.isLeaf() ) {
74-
if ( parent.left == current ) {
75-
parent.left = null;
76-
} else {
77-
parent.right = null;
78-
}
79-
return;
60+
private Node remove( Node root, E data ) {
61+
// If tree is not empty
62+
if ( root == null ) {
63+
return null;
8064
}
81-
if ( current.left == null ) {
82-
if ( parent.left == current ) {
83-
parent.left = current.right;
84-
} else {
85-
parent.right = current.right;
65+
66+
if ( data.compareTo( root.data ) < 0 ) {
67+
root.left = remove( root.left, data );
68+
} else if ( data.compareTo( root.data ) > 0 ) {
69+
root.right = remove( root.right, data );
70+
} else {
71+
if ( root.left == null ) {
72+
return root.right;
8673
}
87-
return;
88-
}
89-
if ( current.right == null ) {
90-
if ( parent.left == current ) {
91-
parent.left = current.left;
92-
} else {
93-
parent.right = current.left;
74+
if ( root.right == null ) {
75+
return root.left;
9476
}
95-
return;
96-
}
97-
Node itr = current.right;
98-
parent = current;
99-
while ( itr.left != null ) {
100-
parent = itr;
101-
itr = itr.left;
77+
//Element found
78+
root.data = minValue( root.right );
79+
root.right = remove( root.right, root.data );
10280
}
103-
current.data = itr.data;
104-
remove( parent, itr );
81+
return root;
10582
}
10683

107-
private Node findParent( Node current, E data ) {
108-
if ( current == null || current.data == data ) {
109-
return null;
110-
}
111-
if ( current.left != null && current.left.data == data ) {
112-
return current;
113-
} else if ( current.right != null && current.right.data == data ) {
114-
return current;
115-
} else if ( data.compareTo( current.data ) < 0 ) {
116-
return findParent( current.left, data );
117-
} else {
118-
return findParent( current.right, data );
84+
private E minValue( Node root ) {
85+
while ( root.left != null ) {
86+
root = root.left;
11987
}
88+
return root.data;
12089
}
12190

12291
private class Node {

src/test/java/com/datastructures/TreeTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public void add_node_in_tree() {
1616
tree.add( 2 );
1717
tree.add( 1 );
1818

19-
assertEquals( true, tree.find( 2 ) );
20-
assertEquals( false, tree.find( 5 ) );
19+
assertEquals( true, tree.contains( 2 ) );
20+
assertEquals( false, tree.contains( 5 ) );
2121
}
2222

2323
@Test
@@ -30,12 +30,12 @@ public void remove_node_in_tree() {
3030
tree.add( 1 );
3131
tree.add( 3 );
3232

33-
List<Integer> list = Arrays.asList( 8, 7, 2, 1, 3 );
33+
List<Integer> list = Arrays.asList( 4, 8, 7, 2, 1, 3 );
3434
list.forEach( element -> {
3535
System.out.println( element );
36-
assertEquals( true, tree.find( element ) );
36+
assertEquals( true, tree.contains( element ) );
3737
assertEquals( true, tree.remove( element ) );
38-
assertEquals( false, tree.find( element ) );
38+
assertEquals( false, tree.contains( element ) );
3939
} );
4040
}
4141
}

0 commit comments

Comments
 (0)