12.4-1
證明前先需要知道有
先證明附錄 C 的練習 C.1-7
然後有:
12.4-2
首先給出第二個問題的答案,即漸進上界
下面給出一個例子,
對此例的平均深度證明是
12.4-3
先給出
從左到右依次編號爲
因此圖3被創建的概率是
12.4-4
在書的附錄裏面有凸函數的定義(見中文版第三版的P701或英文版的P1199),然後國外的語意和國內相反,所以我們是要證明
證明:根據凹函數定義:對於任意
這裏有個重要不等式,即對於任意
這樣用
帶入並化解得:
把②和③式帶入①式得:
由於
12.4-5
略。
附上本章的一些實現代碼
#include <iostream>
#include <stack>
using std::cout;
using std::endl;
using std::stack;
struct BinTree
{
int key;
BinTree *parent;
BinTree *left;
BinTree *right;
};
void preOrder(BinTree *root)
{
if(root != NULL)
{
cout << root->key << ' ';
preOrder(root->left);
preOrder(root->right);
}
}
void non_recursive_preOrder(BinTree *root)
{
stack<BinTree *> ptr;
ptr.push(root);
while(root != NULL && !ptr.empty())
{
BinTree *p = ptr.top();
cout << p->key << ' ';
ptr.pop();
if(p->right != NULL)
ptr.push(p->right);
if(p->left != NULL)
ptr.push(p->left);
}
}
void inOrder(BinTree *root)
{
if(root != NULL)
{
inOrder(root->left);
cout << root->key << ' ';
inOrder(root->right);
}
}
void non_recursive_inOrder(BinTree *root)
{
stack<BinTree *> ptr;
BinTree *p = root;
while(p != NULL || !ptr.empty())
{
while(p != NULL) //找到最左孩子
{
ptr.push(p);
p = p->left;
}
if(!ptr.empty()) //彈出,然後向右
{
p = ptr.top();
ptr.pop();
cout << p->key << ' ';
p = p->right;
}
}
}
void postOrder(BinTree *root)
{
if(root != NULL)
{
postOrder(root->left);
postOrder(root->right);
cout << root->key << ' ';
}
}
void non_recursive_postOrder(BinTree *root)
{
stack<BinTree *> ptr;
BinTree *cur;
BinTree *pre = NULL;
ptr.push(root);
while(!ptr.empty())
{
cur = ptr.top();
//當前結點沒孩子或者左(右)孩子已經訪問過,則訪問當前結點
if(cur->left == NULL && cur->right == NULL || pre != NULL && (pre == cur->left || pre == cur->right))
{
cout << cur->key << ' ';
ptr.pop();
pre = cur;
}
else
{
if(cur->right != NULL)
ptr.push(cur->right);
if(cur->left != NULL)
ptr.push(cur->left);
}
}
}
BinTree *TREE_SEARCH(BinTree *root,int value)
{
if(root == NULL)
return root;
if(root->key == value)
return root;
if(root->key < value)
TREE_SEARCH(root->right,value);
else TREE_SEARCH(root->left,value);
}
BinTree *non_recursive_TREE_SEARCH(BinTree *root,int value)
{
while(root != NULL && root->key != value)
{
if(root->key < value)
root = root->right;
else root = root->left;
}
return root;
}
BinTree *TREE_MINIMUM(BinTree *root)
{
if(root == NULL)
return root;
if(root->left == NULL)
return root;
else return TREE_MINIMUM(root->left);
}
BinTree *non_recursive_TREE_MINIMUM(BinTree *root)
{
if(root == NULL)
return root;
while(root->left != NULL)
root = root->left;
return root;
}
BinTree *TREE_MAXIMUM(BinTree *root)
{
if(root == NULL)
return root;
if(root->right == NULL)
return root;
else return TREE_MAXIMUM(root->right);
}
BinTree *non_recursive_TREE_MAXIMUM(BinTree *root)
{
if(root == NULL)
return root;
while(root->right != NULL)
root = root->right;
return root;
}
BinTree *TREE_SUCCESSOR(BinTree *p)
{
if(p == NULL)
return p;
if(p->right != NULL)
return TREE_MINIMUM(p->right);
BinTree *x = p->parent;
while(x != NULL && p == x->right)
{
p = x;
x = x->parent;
}
return x;
}
BinTree *TREE_PREDECESSOR(BinTree *p)
{
if(p == NULL)
return p;
if(p->left != NULL)
return TREE_MAXIMUM(p->left);
BinTree *x = p->parent;
while(x != NULL && p == x->left)
{
p = x;
x = x->parent;
}
return x;
}
void TREE_INSERT(BinTree **root,int key)
{
if(*root == NULL) //插入根節點
{
BinTree *z = new BinTree;
z->key = key;
z->left = z->right = NULL;
*root = z;
z->parent = NULL;
return;
}
BinTree *x = *root;
if(x->key > key) //左子樹
{
if(x->left == NULL) //左子樹爲空
{
BinTree *z = new BinTree;
z->key = key;
z->left = z->right = NULL;
z->parent = x;
x->left = z;
}
else TREE_INSERT(&(x->left),key);//以左子樹爲根遞歸插入
}
else //右子樹
{
if(x->right == NULL)
{
BinTree *z = new BinTree;
z->key = key;
z->left = z->right = NULL;
z->parent = x;
x->right = z;
}
else TREE_INSERT(&(x->right),key);
}
}
void non_recursive_TREE_INSERT(BinTree **root,int key)
{
BinTree *z = new BinTree;
z->key = key;
z->left = z->right = NULL;
BinTree *y = NULL;
BinTree *x = *root;
while(x != NULL)
{
y = x;
if(x->key < z->key)
x = x->right;
else x = x->left;
}
z->parent = y;
if(y == NULL)
*root = z;
else if(y->key < z->key)
y->right = z;
else y->left = z;
}
void TRANSPLANT(BinTree **root,BinTree *u,BinTree *v)
{
if(u->parent == NULL)
*root = v;
else if(u == u->parent->left)
u->parent->left = v;
else u->parent->right = v;
if(v != NULL)
v->parent = u->parent;
}
void TREE_DELETE(BinTree **root,int key)
{
BinTree *p = TREE_SEARCH(*root,key);
if(p == NULL)
return;
if(p->left == NULL)
TRANSPLANT(root,p,p->right);
else if(p->right == NULL)
TRANSPLANT(root,p,p->left);
else{
BinTree *y = TREE_MINIMUM(p->right);
if(y->parent != p)
{
TRANSPLANT(root,y,y->right);
y->right = p->right;
y->right->parent = y;
}
TRANSPLANT(root,p,y);
y->left = p->left;
y->left->parent = y;
}
delete p;
}
int main()
{
BinTree *root = NULL;
int array[] = {8,2,-5,1,77,-6,45,0,5};
for(int i = 0; i < 5; ++i)
TREE_INSERT(&root,array[i]); //創建樹
for(int i = 5; i < 9; ++i)
non_recursive_TREE_INSERT(&root,array[i]); //接上繼續創建,在這只是爲了測試
//分別用先中後序輸出,包括每種順序的遞歸和非遞歸
cout << "preOrder: ";
preOrder(root);
cout << endl;
cout << "non recursive preOrder: " ;
non_recursive_preOrder(root);
cout << endl;
cout << "inOrder: " ;
inOrder(root);
cout << endl;
cout << "non recursive inOrder: ";
non_recursive_inOrder(root);
cout << endl;
cout << "postOrder: ";
postOrder(root);
cout << endl;
cout << "non recursive postOrder: ";
non_recursive_postOrder(root);
cout << endl;
//查找元素,找到則輸出並找到該元素的前驅和後繼
BinTree *p = TREE_SEARCH(root,77);
if(p != NULL){
cout << "find " << p->key << endl;
cout << "it's predecessor and successor: ";
BinTree *pre = TREE_PREDECESSOR(p);
if(pre != NULL)
cout << pre->key << ' ';
else cout << "NULL" << ' ';
BinTree *succ = TREE_SUCCESSOR(p);
if(succ != NULL)
cout << succ->key << ' ';
else cout << "NULL" << ' ';
cout << endl;
}
else cout << "can not find in the tree" << endl;
p = TREE_SEARCH(root,66);
if(p != NULL){
cout << "find " << p->key << endl;
cout << "it's predecessor and successor: ";
BinTree *pre = TREE_PREDECESSOR(p);
if(pre != NULL)
cout << pre->key << ' ';
else cout << "NULL" << ' ';
BinTree *succ = TREE_SUCCESSOR(p);
if(succ != NULL)
cout << succ->key << ' ';
else cout << "NULL" << ' ';
cout << endl;
}
else cout << "can not find in the tree" << endl;
p = TREE_SEARCH(root,8);
if(p != NULL){
cout << "find " << p->key << endl;
cout << "it's predecessor and successor: ";
BinTree *pre = TREE_PREDECESSOR(p);
if(pre != NULL)
cout << pre->key << ' ';
else cout << "NULL" << ' ';
BinTree *succ = TREE_SUCCESSOR(p);
if(succ != NULL)
cout << succ->key << ' ';
else cout << "NULL" << ' ';
cout << endl;
}
else cout << "can not find in the tree" << endl;
//找到樹的最小和最大值
p = TREE_MINIMUM(root);
if(p != NULL)
cout << "minimum is: " << p->key << endl;
p = TREE_MAXIMUM(root);
if(p != NULL)
cout << "maximum is: " << p->key << endl;
//刪除操作
TREE_DELETE(&root,8);
p = TREE_SEARCH(root,8);
if(p != NULL){
cout << "find " << p->key << endl;
cout << "it's predecessor and successor: ";
BinTree *pre = TREE_PREDECESSOR(p);
if(pre != NULL)
cout << pre->key << ' ';
else cout << "NULL" << ' ';
BinTree *succ = TREE_SUCCESSOR(p);
if(succ != NULL)
cout << succ->key << ' ';
else cout << "NULL" << ' ';
cout << endl;
}
else cout << "can not find in the tree" << endl;
//刪除後中序遍歷
inOrder(root);
cout << endl;
return 0;
}