SHOW:
|
|
- or go back to the newest paste.
1 | - | /* |
1 | + | |
2 | - | Project 10.1 |
2 | + | |
3 | - | Directions: Write a method that returns the minimum value in a 2-3-4 tree |
3 | + | |
4 | class Tree234 | |
5 | - | Plan: |
5 | + | |
6 | - | - The 2-3-4 tree's minimum should ideally be the left most node's child (which will be a leaf) |
6 | + | |
7 | - | - ie. xxx |
7 | + | |
8 | - | / \ |
8 | + | |
9 | - | xxx xxx |
9 | + | |
10 | - | / \ |
10 | + | |
11 | - | oxx xxx |
11 | + | |
12 | - | where o is the minimum value |
12 | + | |
13 | curNode = getNextChild(curNode, min); | |
14 | - | - return the min value, we should write a method that returns a long (value) |
14 | + | |
15 | - | - should just return the left most value |
15 | + | |
16 | - | - Node has a getMin() method that returns the item at the left most value (represents the smallest amount in current node) |
16 | + | |
17 | - | - if the parent at getMin() is not a leaf, go to the left child of that parent and call it again |
17 | + | |
18 | - | - stop once getMin() is actually at a leaf |
18 | + | |
19 | - | _ CANT FIGURE OUT HOW TO DO THIS |
19 | + | |
20 | - | */ |
20 | + | |
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 |