Repository navigation
Expand file tree
/
Copy pathBSTWithDuplicate.java
More file actions
78 lines (65 loc) · 2.07 KB
/
Copy pathBSTWithDuplicate.java
File metadata and controls
78 lines (65 loc) · 2.07 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ds;
/**
* This is another version of binary search tree with support of duplicates.
* Idea: store extra variable that counts the number of duplicates in each node.
*/
public class BSTWithDuplicate<K extends Comparable> implements Tree<K> {
private Node root;
@Override
public void insert(K k) {
if (k == null) throw new NullPointerException();
root = newNode(k, root);
}
private Node newNode(K k, Node root) {
if (root == null) return new Node(k, null);
if (k.compareTo(root.value) < 0)
root.left = newNode(k, root.left);
else if (k.compareTo(root.value) > 0)
root.right = newNode(k, root.right);
else
root.duplicateCount++;
return root;
}
@Override
public boolean search(K k) {
if (k == null) throw new NullPointerException();
return find(k, root);
}
private boolean find(K k, Node root) {
if (root == null) return false;
if (k.compareTo(root.value) > 0)
return find(k, root.right);
else if (k.compareTo(root.value) < 0)
return find(k, root.left);
else
return true;
}
@Override
public void delete(K k) {
// this is the same implementation as other BST provided with small change i.e. decrement
// node counter if it is more than one otherwise remove it.
}
public int numberOf(K k) {
if (k == null) throw new NullPointerException();
return numberOf(k, root);
}
private int numberOf(K k, Node root) {
if (root == null) return 0;
if (k.compareTo(root.value) > 0)
return numberOf(k, root.right);
else if (k.compareTo(root.value) < 0)
return numberOf(k, root.left);
else
return root.duplicateCount;
}
private class Node {
K value;
int size = 1;
int duplicateCount = 1;
Node left, right, parent;
public Node(K k, Node parent) {
this.value = k;
this.parent = parent;
}
}
}