|
拜托各位大佬帮我看下我的code有什么错误呀,我的一些test case 是错的,十万火急,在外求学拿分不易!
Input string: ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
Level-Order: ABCDEFGHIJKLMNOPQRSTUVWXYZ
THE CORRECT OUTPUT OF THE TEST CASE:(正确输出)
Input string: ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
Level-Order: <empty>
YOUR CODE'S OUTPUT:
Input string: DBFACEGD
Level-Order: DBFACEG
THE CORRECT OUTPUT OF THE TEST CASE:
Input string: DBFACEGD
Level-Order: EBFACG
import java.util.Scanner; // Import the Scanner class
public class BST {
public char key;
public BST left;
public BST right;
public BST() {}
public BST(char c) {
key = c;
left = null;
right = null;
}
public static BST find(BST T, char L) {
if (T == null || L == T.key)
return T;
else if (L < T.key)
return find(T.left, L);
else
return find(T.right, L);
}
public static BST insert(BST T, char L) {
if (T == null)
return new BST(L);
if (L < T.key)
T.left = insert(T.left, L);
else if (L > T.key)
T.right = insert(T.right, L);
return T;
}
public static char minValue(BST T) {
char minv = T.key;
while (T.left != null) {
minv = T.left.key;
T = T.left;
}
return minv;
}
public static BST delete(BST T, char L) {
if (T == null)
return T;
if (L < T.key)
T.left = delete(T.left, L);
else if (L > T.key)
T.right =delete(T.right, L);
else {
if (T.left == null)
return T.right;
else if (T.right == null)
return T.left;
T.key = minValue(T.right);
T.right = delete(T.right, T.key);
}
return T;
}
public static void levelOrder(BST T) {
System.out.print("Level-Order: ");
if (T==null)
{
System.out.println("<empty>");
return;
}
BST.MyQueue queue = new BST().new MyQueue();
queue.enqueue(T.key);
while (queue.front != null) {
BST tempNode = find(T, queue.dequeue().data);
System.out.print(tempNode.key);
if (tempNode.left != null) {
queue.enqueue(tempNode.left.key);
}
if (tempNode.right != null) {
queue.enqueue(tempNode.right.key);
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String stream = input.nextLine();
System.out.println("Input string: " + stream);
BST root = null;
for (int i = 0;i < stream.length();i++)
root = insert(root, stream.charAt(i));
levelOrder(root);
}
class QNode {
public char data;
public QNode next;
public QNode(char c) {
data = c;
next = null;
}
}
class MyQueue {
public QNode front;
public QNode rear;
public MyQueue() {
front = null;
rear = null;
}
public void enqueue(char c) {
QNode temp = new QNode(c);
if (front == null)
front = temp;
else
rear.next = temp;
rear = temp;
}
public QNode dequeue() {
QNode temp = null;
if (front != null) {
temp = front;
front = front.next;
}
return temp;
}
}
}
程序猿的技术大观园:www.javathinker.net
|
|