# 算法导论-7.二叉查找树

## 二叉查找树

template <typename T> class xBinaryTreeNode{
public:
xBinaryTreeNode(T input);
T data;
xBinaryTreeNode<T>* leftChild;
xBinaryTreeNode<T>* rightChild;
xBinaryTreeNode<T>* father;
};
template <typename T> xBinaryTreeNode<T>::xBinaryTreeNode(T input){
data = input;
leftChild = NULL;
rightChild = NULL;
father = NULL;
}

template <typename T> class xBinarySearchTree{
public:
xBinarySearchTree(T dv);    // dv -> default Null Value
bool insertValue(T value);
bool deleteNode(xBinaryTreeNode<T>* node);
void inorderTreeWalk();
xBinaryTreeNode<T>* treeSearch(T value);
T treeMin();
T treeMax();
private:
T dValue;
xBinaryTreeNode<T>* successor(xBinaryTreeNode<T>* node);
xBinaryTreeNode<T>* predecessor(xBinaryTreeNode<T>* node);
};
template <typename T> xBinarySearchTree<T>::xBinarySearchTree(T dv){
dValue = dv;
}

template <typename T> void xBinarySearchTree<T>::inorderTreeWalk(){
}
template <typename T> void xBinarySearchTree<T>::inorderTreeWalk(xBinaryTreeNode<T>* headNode){
}
template <typename T> bool xBinarySearchTree<T>::insertValue(T value){
xBinaryTreeNode<T>* father = NULL;
xBinaryTreeNode<T>* child = new xBinaryTreeNode<T>(value);
while(tmp!=NULL){
father = tmp;
tmp = (tmp->data>=value) ? tmp->leftChild : tmp->rightChild;
}
if (father == NULL){head = child;}
else{
if (father->data>=value){father->leftChild = child;}
else{father->rightChild = child;}
child->father = father;
}
return true;
}
template <typename T> xBinaryTreeNode<T>* xBinarySearchTree<T>::treeSearch(T value){
}
template <typename T> xBinaryTreeNode<T>* xBinarySearchTree<T>::treeSearch(T value, xBinaryTreeNode<T>* headNode){
if (tmp == NULL){return NULL;}
if (tmp->data == value){return tmp;}
if (tmp->data < value){return treeSearch(value, tmp->rightChild);}
if (tmp->data > value){return treeSearch(value, tmp->leftChild);}
return NULL;
}
template <typename T> T xBinarySearchTree<T>::treeMin(){
if (tmp != NULL){return tmp->data;}
else{return dValue;}
}
template <typename T> T xBinarySearchTree<T>::treeMax(){
if (tmp != NULL){return tmp->data;}
else{return dValue;}
}
template <typename T> xBinaryTreeNode<T>* xBinarySearchTree<T>::treeMinNode(xBinaryTreeNode<T>* headNode){
while (tmp->leftChild != NULL){tmp = tmp->leftChild;}
return tmp;
}
template <typename T> xBinaryTreeNode<T>* xBinarySearchTree<T>::treeMaxNode(xBinaryTreeNode<T>* headNode){
while (tmp->rightChild != NULL){
tmp = tmp->rightChild;
}
return tmp;
}

template <typename T> bool xBinarySearchTree<T>::deleteNode(xBinaryTreeNode<T>* node){
if (node->leftChild == NULL && node->rightChild==NULL){
if (node->father == NULL){head = NULL;}
else if (node->father->leftChild == node){node->father->leftChild = NULL;}
else{node->father->rightChild = NULL;}
}
else if (node->leftChild == NULL && node->rightChild != NULL){
if (node->father == NULL){head = node->rightChild;}
else if (node->father->leftChild == node){node->father->leftChild = node->rightChild;}
else{node->father->rightChild = node->rightChild;}
}
else if (node->leftChild != NULL && node->rightChild == NULL){
if (node->father == NULL){head = node->leftChild;}
else if (node->father->leftChild == node){node->father->leftChild = node->leftChild;}
else{node->father->rightChild = node->leftChild;}
}
else{
xBinaryTreeNode<T>* tmpNode = successor(node);
T tmpValue = tmpNode->data;
deleteNode(tmpNode);
node->data = tmpValue;
}
return true;
}

15 6 18 3 7 17 13

template <typename T> xBinaryTreeNode<T>* xBinarySearchTree<T>::successor(xBinaryTreeNode<T>* node){
if (node->rightChild != NULL){return treeMinNode(node->rightChild);}
else if (node->father != NULL){
xBinaryTreeNode<T>* tmp = node;
while(tmp->father!=NULL){
if (tmp->father->leftChild == tmp){
return tmp->father;
}
tmp = tmp->father;
}
return node;
}
else{return NULL;}
}
template <typename T> xBinaryTreeNode<T>* xBinarySearchTree<T>::predecessor(xBinaryTreeNode<T>* node){
if (node->leftChild != NULL){return treeMaxNode(node->leftChild);}
else if (node->father != NULL){
xBinaryTreeNode<T>* tmp = node;
while(tmp->father!=NULL){
if (tmp->father->rightChild == tmp){
return tmp->father;
}
tmp = tmp->father;
}
return node;
}
else{return NULL;}
}

1. 很显然的，二叉树中节点的平均深度：
$$\frac{1}{n}\sum_{x\in T}d(x,T)=\frac{1}{n}P(T)$$
2. 假设 $T_{L}$ 和 $T_{R}$ 为树 $T$ 的左右子树。$T$ 的 $n$ 个节点，除去根节点，左右子树中共有 $n-1$ 各节点，每个节点在子树 $T_{L}$ 或 $T_{R}$ 中的深度比在树 $T$ 中的深度小1，所以：
$$P(T)=P(T_{L})+P(T_{R})+n-1$$
3. 二叉查找树是随机构造的，也就是说，含有 $n$ 个节点的树 $T$ 的根节点在这 $n$ 个节点中的顺序为 $1,2,3...n$ 都是等可能的，概率为 $1/n$ 。所以平均路径总长度为：
$$P(n)=\frac{1}{n}\sum_{i=0}^{n-1}(P(i)+P(n-i-1)+n-1)$$
考虑对称性，上式可写为：
$$P(n)=\frac{2}{n}\sum_{k=1}^{n-1}P(k)+\Theta(n)$$
4. 这里希望证明$P(n)\leq \Theta(\lg n)$。先假设结论 $P(n)\leq \Theta(\lg n)$ 对某个 $P(n)$ 成立，则有 $P(n)\leq ak\lg k+b$，代入 $P(n)$ 的表达式得到：
$$P(n)\leq \frac{2}{n}\sum_{k=2}^{n-1}(ak\lg k+b)+\Theta(n)=\frac{2a}{n}\sum_{k=2}^{n-1}k\lg k+\frac{2b}{n}(n-2)+\Theta(n)$$
先考虑这样一个式子（一会再去证明）：
$$\sum_{k=2}^{n-1}k\lg k\leq \frac{1}{2}n^{2}\lg n+\frac{1}{8}n^{2}$$
有：
$$P(n)\leq \frac{2a}{n}(\frac{1}{2}n^{2}\lg n-\frac{1}{8}n^{2})+\frac{2b}{n}(n-2)+\Theta(n)\leq an\lg n+b$$
得证。
5. 再补充证明一下这个式子：
$$\sum_{k=2}^{n-1}k\lg k\leq \frac{1}{2}n^{2}\lg n+\frac{1}{8}n^{2}$$
将 $\sum_{k=2}^{n-1}k\lg k$ 分成两个部分：
$$\sum_{k=2}^{n-1}k\lg k<\lg(\frac{n}{2})\sum_{k=2}^{n/2-1}k+\lg n\sum_{k=n/2}^{n-1}k=\lg n\sum_{k=2}^{n-1}k-\sum_{k=2}^{n/2-1}k$$
代入求和公式，并考虑 $k=1$ 的情况，有
$$\sum_{k=2}^{n-1}k\lg k\leq \frac{1}{2}n(n-2)\lg n-\frac{1}{2}(\frac{n}{2}-1)\frac{n}{2}\leq \frac{1}{2}n^{2}\lg n+\frac{1}{8}n^{2}$$

posted @ 2013-01-09 08:51  一叶斋主人  阅读(1795)  评论(2编辑  收藏  举报