模版是这样的
package Tree2App;
class Node {
public int data;
public void displayNode() {
System.out.print( '{ ');
System.out.print(data);
System.out.print( "} ");
}
}
class Tree2 {
Node nodes[];
public Tree2(int size) {
nodes = new Node[size];
}
public Node find(int key) {
int index = 0;
while (true) {
if (nodes[index] == null) {
return null;
}
if (key == nodes[index].data) {
return nodes[index];
} else if (key < nodes[index].data) {
index = 2 * index + 1;
} else {
index = 2 * index + 2;
}
}
}
public void insert(int data)
{
}
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
System.out.print( "\nPreorder traversal: ");
preOrder(0);
break;
case 2:
System.out.print( "\nInorder traversal: ");
inOrder(0);
break;
case 3:
System.out.print( "\nPostorder traversal: ");
postOrder(0);
break;
}
System.out.println();
}
private void preOrder(int index) {
}
private void inOrder(int index) {
if (nodes[index] == null) {
return;
} else {
inOrder(2 * index + 1);
System.out.println(nodes[index].data);
inOrder(2 * index + 2);
}
}
private void postOrder(int index) {
}
}
public class Tree2App {
public static void main(String[] args) {
}
}
这是一道关于二叉树在数组里运用的题目.程序功能是在数组里插入新的数据并且用二叉树排序.求高手帮忙啊.
------解决方案--------------------
是为了应用的话,TreeSet就是一个二叉排序树