[英]Insert method in Binary Search Tree
當我將節點插入二叉樹並 go 主要通過按順序打印來測試它時 output 總是錯誤的錯誤的順序我檢查了它的完美顯示,但問題在於插入方法(插入方法插入非重復鍵)。
class BSTNode<T> {
public int key;
public T data;
public BSTNode<T> left, right;
public BSTNode(int k, T val) {
key = k;
data = val;
left = right = null;
}
public BSTNode(int k, T val, BSTNode<T> l, BSTNode<T> r) {
key = k;
data = val;
left = l;
right = r;
}
}
public class BST<T> {
BSTNode<T> root, current;
public BST() {
root = current = null;
}
public boolean empty() {
return root == null;
}
public boolean full() {
return false;
}
public T retrieve() {
return current.data;
}
public boolean findkey(int tkey) {
BSTNode<T> p = root, q = root;
if (empty())
return false;
int nb = 0;
while (p != null) {
q = p;
nb++;
if (p.key == tkey) {
current = p;
return true;
} else if (tkey < p.key)
p = p.left;
else
p = p.right;
}
current = q;
return false;
}
public boolean insert(int key, T val) {
BSTNode<T> p = current, q = current;
while (p != null) {
q = p;
if (p.key == key) {
return false;
} else if (key < p.key)
p = p.left;
else
p = p.right;
}
p = new BSTNode<T>(key, val);
if (empty()) {
root = current = p;
return true;
} else {
// current is pointing to parent of the new key
if (key < current.key)
current.left = p;
else
current.right = p;
current = p;
return true;
}
}
public void display() {
if (root == null)
System.out.println("BST IS EMPTY");
else
displayin(root);
System.out.println();
}
private void displayin(BSTNode<T> p) {
if (p != null) {
displayin(p.left);
System.out.print(p.key + " " + p.data + " , ");
displayin(p.right);
}
}
}
在您的insert
方法中,第一個代碼部分:
while (p != null) {
q = p;
if (p.key == key) {
current = p;
return true;
} else if (key < p.key)
p = p.left;
else
p = p.right;
}
當找到要寫入的密鑰時,只需返回true
。 但是,它根本不會在此處插入val
。 您可以通過在某棵樹上調用 insert 方法然后打印該樹並將您預期的結果與實際結果進行比較來檢測到這一點。 在自動形式中,您可以使用 JUnit 測試來執行此操作。
我想我知道你想要(現在)。 我已將“display”-Method 更改為 toString,重寫了 insert 並為 toString 實現了一種bfs形式。
import java.util.ArrayList;
class BSTNode<T> {
public int key;
public T data;
public BSTNode<T> left, right;
public BSTNode(int k, T val) {
key = k;
data = val;
left = right = null;
}
public BSTNode(int k, T val, BSTNode<T> l, BSTNode<T> r) {
key = k;
data = val;
left = l;
right = r;
}
@Override
public String toString() {
return "[" + key + ":" + data + "]";
}
}
public class BST<T> {
BSTNode<T> root, current;
public BST() {
root = current = null;
}
public boolean empty() {
return root == null;
}
public T retrieve() {
return current.data;
}
public boolean findkey(int tkey) {
BSTNode<T> current = root;
while (current != null) {
if (current.key == tkey)
return true;
if (tkey < current.key)
current = current.left;
else
current = current.right;
}
return false;
}
public boolean insert(int key, T val) {
BSTNode<T> node = new BSTNode<>(key, val);
if (root == null) {
root = node;
} else {
BSTNode<T> current = root;
while (current != null) {
if (key == current.key)
return false;
if (key < current.key) {
if (current.left == null) {
current.left = node;
return true;
}
current = current.left;
} else {
if (current.right == null) {
current.right = node;
return true;
}
current = current.right;
}
}
}
return false;
}
public String toString() {
if (root == null)
return "BST IS EMPTY";
ArrayList<BSTNode<T>> visited = new ArrayList<>();
ArrayList<BSTNode<T>> queue = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty()) {
BSTNode<T> vertex = queue.remove(0);
if (vertex != null) {
queue.add(vertex.left);
queue.add(vertex.right);
}
visited.add(vertex);
}
return visited.toString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.