您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Java怎么實現(xiàn)搜索算法二叉搜索樹”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java怎么實現(xiàn)搜索算法二叉搜索樹”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
/**
* user:ypc;
* date:2021-05-18;
* time: 15:09;
*/
class Node {
int val;
Node left;
Node right;
Node(int val) {
this.val = val;
}
}
public void insert(int key) {
Node node = new Node(key);
if (this.root == null) {
root = node;
}
Node cur = root;
Node parent = null;
while (cur != null) {
if (cur.val == key) {
//System.out.println("元素已經(jīng)存在");
return;
} else if (cur.val > key) {
parent = cur;
cur = cur.left;
} else {
parent = cur;
cur = cur.right;
}
}
if (key > parent.val) {
parent.right = node;
} else {
parent.left = node;
}
}
public boolean search(int key) {
Node cur = root;
while (cur != null) {
if (cur.val == key) {
return true;
} else if (cur.val > key) {
cur = cur.left;
} else {
cur = cur.right;
}
}
return false;
}
public void removenode1(Node parent, Node cur) {
if (cur.left == null) {
if (cur == root) {
root = cur.right;
} else if (cur == parent.right) {
parent.left = cur.right;
} else {
parent.right = cur.right;
}
} else if (cur.right == null) {
if (cur == root) {
root.left = cur;
} else if (cur == parent.right) {
parent.right = cur.left;
} else {
parent.left = cur.left;
}
} else {
Node tp = cur;
Node t = cur.right;
while (t.left != null) {
tp = t;
t = t.left;
}
if (tp.left == t) {
cur.val = t.val;
tp.left = t.right;
}
if (tp.right == t) {
cur.val = t.val;
tp.right = t.right;
}
}
}
public void remove(int key) {
Node cur = root;
Node parent = null;
while (cur != null) {
if (cur.val == key) {
removenode1(parent, cur);
//removenode2(parent, cur);
return;
} else if (key > cur.val) {
parent = cur;
cur = cur.right;
} else {
parent = cur;
cur = cur.left;
}
}
}
public void removenode2(Node parent, Node cur) {
if (cur.left == null) {
if (cur == root) {
root = cur.right;
} else if (cur == parent.right) {
parent.left = cur.right;
} else {
parent.right = cur.right;
}
} else if (cur.right == null) {
if (cur == root) {
root.left = cur;
} else if (cur == parent.right) {
parent.right = cur.left;
} else {
parent.left = cur.left;
}
} else {
Node tp = cur;
Node t = cur.left;
while (t.right != null) {
tp = t;
t = t.right;
}
if (tp.right == t) {
cur.val = t.val;
tp.right = t.left;
}
if (tp.left == t) {
cur.val = t.val;
tp.left = t.left;
}
}
}
/**
* user:ypc;
* date:2021-05-18;
* time: 15:09;
*/
class TestBinarySearchTree {
public static void main(String[] args) {
int a[] = {5, 3, 4, 1, 7, 8, 2, 6, 0, 9};
BinarySearchTree binarySearchTree = new BinarySearchTree();
for (int i = 0; i < a.length; i++) {
binarySearchTree.insert(a[i]);
}
binarySearchTree.inOrderTree(binarySearchTree.root);
System.out.println();
binarySearchTree.preOrderTree(binarySearchTree.root);
binarySearchTree.remove(7);
System.out.println();
System.out.println("方法一刪除后");
binarySearchTree.inOrderTree(binarySearchTree.root);
System.out.println();
binarySearchTree.preOrderTree(binarySearchTree.root);
}
}
讀到這里,這篇“Java怎么實現(xiàn)搜索算法二叉搜索樹”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。