Advertisement
Guest User

Node

a guest
Jul 28th, 2014
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1.  
  2. ////////////////////////////////////////////////////////////////
  3. class Node
  4. {
  5. private static final int ORDER = 4;
  6. private int numItems; // number of items currently in a node
  7. private Node parent; // the node's parent
  8. private Node childArray[] = new Node[ORDER]; // four cells long, holds references to children the node might have
  9. private DataItem itemArray[] = new DataItem[ORDER-1]; // three cells long, holds references to DataItem contained in the node
  10. // data items in itemArray may need to be shifted to make room to insert/remove in ORDER.
  11. // itemArray is considered to be an ordered array
  12. // -------------------------------------------------------------
  13. // connect child to this node
  14. public void connectChild(int childNum, Node child)
  15. {
  16. childArray[childNum] = child;
  17. if(child != null)
  18. child.parent = this;
  19. }
  20. // -------------------------------------------------------------
  21. // disconnect child from this node, return it
  22. public Node disconnectChild(int childNum)
  23. {
  24. Node tempNode = childArray[childNum];
  25. childArray[childNum] = null;
  26. return tempNode;
  27. }
  28. // -------------------------------------------------------------
  29. public Node getChild(int childNum)
  30. { return childArray[childNum]; }
  31. // -------------------------------------------------------------
  32. public Node getParent()
  33. { return parent; }
  34. // -------------------------------------------------------------
  35. public boolean isLeaf()
  36. // returns true if childArray[0] == null, and false if not
  37. // basically, if the childArray at the first location is null, then the
  38. // parent array is actually a leaf and there is no childArray
  39. { return (childArray[0]==null) ? true : false; }
  40. // -------------------------------------------------------------
  41. public int getNumItems() // get number of items currently in node
  42. { return numItems; }
  43. // -------------------------------------------------------------
  44. public DataItem getItem(int index) // get DataItem at index
  45. { return itemArray[index]; }
  46. // -------------------------------------------------------------
  47. public boolean isFull() // if # of items = 3, array is full, returns true, false if not full
  48. { return (numItems==ORDER-1) ? true : false; }
  49. // -------------------------------------------------------------
  50. // searches through a node for a data item with a particular key, returns index of item
  51. public int findItem(long key) // return index of item (within node)
  52. {
  53. for(int j=0; j<ORDER-1; j++) // go through array of data items
  54. {
  55. if(itemArray[j] == null) // if we go through each item and end at null
  56. break; // break out of loop, not found, return -1
  57. else if(itemArray[j].dData == key) // if the items data matches the key
  58. return j; // return the index that it belongs to
  59. }
  60. return -1; // return -1
  61. } // end findItem
  62. // -------------------------------------------------------------
  63. // find the minimum value from a set of data
  64. public long getMin() {
  65. return itemArray[0].dData; // item at [0] should be smallest data item
  66. } // end findMin()
  67. // -------------------------------------------------------------
  68. // inserts a new data item into a node, moving existing items if necessary
  69. public int insertItem(DataItem newItem)
  70. {
  71. // assumes node is not full
  72. numItems++; // will add new item
  73. long newKey = newItem.dData; // key of new item
  74.  
  75. for(int j=ORDER-2; j>=0; j--) // start on right (for j = 2 until it goes down to 0, decrement)
  76. { // examine items
  77. if(itemArray[j] == null) // if item null,
  78. continue; // skips current iteration of the loop, go left one cell
  79. else // if item array is not null
  80. {
  81. long itsKey = itemArray[j].dData; // get its key
  82. if(newKey < itsKey) // if the new data item is smaller than the key we are comparing
  83. itemArray[j+1] = itemArray[j]; // shift the old data item to the right (rules of 2-3-4 tree, sorts it)
  84. else // else the new key is bigger
  85. {
  86. itemArray[j+1] = newItem; // insert new item right of old item
  87. return j+1; // return index to
  88. } // new item
  89. } // end else (not null)
  90. } // end for // shifted all items,
  91. itemArray[0] = newItem; // if there is no contents in the item array, insert into first position
  92. return 0; // return the index
  93. } // end insertItem()
  94. // -------------------------------------------------------------
  95. public DataItem removeItem() // remove largest item
  96. {
  97. // assumes node not empty
  98. DataItem temp = itemArray[numItems-1]; // save item
  99. itemArray[numItems-1] = null; // disconnect it
  100. numItems--; // one less item
  101. return temp; // return item
  102. }
  103. // -------------------------------------------------------------
  104. public void displayNode() // format "/24/56/74/"
  105. {
  106. for(int j=0; j<numItems; j++)
  107. itemArray[j].displayItem(); // "/56"
  108. System.out.println("/"); // final "/"
  109. }
  110. // -------------------------------------------------------------
  111. } // end class Node
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement