-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeList.java
58 lines (54 loc) · 1.31 KB
/
TreeList.java
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
/**
*
* Tree list
*
* An implementation of a Tree list with key and info
*
*/
public class TreeList {
private AVLTree avlTree;
/**
* public TreeList()
* constructor.
* Complexity: O(1)
*/
public TreeList() {
this.avlTree = new AVLTree();
}
/**
* public Item retrieve(int i)
* returns the item in the ith position if it exists in the list. otherwise,
* returns null
* Complexity: O(logn)
*/
public Item retrieve(int i) {
if (i < 0 || i > this.avlTree.size() - 1)
return null;
AVLTree.IAVLNode node = this.avlTree.treeSelect(i + 1);
return new Item(node.getKey(), node.getValue());
}
/**
* public int insert(int i, int k, String s)
* inserts an item to the ith position in list with key k and info s. returns -1
* if i<0 or i>n otherwise return 0.
* Complexity: O(logn)
*/
public int insert(int i, int k, String s) {
if (i < 0 || i > this.avlTree.size())
return -1;
this.avlTree.insertByRank(i, k, s);
return 0;
}
/**
* public int delete(int i)
* deletes an item in the ith posittion from the list. returns -1 if i<0 or
* i>n-1 otherwise returns 0.
* Complexity: O(logn)
*/
public int delete(int i) {
if (i < 0 || i > this.avlTree.size() - 1)
return -1;
this.avlTree.deleteByRank(i);
return 0;
}
}