Advertisement
Guest User

Tree234

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