Я новичок, и в настоящее время я узнаю о BST в Java. Для этого проекта я пытаюсь удалить узел с двумя дочерними узлами, но у меня возникают проблемы с получением рекурсивного метода, чтобы вернуть правильное значение. Этот метод найдет правильное значение (19), но вернет начальное значение (0). < /P>
//Method that returns 0 instead of 19
public static int findHeir(Node node, int heir) {
System.out.println("Loop");
if (node.rightChild != null && node.leftChild != null) {
heir = node.leftChild.data;
System.out.print("Current node: " + node.rightChild.data);
System.out.println(" Current heir: " + heir);
return findHeir(node.rightChild, heir);
}
else if (node.rightChild == null) {
System.out.println("Heir value before return: " + heir);
return heir;
}
return heir;
}
< /code>
Мне нужно Findheir, чтобы вернуть 19, чтобы в методе удаления я мог переключить значение узла (12) с наследником (19) и удалить начальный узел. Почему он продолжает возвращать начальное значение? Что я могу сделать, чтобы он вернул последнее значение? < /P>
Полный воспроизводимый код: < /p>
import java.util.Scanner;
public class Main {
static class Node {
int data;
int key;
Node leftChild;
Node rightChild;
Node (int data) {
this.data = data;
leftChild = rightChild = null;
}
}
static Node root;
public Main() {
root = null;
}
//Inserts the node in the right spot
public static void insert(int key) {
root = insertNode(root, key);
}
public static Node insertNode(Node node, int key) {
if (node == null) {
node = new Node(key);
return node;
}
else if (key node.data) {
node.rightChild = insertNode(node.rightChild, key);
}
return node;
}
//Method that returns 0 instead of 19
public static int findHeir(Node node, int heir) {
System.out.println("Loop");
if (node.rightChild != null && node.leftChild != null) {
heir = node.leftChild.data;
System.out.print("Current node: " + node.rightChild.data);
System.out.println(" Current heir: " + heir);
return findHeir(node.rightChild, heir);
}
else if (node.rightChild == null) {
System.out.println("Heir value before return: " + heir);
return heir;
}
return heir;
}
//Deletes the node
public static Node delete(Node node, int key) {
//Checks if the node exists
if (node == null) {
System.out.println("The node you entered does not exist.");
return node;
}
//Looks for the correct node
else if (key < node.data) {
node.leftChild = delete(node.leftChild, key);
}
else if (key > node.data) {
node.rightChild = delete(node.rightChild, key);
}
//Found the correct node
else {
//Delete node if it has 2 children
if (node.leftChild != null && node.rightChild != null) {
int heir = 0;
findHeir(node, heir);
System.out.println("Final node value: " + node.data);
System.out.println("Final heir value: " + heir + " (should be 19)");
//TODO add code to switch heir and node.data and delete node
}
}
return node;
}
public static void main(String [] args) {
int[] treeNodes = {5, 2, 12, -4, 3, 9, 21, 19, 25};
Main binaryTree = new Main();
for (int i = 0; i < treeNodes.length; i++) {
binaryTree.insert(treeNodes);
System.out.print(treeNodes + " ");
}
System.out.println();
int key = 12;
delete(root, key);
}
}
< /code>
Дерево выглядит следующим образом: < /p>
5
/ \
2 12
/ \ / \
-4 3 9 21
/ \
19 25
Подробнее здесь: https://stackoverflow.com/questions/794 ... ction-java