树-234树

package h_tree.B_tree234;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 234树应用
 * 
 * @author Administrator
 *
 */
public class Tree234App {
public static void main(String[] args) throws IOException {
Tree234 theTree = new Tree234();
theTree.insert(10);
theTree.insert(90);
theTree.insert(30);
theTree.insert(75);
theTree.insert(36);


int value;
while (true) {
System.out.println("请输入执行操作的第一个字母:");
System.out.println(" show , insert ,or find");
char choice = getChar();
switch (choice) {
case 's':
theTree.displayTree();


break;
case 'i':
System.out.println("请输入要插入的字符:");
value = getInt();
theTree.insert(value);
break;
case 'f':
System.out.println("请输入要查询的数字:");
value = getInt();
int found = theTree.find(value);
if (found == -1) {
System.out.println("没有找到");
} else {
System.out.println("找到");
}
break;
default:
System.out.println("输入无意义的字符");


}
}


}


private static char getChar() throws IOException {


return getString().charAt(0);
}


private static String getString() throws IOException {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
}


private static int getInt() throws NumberFormatException, IOException {


return Integer.parseInt(getString());
}
}package h_tree.B_tree234;


public class Tree234 {
private Node root = new Node();


/**
* 根据值查询数据项,并返回索引

* @param key
* @return
*/
public int find(long key) {
Node curNode = root;
int childNumber;
while (true) {
if ((childNumber = curNode.findItem(key)) != -1) {
return childNumber;
} else if (curNode.isLeaf()) {
return -1;
} else {
getNextChild(curNode, key);
}


}
}


/**
* 插入数据项

* @param dValue
*/
public void insert(long dValue) {
Node curNode = root;
DataItem tempItem = new DataItem(dValue);
while (true) {
// 判断数据项是否满了
if (curNode.isFull()) {
split(curNode);// 分割
curNode = curNode.getParent();// 获取父节点
curNode = getNextChild(curNode, dValue);// 获取下一个节点
} else if (curNode.isLeaf()) {
break;
} else {
curNode = getNextChild(curNode, dValue);
}
}
curNode.insertItem(tempItem);// 数据项插入叶节点


}


/**
* 获得子节点

* @param curNode
* @param key
* @return
*/
public Node getNextChild(Node curNode, long key) {
int j;
int numItems = curNode.getNumItems();
for (j = 0; j < numItems; j++) {
if (key < curNode.getDataItem(j).dData) {
return curNode.getChild(j);
}
}
return curNode.getChild(j);
}


/**
* 分割数据项满的节点

* @param thisNode
*/
public void split(Node thisNode) {
DataItem itemC = thisNode.removeItem();
DataItem itemB = thisNode.removeItem();


Node child2 = thisNode.getChild(2);
Node child3 = thisNode.getChild(3);


Node newRight = new Node();
Node parent;
// 判断是否为root
if (thisNode == root) {
root = new Node();
parent = root;
root.connectChild(0, thisNode);
} else {
parent = thisNode.getParent();
}


int itemIndex = parent.insertItem(itemB);// 父节点存储数据项的索引
int n = parent.getNumItems();// 获得父节点的个数
// 由于父节点中”有序“插入新的数据项,改变了父节点的子节点的引用,所以需要修改
for (int i = n - 1; i > itemIndex; i--) {
Node temp = parent.getChild(i);
parent.connectChild(i + 1, temp);
}


// 父节点插入子右节点
parent.connectChild(itemIndex + 1, newRight);


// 右节点插入数据项+连接子节点
newRight.insertItem(itemC);
newRight.connectChild(0, child2);
newRight.connectChild(1, child3);


}


/**
* 遍历
*/
public void displayTree() {
recDisplayTree(root, 0, 0);
}


private void recDisplayTree(Node thisNode, int level, int childNumber) {
System.out.print("level = " + level + " childNumber= " + childNumber
+ " ");
thisNode.displayNode();


int numItems = thisNode.getNumItems();
for (int i = 0; i < numItems + 1; i++) {
Node nextNode = thisNode.getChild(i);
if (nextNode != null) {
recDisplayTree(nextNode, level + 1, i);
} else {
return;
}
}
}
}package h_tree.B_tree234;


public 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];// 节点数据


/**
* 根据索引连接子节点

* @存储的子节点2种情况 1:为null,不进行任何操作 2:不为null,子节点添加父节点
* @param childNum
*            连接子节点的索引
* @param child
*            子节点
*/
public void connectChild(int childNum, Node child) {
childArray[childNum] = child;
if (child != null)
child.parent = this;
}


/**
* 根据索引断开子节点的连接

* @param childNum
*            子节点的索引

* @return 返回断开的子节点
*/
public Node disconnectChild(int childNum) {
Node tempNode = childArray[childNum];
childArray[childNum] = null;
return tempNode;
}


/**
* 根据索引获得子节点

* @param childNum
* @return
*/
public Node getChild(int childNum) {
return childArray[childNum];
}


/**
* 获取父节点

* @return
*/
public Node getParent() {
return parent;
}


/**
* 判断是否为叶节点

* @return
*/
public boolean isLeaf() {
return childArray[0] == null;
}


/**
* 获取节点的数据项个数

* @return
*/
public int getNumItems() {
return numItems;
}


/**
* 根据索引获取对应数据条目

* @param index
* @return
*/
public DataItem getDataItem(int index) {
return itemArray[index];
}


/**
* 判断数据项是否满

* @return
*/
public boolean isFull() {
return numItems == ORDER - 1;
}


/**
* 查询数据项值,并返回索引

* @param key
* @return
*/
public int findItem(long key) {
for (int i = 0; i < numItems; i++) {
if (itemArray[i] == null)
break;
else if (itemArray[i].dData == key) {
return i;
}
}
return -1;
}


/**
* 插入数据项,并返回索引

* @param newItem
* @return
*/
public int insertItem(DataItem newItem) {
numItems++;
long newKey = newItem.dData;
for (int i = ORDER - 2; i >= 0; i--) {
if (itemArray[i] == null)
continue;
else {
long itsKey = itemArray[i].dData;
if (itsKey > newKey) {
itemArray[i + 1] = itemArray[i];
} else {
itemArray[i + 1] = newItem;
return i + 1;
}
}
}
itemArray[0] = newItem;
return 0;
}


/**
* 删除数据项并返回数据项,从后往前删
*/
public DataItem removeItem() {
--numItems;
DataItem temp = itemArray[numItems];
itemArray[numItems] = null;
return temp;


}


/**
* 显示
*/
public void displayNode() {
for (int i = 0; i < numItems; i++) {
itemArray[i].displayItem();
System.out.print("/");
}
System.out.println();
}


}package h_tree.B_tree234;


public class DataItem {
public long dData;


public DataItem(long dData) {
this.dData = dData;
}


public void displayItem() {
System.out.print("/" + dData);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值