Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. /**
  2. *
  3. * @author Stephen Colegrove
  4. *
  5. * @param <Key>
  6. * @param <Value>
  7. */
  8. public class MyBinarySearchTree< Key extends Comparable< Key >, Value extends Comparable< Value > > implements BinarySearchTreeInterface< Key , Value> {
  9.  
  10. private Node root = null;
  11. private Node current = root;
  12.  
  13. private int size = 0;
  14.  
  15. private class Node{
  16.  
  17. private Node parent;
  18. private Node leftChild;
  19. private Node rightChild;
  20. public KeyValuePair<Key, Value> theData;
  21.  
  22.  
  23. //constructor?
  24. public Node(KeyValuePair<Key, Value> data){
  25.  
  26.  
  27. theData = data;
  28. parent = null;
  29. leftChild = null;
  30. rightChild = null;
  31. }
  32.  
  33. //getters
  34. public Key getKey(){
  35.  
  36. return theData.getKey();
  37.  
  38. }
  39.  
  40. public Value getValue(){
  41.  
  42. return theData.getValue();
  43. }
  44.  
  45.  
  46. public Node getParent(){
  47.  
  48. return parent;
  49. }
  50.  
  51. public Node getLeftChild(){
  52.  
  53. return leftChild;
  54. }
  55.  
  56. public Node getRightChild(){
  57.  
  58. return rightChild;
  59. }
  60.  
  61. //setters
  62. public void setKey(Key k ){
  63. theData.setKey(k);
  64. }
  65.  
  66. public void setValue(Value v){
  67. theData.setValue(v);
  68. }
  69.  
  70. public void setParent(Node n){
  71.  
  72. parent = n;
  73. }
  74.  
  75. public void setleftChild(Node n){
  76.  
  77. leftChild = n;
  78. }
  79.  
  80. public void setRightChild(Node n){
  81.  
  82. leftChild = n;
  83. }
  84.  
  85. //sanity checks
  86.  
  87. public boolean hasLeftChild(){
  88.  
  89. return leftChild == null;
  90. }
  91.  
  92. public boolean hasRightChild(){
  93.  
  94. return rightChild == null;
  95. }
  96.  
  97. }
  98.  
  99.  
  100. // Return the value at the tree node indicated by the key.
  101. // If none exists, return null;
  102. public Value get( Key key ) {
  103.  
  104. if(this.isEmpty()){
  105. return null;
  106. }
  107.  
  108. if(root.getKey().compareTo(key) == 0){
  109. return root.getValue();
  110. }
  111.  
  112. if(current.hasLeftChild()){
  113. if(key.compareTo(current.getKey()) < 0){
  114. current = current.leftChild;
  115. }
  116. //current.setleftChild(current);
  117.  
  118. return get(current.getLeftChild().getKey());
  119.  
  120. } else {
  121.  
  122. if(current.hasRightChild())
  123. return get(current.getRightChild().getKey());
  124.  
  125. }
  126.  
  127.  
  128.  
  129. return null;
  130. }
  131. //To do
  132. public Value getHelp(Key key, Node currentNode){
  133.  
  134.  
  135.  
  136.  
  137.  
  138. return null;
  139.  
  140. }
  141.  
  142. // Insert or replace the value at the tree node indicated by key.
  143. // Return the previous value or null if none existed.
  144. public Value put( Key key, Value value ) {
  145.  
  146. Node temp = root;
  147.  
  148. if (root == null) {
  149. //make new node
  150. KeyValuePair<Key,Value> pair = new KeyValuePair<Key,Value>();
  151. pair.setKey(key);
  152. pair.setValue(value);
  153. temp = new Node(pair);
  154. size++;
  155. root = temp;
  156. return null;
  157. }
  158.  
  159. if(key.compareTo(current.getKey()) < 0){
  160. if(current.hasLeftChild()){
  161.  
  162. }
  163.  
  164.  
  165. }
  166. return value;
  167. }
  168.  
  169. // Remove the tree node indicated by key.
  170. // Return the previous value or null if none existed.
  171. public Value remove( Key key ) {
  172. return null;
  173. }
  174.  
  175. // Return the list of KeyValuePairs produced by a pre-order traversal of the tree.
  176. // Return an empty list of the tree is empty.
  177. public List<KeyValuePair<Key,Value>> preOrderTraversal( ) {
  178. return null;
  179. }
  180.  
  181. // Return the list of KeyValuePairs produced by an in-order traversal of the tree.
  182. // Return an empty list of the tree is empty.
  183. public List<KeyValuePair<Key,Value>> inOrderTraversal( ) {
  184. return null;
  185. }
  186.  
  187. // Return the list of KeyValuePairs produced by a post-order traversal of the tree.
  188. // Return an empty list of the tree is empty.
  189. public List<KeyValuePair<Key,Value>> postOrderTraversal( ) {
  190. return null;
  191. }
  192.  
  193. // Return an array of KeyValuePairs produced by a breadth-first traversal of the tree.
  194. // Return an empty array of the tree is empty.
  195. // Missing children should be represented by a null entry in the array.
  196. public KeyValuePair< Key, Value > [ ] breadthFirstTraversal( ) {
  197. return null;
  198. }
  199.  
  200. // Returns the number of nodes in tree.
  201. public int getSize( ) {
  202.  
  203. return size;
  204. }
  205.  
  206. // Return true if there are no nodes in the tree
  207. public boolean isEmpty( ) {
  208. if(size == 0){
  209. return true;
  210. }
  211.  
  212. return false;
  213. }
  214.  
  215. // Return a String representation of the tree.
  216. // Nodes should be ordered as if by breadth-first traversal.
  217. public String toString( ) {
  218. return null;
  219. }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement