我正在研究一些简单的数据结构,而我在BST中的轮换工作正常。
public class BinarySearchTree {
private static class Node {
Node parent; //null for root node
Node left;
Node right;
int data;
//constructor for node
Node(int newData) {
left = null;
right = null;
parent = null;
data = newData;
}
}
private Node root; //root null in empty
//search methods
public boolean search(int data) {
return search(root,data); //true if exists
}
private boolean search(Node n, int data) {
if(n == null)
return false; //node doesn't exist
if(data == n.data) //found
return true;
else if(data < n.data) //data on left if exists
return search(n.left, data);
else //data on right if it exists
return search(n.right,data);
}
public Node findNode(int data) {
return findNode(root,data);
}
private Node findNode(Node n, int data) {
if(n == null)
return n; //nothing exists here
else if (data < n.data) //data should be on left if it exists
return findNode(n.left,data);
else if(data > n.data) //data should be on right if it exists
return findNode(n.right,data);
else
return n; //found it
}
//recursive insertion
public void insertNode(int data) {
root = insertNode(root,null,data);
}
private Node insertNode(Node n, Node parentNode, int data) {
if(n == null) {
n = new Node(data); //if node doesn't exist add it here
n.parent = parentNode;
}
else {
if(data <= n.data) {
n.left = insertNode(n.left,n,data);
} else {
n.right = insertNode(n.right,n,data);
}
}
return n; //return self at end (will go back to root)
}
//rotations
public void rotateLeft(Node n) {
Node y = n.right;
n.right = y.left; //y's left subtree into n's right subtree
if(y.left != null)
y.left.parent = n;
y.parent = n.parent;
//link n's parent to y
if(n.parent == null) //check to see if we are at root
root = y;
else if(n == n.parent.left) //n on left of parent
n.parent.left = y;
else //n on right of parent
n.parent.right = y;
y.left = n;
n.parent = y; //n on y's left
}
public void rotateRight(Node n) {
Node y = n.left;
n.left = y.right;
if(y.right != null)
y.right.parent = n;
y.parent = n.parent;
if(n.parent == null)
root = y;
else if(n == n.parent.right)
n.parent.right = y;
else
n.parent.left = y;
y.right = n;
n.parent = y;
}
//Printing
public void printPreorder() {
printPreorder(root);
}
private void printPreorder(Node n) {
if(n == null) //nothing here
return;
System.out.print(n.data + " "); //print node data here
printPreorder(n.left);
printPreorder(n.right);
}
public void printInorder() {
printInorder(root);
}
private void printInorder(Node n) {
if(n == null) //nothing here
return;
printInorder(n.left);
System.out.print(n.data + " "); //print data here (in order)
printInorder(n.right);
}
public void printPostorder() {
printPostorder(root);
}
private void printPostorder(Node n) {
if(n == null)
return; //nothing here
printPostorder(n.left);
printPostorder(n.right);
//node is here so print data
System.out.print(n.data + " ");
}
}
快速凌乱的主要
public class MainBST {
public static void main(String args[]) {
BinarySearchTree BST = new BinarySearchTree();
//test run
BST.insertNode(8);
BST.insertNode(7);
BST.insertNode(5);
BST.insertNode(6);
BST.insertNode(3);
BST.insertNode(4);
System.out.print("Before rotation: \nPre-order: ");
BST.printPreorder();
System.out.print("\nIn-order: ");
BST.printInorder();
System.out.print("\nPost-order: ");
BST.printPostorder();
if(BST.search(5))
BST.rotateRight(BST.findNode(5));
System.out.print("\n\nAfter rotation: \nPre-order: ");
BST.printPreorder();
System.out.print("\nIn-order: ");
BST.printInorder();
System.out.print("\nPost-order: ");
BST.printPostorder();
}
}
输出:
Before rotation:
Pre-order: 8 7 5 3 4 6
In-order: 3 4 5 6 7 8
Post-order: 4 3 6 5 7 8
After rotation:
Pre-order: 8 7 3 5 4 6
In-order: 3 4 5 6 7 8
Post-order: 4 6 5 3 7 8
然而,这是旋转后的预期输出(旋转前我有正确的树输出):
Pre-order: 8 5 3 4 7 6
In-order: 3 4 5 6 7 8
Post-order: 4 3 6 7 5 8
很明显存在一些问题。也许它与我不稳定的findNode()或节点结构有关?或者甚至旋转方法都有问题。谁能帮我吗?