Advertisement
Guest User

Tree234

a guest
Jul 28th, 2014
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. /*
  2. Project 10.1
  3. Directions: Write a method that returns the minimum value in a 2-3-4 tree
  4.  
  5. Plan:
  6. - The 2-3-4 tree's minimum should ideally be the left most node's child (which will be a leaf)
  7. - ie. xxx
  8. / \
  9. xxx xxx
  10. / \
  11. oxx xxx
  12. where o is the minimum value
  13.  
  14. - return the min value, we should write a method that returns a long (value)
  15. - should just return the left most value
  16. - Node has a getMin() method that returns the item at the left most value (represents the smallest amount in current node)
  17. - if the parent at getMin() is not a leaf, go to the left child of that parent and call it again
  18. - stop once getMin() is actually at a leaf
  19. _ CANT FIGURE OUT HOW TO DO THIS
  20. */
  21. ////////////////////////////////////////////////////////////////
  22. // An object of this class represents the entire tree
  23. class Tree234
  24. {
  25. private Node root = new Node(); // make root node
  26. // -------------------------------------------------------------
  27. // minimum value method
  28. public long minValue() {
  29. Node curNode = root; // current node = root
  30. long min = curNode.getMin();
  31. while(!curNode.isLeaf()) { // while current node is NOT a leaf
  32. curNode = getNextChild(curNode, min);
  33. } // end while
  34. return min;
  35. } // end minValue()
  36. // -------------------------------------------------------------
  37. public int find(long key) // find data item with given key
  38. {
  39. Node curNode = root; // current node = root
  40. int childNumber; // reference to index of child
  41. while(true)
  42. {
  43. // we set childNumber to = the currentNode.findItem(key) which returns the index of the matching value
  44. // if the matching value is not found, findItem() returns -1
  45. // if childNumber is found, we return that index
  46. if(( childNumber=curNode.findItem(key) ) != -1)
  47. return childNumber; // found it
  48. else if( curNode.isLeaf() ) // if the current node is a leaf, we can't go farther down
  49. return -1; // can't find it
  50. else // search deeper, current node is not a leaf
  51. curNode = getNextChild(curNode, key); // figures out which of the node's children it should go to next
  52. } // end while
  53. }
  54. // -------------------------------------------------------------
  55. // insert a DataItem
  56. // like find, except it splits a full node if it finds one
  57. // goes until it finds a leaf node, then inserts data item into leaf
  58. public void insert(long dValue)
  59. {
  60. Node curNode = root;
  61. DataItem tempItem = new DataItem(dValue); // temp item used to safely store data
  62.  
  63. while(true)
  64. {
  65. if( curNode.isFull() ) // if node full,
  66. {
  67. split(curNode); // split it up
  68. curNode = curNode.getParent(); // back up, current node now equals the current node's parent (one level up)
  69. curNode = getNextChild(curNode, dValue); // search once through the item array
  70. // we then find if the next child is greater than or less than the value given
  71. } // end if(node is full)
  72.  
  73. else if( curNode.isLeaf() ) // if node is leaf,
  74. break; // go to insert data item, break out of loop
  75. else // node is not full, not a leaf; so go to lower level
  76. curNode = getNextChild(curNode, dValue); // after this executes, it returns to the first if statement
  77. } // end while
  78.  
  79. curNode.insertItem(tempItem); // insert the new DataItem (when it is a leaf and has been split)
  80. } // end insert()
  81. // -------------------------------------------------------------
  82. // split is passed the node it will split
  83. public void split(Node thisNode) // split the node
  84. {
  85. // assumes node is full
  86. DataItem itemB, itemC;
  87. Node parent, child2, child3;
  88. int itemIndex;
  89. // two right most items are removed from the node and stored
  90. itemC = thisNode.removeItem(); // remove items from
  91. itemB = thisNode.removeItem(); // this node
  92. child2 = thisNode.disconnectChild(2); // remove children
  93. child3 = thisNode.disconnectChild(3); // from this node
  94.  
  95. Node newRight = new Node(); // make new node, goes to the right of the node being split
  96.  
  97. if(thisNode==root) // if this is the root,
  98. {
  99. root = new Node(); // make new root
  100. parent = root; // root is our parent
  101. root.connectChild(0, thisNode); // connect to parent
  102. }
  103. else // this node not the root
  104. parent = thisNode.getParent(); // get parent
  105.  
  106. // deal with parent
  107. itemIndex = parent.insertItem(itemB); // item B is inserted in the parent node
  108. int n = parent.getNumItems(); // total items?
  109.  
  110. for(int j=n-1; j>itemIndex; j--) // move parent's
  111. { // connections
  112. Node temp = parent.disconnectChild(j); // one child
  113. parent.connectChild(j+1, temp); // to the right
  114. }
  115. // connect newRight to parent
  116. parent.connectChild(itemIndex+1, newRight);
  117.  
  118. // deal with newRight
  119. newRight.insertItem(itemC); // item C to newRight
  120. newRight.connectChild(0, child2); // connect to 0 and 1
  121. newRight.connectChild(1, child3); // on newRight
  122. } // end split()
  123. // -------------------------------------------------------------
  124. // gets appropriate child of node during search for value
  125. // we put in a parent node, and a value which will determine which child we pick
  126. // we will either pick the right child of a value, or the left child depending on which is greater
  127. public Node getNextChild(Node theNode, long theValue)
  128. {
  129. int j;
  130. // assumes node is not empty, not full, not a leaf
  131. int numItems = theNode.getNumItems();
  132. for(j=0; j<numItems; j++) // for each item in node
  133. {
  134. if( theValue < theNode.getItem(j).dData ) // is the value given less than the value at the first index of the array?
  135. return theNode.getChild(j); // if so, return left child so we can use it at another point in time
  136. } // end for // otherwise, our value is greater we're greater, so
  137. return theNode.getChild(j); // we return right child (the child right after the value given, greater)
  138. }
  139. // -------------------------------------------------------------
  140. public void displayTree()
  141. {
  142. recDisplayTree(root, 0, 0);
  143. }
  144. // -------------------------------------------------------------
  145. private void recDisplayTree(Node thisNode, int level,
  146. int childNumber)
  147. {
  148. System.out.print("level="+level+" child="+childNumber+" ");
  149. thisNode.displayNode(); // display this node
  150.  
  151. // call ourselves for each child of this node
  152. int numItems = thisNode.getNumItems();
  153. for(int j=0; j<numItems+1; j++)
  154. {
  155. Node nextNode = thisNode.getChild(j);
  156. if(nextNode != null)
  157. recDisplayTree(nextNode, level+1, j);
  158. else
  159. return;
  160. }
  161. } // end recDisplayTree()
  162. // -------------------------------------------------------------\
  163. } // end class Tree234
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement