0
이진 검색 트리 삭제를위한 코드입니다. 트리에 요소를 삽입하고 인쇄하려고하면 나타나는 값은 0입니다. 디버그 기술을 사용해 보았습니다. root.key
요소가 inorderRec() method
의 사용자 삽입 요소를 인쇄하지 않기 때문에 오류가 "void insert()"
메서드에서 발생하는 것 같습니다. 나는 아직도 나무 DS를 배우고있다. 선배들 덕분에.이진 검색 트리 -> Inorder 순회 방식이 삽입 된 값을 인쇄하지 않습니다.
노드 클래스 :
class Node {
int key; //elements
Node left, right; //positions
//constructor
public Node(int item) {
item = key;
left = right = null;
}
}
BST의 등급 (class) :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package binarysearchdeletion;
/**
*
* @author Srt
*/
class BinarySearchDeletion {
Node root;
public BinarySearchDeletion() {
root = null;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
BinarySearchDeletion tree = new BinarySearchDeletion();
System.out.println("print the statement");
tree.insert(50);
tree.inorder();
}
//delete method,, to delete keys
void delete(int key) {
root = deleteRec(root, key);
}
//delete recursion method
Node deleteRec(Node root, int key) {
//if tree is empty return the root
if (root == null) {
return root;
}
//deleteing the leaf node based on the input
if (key < root.key) {
root.left = deleteRec(root.left, key);
} else if (key > root.key) {
root.right = deleteRec(root.right, key);
} //deleting node when the parent has only one child.
else {
// node with only one child or no child
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
//deleting node when the parent has two children(inorder traversal-> the samllest in the right sub-tree)
root.key = minValue(root.right);
//delete the successor when it is placed and copied to the position of the deleted parent
root.right = deleteRec(root.right, root.key);
}
return root;
}
//method for calling the samllest element greater than the input node to be deleted
int minValue(Node root) {
int minval = root.key;
while (root.left != null) {
minval = root.left.key;
root = root.left;
}
return minval;
}
//calls the insert recursion method
void insert(int key) {
// System.out.println(key);
root = insertRec(root, key); //the problem is here
// System.out.println("insert method "+root.key);
}
//inserting recursion method inserting the elements based on the control structure conditions
Node insertRec(Node root, int key) {
//if tree empty
if (root == null) {
root = new Node(key);
return root;
}
//inserting based on the BST properties and recur down
if (key < root.key) {
root.left = insertRec(root.left, key);
} else if (key > root.key) {
root.right = insertRec(root.right, key);
}
return root;
}
//calls inorder recursion method
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key + " ");
inorderRec(root.right);
}
}
void inorder() {
// System.out.println(key);
inorderRec(root);
}
}
내가 무엇입니까
샘플 출력 :
print the statement
0
예상 출력 :
print the statement
50