Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

question: Write a method that does an inorder traverse of a 2-3-4 tree. It should display all the items in order. Add an application class

question: Write a method that does an inorder traverse of a 2-3-4 tree. It should display all the items in order. Add an application class to get an executable program that tests the method that you are asked to write. Be sure that code compiles and executes correctly.

my code below.

________________________________________________________________________________________

// tree234.java // demonstrates 234 tree // to run this program: C>java Tree234App import java.io.*; //////////////////////////////////////////////////////////////// class DataItem { public long dData; // one data item //-------------------------------------------------------------- public DataItem(long dd) // constructor { dData = dd; } //-------------------------------------------------------------- public void displayItem() // display item, format "/27" { System.out.print("/"+dData); } //-------------------------------------------------------------- } // end class DataItem //////////////////////////////////////////////////////////////// class Node { private static final int ORDER = 4; private int numItems; private Node parent; private Node childArray[] = new Node[ORDER]; private DataItem itemArray[] = new DataItem[ORDER-1]; // ------------------------------------------------------------- // connect child to this node public void connectChild(int childNum, Node child) { childArray[childNum] = child; if(child != null) child.parent = this; } // ------------------------------------------------------------- // disconnect child from this node, return it public Node disconnectChild(int childNum) { Node tempNode = childArray[childNum]; childArray[childNum] = null; return tempNode; } // ------------------------------------------------------------- public Node getChild(int childNum) { return childArray[childNum]; } // ------------------------------------------------------------- public Node getParent() { return parent; } // ------------------------------------------------------------- public boolean isLeaf() { return (childArray[0]==null) ? true : false; } // ------------------------------------------------------------- public int getNumItems() { return numItems; } // ------------------------------------------------------------- public DataItem getItem(int index) // get DataItem at index { return itemArray[index]; } // ------------------------------------------------------------- public boolean isFull() { return (numItems==ORDER-1) ? true : false; } // ------------------------------------------------------------- public int findItem(long key) // return index of { // item (within node) for(int j=0; j=0; j--) // start on right, { // examine items if(itemArray[j] == null) // if item null, continue; // go left one cell else // not null, { // get its key long itsKey = itemArray[j].dData; if(newKey < itsKey) // if it's bigger itemArray[j+1] = itemArray[j]; // shift it right else { itemArray[j+1] = newItem; // insert new item return j+1; // return index to } // new item } // end else (not null) } // end for // shifted all items, itemArray[0] = newItem; // insert new item return 0; } // end insertItem() // ------------------------------------------------------------- public DataItem removeItem() // remove largest item { // assumes node not empty DataItem temp = itemArray[numItems-1]; // save item itemArray[numItems-1] = null; // disconnect it numItems--; // one less item return temp; // return item } // ------------------------------------------------------------- public void displayNode() // format "/24/56/74/" { for(int j=0; jitemIndex; j--) // move parent's { // connections Node temp = parent.disconnectChild(j); // one child parent.connectChild(j+1, temp); // to the right } // connect newRight to parent parent.connectChild(itemIndex+1, newRight); // deal with newRight newRight.insertItem(itemC); // item C to newRight newRight.connectChild(0, child2); // connect to 0 and 1 newRight.connectChild(1, child3); // on newRight } // end split() // ------------------------------------------------------------- // gets appropriate child of node during search for value public Node getNextChild(Node theNode, long theValue) { int j; // assumes node is not empty, not full, not a leaf int numItems = theNode.getNumItems(); for(j=0; j

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

=+What is your native language?

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago