Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 Kevin 1
import java.util.ArrayList;
2
 
3
// Generic BST implementation
4
public class BSTree<K extends Comparable<? super K>, E> implements Dictionary<K, E> {
5
	private BSTreeNode<K,E> root;	// Root of the BST
6
 
7
	// Constructor
8
	public BSTree() {
9
		this.root = null;
10
	}
11
 
12
	// Inserts a node into the BST
13
	public void insert(K key, E element) {
14
		// Set the BST root as the returned node
15
		this.root = insertRecursive(root, key, element);
16
	}
17
 
18
	// Removes a node from the BST given the key
19
	public void remove(K key) {
20
		// Adds invalid coordinates if none are specified
21
		this.remove(key, -1, -1);
22
	}
23
	public void remove(K key, int x, int y) {
24
		// Check if node exists
25
		E temp = findRecursive(root, key);
26
		if (temp != null) {
27
			// Remove the node
28
			this.root = removeRecursive(root, key, x, y);
29
		}
30
	}
31
 
32
	// Returns a node from the BST given the key
33
	public E find(K key) {
34
		return findRecursive(root, key);
35
	}
36
 
37
	// Returns a list of all nodes with the given key
38
	public ArrayList<E> findAll(K key) {
39
		ArrayList<E> elementList = new ArrayList<E>();
40
		findAllRecursive(root, key, elementList);
41
		return elementList;
42
	}
43
 
44
	// Removes all nodes from the BST
45
	public void clear() {
46
		this.root = null;
47
	}
48
 
49
	// Recursively inserts a node into the BST
50
	private BSTreeNode<K,E> insertRecursive(BSTreeNode<K,E> root, K key, E element) {
51
		if (root == null)
52
			// If there are no nodes in the BST, create and return a new node
53
			return new BSTreeNode<K,E>(key, element);
54
		if (root.getKey().compareTo(key) > 0)
55
			// If passed key is smaller, insert into left child
56
			root.setLeft(insertRecursive(root.getLeft(), key, element));
57
		else
58
			// If passed key is greater, insert into right child
59
			root.setRight(insertRecursive(root.getRight(), key, element));
60
		return root;
61
	}
62
 
63
	// Recursively searches minimal nodes for matches to the given key
64
	private E findRecursive(BSTreeNode<K,E> root, K key) {
65
		if (root == null)
66
			return null;
67
 
68
		if (root.getKey().compareTo(key) > 0)
69
			// If passed key is smaller, search the left child
70
			return findRecursive(root.getLeft(), key);
71
		else if (root.getKey().compareTo(key) == 0)
72
			// If key is found, return with the found element
73
			return root.getElement();
74
		else
75
			// If key is not found, search the right child
76
			return findRecursive(root.getRight(), key);
77
	}
78
 
79
	// Recursively searches all nodes for matches to the given key
80
	private void findAllRecursive(BSTreeNode<K,E> root, K key, ArrayList<E> elementList) {
81
		if (root == null)
82
			return;
83
 
84
		// If key matches, save to list
85
		if (root.getKey().compareTo(key) == 0)
86
			elementList.add(root.getElement());
87
 
88
		// Recursively call search on all possible nodes
89
		if (root.getKey().compareTo(key) > 0)
90
			findAllRecursive(root.getLeft(), key, elementList);
91
		else
92
			findAllRecursive(root.getRight(), key, elementList);
93
	}
94
 
95
	// Recursively removes a node from the BST given a key
96
	private BSTreeNode<K,E> removeRecursive(BSTreeNode<K,E> root, K key, int x, int y) {
97
		if (root == null)
98
			return null;
99
		// Find the node to remove
100
		if (root.getKey().compareTo(key) > 0)
101
			root.setLeft(removeRecursive(root.getLeft(), key, x, y));
102
		else if (root.getKey().compareTo(key) < 0)
103
			root.setRight(removeRecursive(root.getRight(), key, x, y));
104
		// Node's key matches
105
		else {
106
			// If we want to narrow it down by coordinates as well
107
			if (x >= 0 && y >= 0) {
108
				// If coordinates do not match, keep searching from the right child
109
				if (((Record)root.getElement()).getX() != x || ((Record)root.getElement()).getY() != y) {
110
					root.setRight(removeRecursive(root.getRight(), key, x, y));
111
				} else {
112
					// If one of the leaves is null, just return the other leaf
113
					if (root.getLeft() == null)
114
						return root.getRight();
115
					else if (root.getRight() == null)
116
						return root.getLeft();
117
					else {
118
						// Otherwise create a new node with the smallest value on the right leaf
119
						BSTreeNode<K,E> temp = getMin(root.getRight());
120
						root.setElement(temp.getElement());
121
						root.setKey(temp.getKey());
122
						root.setRight(deleteMin(root.getRight()));
123
					}
124
				}
125
			// Otherwise just remove the node
126
			} else {
127
				// If one of the leaves is null, just return the other leaf
128
				if (root.getLeft() == null)
129
					return root.getRight();
130
				else if (root.getRight() == null)
131
					return root.getLeft();
132
				else {
133
					// Otherwise create a new node with the smallest value on the right leaf
134
					BSTreeNode<K,E> temp = getMin(root.getRight());
135
					root.setElement(temp.getElement());
136
					root.setKey(temp.getKey());
137
					root.setRight(deleteMin(root.getRight()));
138
				}
139
			}
140
		}
141
		return root;
142
	}
143
 
144
	// Recursively returns the minimum key node
145
	private BSTreeNode<K,E> getMin(BSTreeNode<K,E> root) {
146
		if (root.getLeft() == null)
147
			return root;
148
		else
149
			return getMin(root.getLeft());
150
	}
151
 
152
	// Recursively deletes the minimum key node
153
	private BSTreeNode<K,E> deleteMin(BSTreeNode<K,E> root) {
154
		if (root.getLeft() == null)
155
			return root.getRight();
156
		else {
157
			root.setLeft(deleteMin(root.getLeft()));
158
			return root;
159
		}
160
	}
161
}