您好,登錄后才能下訂單哦!
一:概念
二叉搜索樹又稱二叉排序樹,它或者是一棵空樹**,或者是具有以下性質的二叉樹:
若它的左子樹不為空,則左子樹上所有節點的值都小于根節點的值
若它的右子樹不為空,則右子樹上所有節點的值都大于根節點的值
它的左右子樹也分別為二叉搜索樹。
二:操作——查找
先和根節點做對比,相等返回,如果不相等,
關鍵碼key>根節點key,在右子樹中找(root=root.rightChild)
關鍵碼key<根節點key,在左子樹中找(root=root.leftChild)
否則返回false
三:操作——插入
根據二叉排序樹的性質,左孩子比根節點的值小,右孩子比根節點的值大。關鍵碼key先于根節點key作比較,然后再判斷與根節點的左或者右作比較,滿足二叉排序樹性質時,即為合理位置,然后插入。
四: 操作-刪除(難點)
設待刪除結點為 cur, 待刪除結點的雙親結點為 parent
1. cur.left == null
五:實現
public class BinarySearchTree<K extends Comparable<K>, V>
{
public static class Node<K extends Comparable<K>, V>
{
K key;
V value;
Node<K, V> left;
Node<K, V> right;
public String toString()
{
return String.format("{%s, %s}", key, value);
}
}
private Node<K, V> root = null;
public V get(K key)
{
Node<K, V> parent = null;
Node<K, V> cur = root;
while (cur != null)
{
parent = cur;
int r = key.compareTo(cur.key);
if (r == 0)
{
return cur.value;
}
else if (r < 0) {
cur = cur.left;
}
else
{
cur = cur.right;
}
}
return null;
}
public V put(K key, V value)
{
if (root == null)
{ root = new Node<>();
root.key = key;
root.v
display(root);
return null;
}
Node<K, V> parent = null;
Node<K, V> cur = root;
while (cur != null)
{
parent = cur;
int r = key.compareTo(cur.key);
if (r == 0)
{
V oldValue = cur.value;
cur.value = value;
display(root);
return oldValue;
}
else if (r < 0)
{
cur = cur.left;
}
else
{
cur = cur.right;
}
}
Node<K, V> node = new Node<>();
node.key = key;
node.value = value;
int r = key.compareTo(parent.key);
if (r < 0)
{ parent.left = node;
}
else { parent.right = node;
}
display(root);
return null;
}
public V remove(K key)
{
Node<K, V> parent = null;
Node<K, V> cur = root;
while (cur != null)
{
int r = key.compareTo(cur.key);
if (r == 0)
{
V oldValue = cur.value;
deleteNode(parent, cur);
display(root);
return oldValue; }
else if (r < 0)
{ parent = cur; cur = cur.left; }
else { parent = cur; cur = cur.right;
}
}
display(root);
return null;
}
private void deleteNode(Node<K,V> parent, Node<K,V> cur)
{
if (cur.left == null)
{
if (cur == root)
{
root = cur.right;
}
else if (cur == parent.left)
{ parent.left = cur.right; }
else { parent.right = cur.right; }
} else if (cur.right == null)
{
if (cur == root)
{ root = cur.left; }
else if (cur == parent.left)
{ parent.left = cur.left; }
else { parent.right = cur.left; }
} else {
// 去 cur 的右子樹中尋找最小的 key 所在的結點 scapegoat
// 即 scapegoat.left == null 的結點
Node<K,V> goatParent = cur;
Node<K,V> scapegoat = cur.right;
while (scapegoat.left != null)
{ goatParent = scapegoat; scapegoat = cur.left; }
cur.key = scapegoat.key;
cur.value = scapegoat.value;
if (scapegoat == goatParent.left)
{
goatParent.left = scapegoat.right;
}
else { goatParent.right = scapegoat.right; }
}
}
private static <K extends Comparable<K>,V> void display(Node<K,V> node)
{
System.out.print("前序: ");
preOrder(node);
System.out.println();
System.out.print("中序: ")
inOrder(node);
System.out.println(); }
private static <K extends Comparable<K>,V> void preOrder(Node<K,V> node)
{ if (node == null)
{ return; }
System.out.print(node + " ");
preOrder(node.left);
preOrder(node.right); }
private static <K extends Comparable<K>,V> void inOrder(Node<K,V> node)
{ if (node == null)
{ return; }
inOrder(node.left);
System.out.print(node + " ");
inOrder(node.right); }
public static void main(String[] args)
{
BinarySearchTree<Integer, String> tree = new BinarySearchTree<>();
int[] keys = { 5, 3, 7, 4, 2, 6, 1, 9, 8 };
for (int key : keys)
{
tree.put(key, String.valueOf(key)); }
System.out.println("=================================="); tree.put(3, "修改過的 3"); System.out.println("=================================="); tree.remove(9);
tree.remove(1); t
ree.remove(3);
``` }
}
**六:性能分析**
插入和刪除操作都必須先查找,查找效率代表了二叉搜索樹中各個操作的性能。
對有n個結點的二叉搜索樹,若每個元素查找的概率相等,則二叉搜索樹平均查找長度是結點在二叉搜索樹的深度的函數,即結點越深,則比較次數越多。
但對于同一個關鍵碼集合,如果各關鍵碼插入的次序不同,可能得到不同結構的二叉搜索樹。
**七: 和 java 類集的關系**
TreeMap 和 TreeSet 即 java 中利用搜索樹實現的 Map 和 Set;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。