forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBstSet.java
More file actions
28 lines (26 loc) · 822 Bytes
/
BstSet.java
File metadata and controls
28 lines (26 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class BstSet<E extends Comparable<E>> extends BinarySearchTree<E> {
protected Node<E> insert(E newItem, Node<E> node) {
if (node == null) {
return new Node<E>(newItem, null, null);
} else if (newItem.compareTo(node.item) < 0) {
node.left = insert(newItem, node.left);
return node;
} else if (newItem.equals(node.item)) {
return node;
} else {
node.right = insert(newItem, node.right);
return node;
}
}
public static void main(String[] args) {
BinarySearchTree<Integer> nums = new BstSet<>();
nums.add(3);
nums.add(4);
nums.add(1);
nums.add(5);
nums.add(2);
nums.add(4);
nums.add(1);
System.out.println(nums);
}
}