对于其他方法,程序运行良好。但是在删除中,特别是在一种情况下,当我要删除具有2个孩子的节点时,我可以执行移植部分,但是无论我尝试了什么,我都无法摆脱我刚刚替换的正确孩子和它的父母。
我尝试将其设置为null并在将其设置为null时立即将其返回(应将其删除)
打印方法工作得很好,但删除方法却不能。
这是我的输出:
INSERT 4 E 3 E 6 E 2 H 1 T 5 Y 7 S
POSTORDER
THEYSEE
INORDER
THEEYES
DELETE 6 4 1 2
PREORDER
YESS
EXIT
所以现在的问题是,为什么在我试图将密钥7的数据从7移到6的过程中,密钥7被复制了却永远都不会被删除。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean done = false;
BinarySearchTree myTree = new BinarySearchTree();
while (!done && s.hasNextLine()) {
String line = s.nextLine();
Scanner ls = new Scanner(line);
String token = ls.next();
switch (token.toUpperCase()) {
case "INSERT":
while(ls.hasNext()) {
myTree.insert(Integer.parseInt(ls.next()), ls.next());
}
continue;
case "INORDER":
myTree.inorderSearch();
System.out.println();
continue;
case "DELETE":
while(ls.hasNext()) {
myTree.deleteNode(myTree.root,Integer.parseInt(ls.next()));
}
continue;
case "PREORDER":
myTree.preorder();
System.out.println();
continue;
case "POSTORDER":
myTree.postorder();
System.out.println();
continue;
case "EXIT":
done = true;
}
}
}
}
//BINARY SEARCH TREE CLASS
class BinarySearchTree {
class Node {
int key;
String data;
Node right, left;
public Node(int item, String data) {
this.key = item;
this.data = data;
this.left = this.right = null;
}
public void setKey(int key) {
this.key = key;
}
public void setData(String data) {
this.data = data;
}
public void setRight(Node right) {
this.right = right;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public Node getLeft() {
return left;
}
public int getKey() {
return key;
}
public String getData() {
return data;
}
}
Node root;
BinarySearchTree() {
this.root = null;
}
private Node insertHelper(Node root, int key, String data) {
if (root == null) {
root = new Node( key, data);
return root;
}
if (key < root.key)
root.left = insertHelper(root.left, key, data);
else if (key > root.key)
root.right = insertHelper(root.right, key, data);
return root;
}
void insert(int key, String data) {
root = insertHelper(root, key, data);
}
void inorderSearch() {
inorderSearchHelper(root);
}
private void inorderSearchHelper(Node root) {
if (root != null) {
inorderSearchHelper(root.left);
System.out.print(root.data);
inorderSearchHelper(root.right);
}
}
public void postorder(){
postorderHelper(root);
}
private void postorderHelper(Node root) {
if (root == null)
return;
postorderHelper(root.left);
postorderHelper(root.right);
System.out.print(root.data);
}
public void preorder(){
preorderHelper(root);
}
private void preorderHelper(Node node) {
if (node == null)
return;
System.out.print(node.data);
preorderHelper(node.left);
preorderHelper(node.right);
}
// DELETE FKING NODEs!
/*private void transplant(Node root,Node u, Node v){
if(u. == )
}*/
public static Node minimumElement(Node root) {
if (root.left == null)
return root;
else {
return minimumElement(root.left);
}
}
public static Node deleteNode(Node root, int key) {
if (root == null)
return null;
if (root.key > key) {
root.left = deleteNode(root.left, key);
} else if (root.key < key) {
root.right = deleteNode(root.right, key);
} else {
// if nodeToBeDeleted have both children
if (root.left != null && root.right != null) {
Node temp = root;
// Finding minimum element from right
Node minNodeForRight = minimumElement(temp.right);
// Replacing current node with minimum node from right subtree
root.data = minNodeForRight.data;
// Deleting minimum node from right now
deleteNode(root.right, minNodeForRight.key);
}
// if nodeToBeDeleted has only left child
else if (root.left != null) {
root = root.left;
}
// if nodeToBeDeleted has only right child
else if (root.right != null) {
root = root.right;
}
// if nodeToBeDeleted do not have child (Leaf node)
else
root = null;
}
return root;
}
}
所以这是正确的输出:
INSERT 4 E 3 E 6 E 2 H 1 T 5 Y 7 S
POSTORDER
THEYSEE
INORDER
THEEYES
DELETE 6 4 1 2
PREORDER
YES
EXIT