Skip to content

Commit edc26bb

Browse files
committed
added tree interface
1 parent 5dc5be2 commit edc26bb

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎src/ds/BST.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
// remove -> O(h) can be O(n) if input is sorted
1010
// no duplicate is allowed, for duplicate supported BST look BSTWithDuplicate.java
1111
// most methods are recursive, look BSTIterative.java for iterative approach BST
12-
public class BST<K extends Comparable> {
12+
public class BST<K extends Comparable> implements Tree<K> {
1313
private Node root;
1414

15+
16+
@Override
1517
public void insert(K k) {
1618
root = put(root, k, null);
1719
}
1820

21+
@Override
1922
public boolean search(K k) {
2023
return findNode(k, root) != null;
2124
}
2225

26+
@Override
2327
public void delete(K k) {
2428
Node x = findNode(k, root);
2529
delete(x);

‎src/ds/BSTIterative.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ds;
2+
3+
/**
4+
* Created by sherxon on 12/18/16.
5+
*/
6+
// Binary Search Tree, two nodes (left, right) and one parent.
7+
// insert -> O(h) can be O(n) if input is sorted
8+
// search -> O(h) can be O(n) if input is sorted
9+
// remove -> O(h) can be O(n) if input is sorted
10+
// no duplicate is allowed, for duplicate supported BST look BSTWithDuplicate.java
11+
// most methods are iterative, look BST.java for recursive approach BST
12+
public class BSTIterative<K extends Comparable> implements Tree<K> {
13+
14+
private Node root;
15+
16+
@Override
17+
public void insert(K k) {
18+
19+
}
20+
21+
@Override
22+
public boolean search(K k) {
23+
return false;
24+
}
25+
26+
@Override
27+
public void delete(K k) {
28+
29+
}
30+
31+
32+
private class Node {
33+
K value;
34+
int size = 1;
35+
Node left, right, parent;
36+
37+
public Node(K k, Node parent) {
38+
this.value = k;
39+
this.parent = parent;
40+
}
41+
}
42+
43+
}

‎src/ds/Tree.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ds;
2+
3+
/**
4+
* Created by sherxon on 12/18/16.
5+
*/
6+
public interface Tree<K> {
7+
void insert(K k);
8+
boolean search(K k);
9+
void delete(K k);
10+
}

0 commit comments

Comments
 (0)