本文内容转自stpeace的博客 http://blog.csdn.net/stpeace
5. BST中所有结点之和(设结点个数为n,且中序遍历为等差数列)
对于BST,一定要理解透彻,下面,我们给出一个有错误的BST判定程序:
- // 程序中的isBST函数的逻辑是有错误
- #include <iostream>
- #define N 7
- using namespace std;
- // BST的结点
- typedef struct node
- {
- int key;
- struct node *lChild, *rChild;
- }Node, *BST;
- // 在给定的BST插入element, 使之称为新的BST
- bool BSTInsert(Node * &p, int element)
- {
- if(NULL == p) // 空树
- {
- p = new Node;
- p->key = element;
- p->lChild = p->rChild = NULL;
- return true;
- }
- if(element == p->key) // BST中不能有相等的值
- return false;
- if(element < p->key) // 递归
- return BSTInsert(p->lChild, element);
- return BSTInsert(p->rChild, element); // 递归
- }
- // 建立BST
- void createBST(Node * &T, int a[], int n)
- {
- T = NULL;
- int i;
- for(i = 0; i < n; i++)
- {
- BSTInsert(T, a[i]);
- }
- }
- // 生成一个结点
- Node *createNode(int i)
- {
- Node * q = new Node;
- q->lChild = NULL;
- q->rChild = NULL;
- q->key = i;
- return q;
- }
- // 建立一棵非BST树(这是之前的代码,直接复制过来的)
- Node *createNotBST()
- {
- Node *p[N] = {NULL};
- int i;
- for(i = 0; i < N; i++)
- p[i] = createNode(i + 1);
- for(i = 0; i < N/2; i++)
- {
- p[i]->lChild = p[i * 2 + 1];
- p[i]->rChild = p[i * 2 + 2];
- }
- return p[0];
- }
- // 判断是否为BST
- bool isBST(BST T)
- {
- // 空树是BST
- if(NULL == T)
- return true;
- // 只有一个结点的数是BST树
- if(NULL == T->lChild && NULL == T->rChild)
- return true;
- // 左子树为空(右子树不为空)
- if(NULL == T->lChild)
- {
- if(T->key < T->rChild->key && isBST(T->rChild))
- return true;
- return false;
- }
- // 右子树为空(左子树不为空)
- if(NULL == T->rChild)
- {
- if(T->key > T->lChild->key && isBST(T->lChild))
- return true;
- return false;
- }
- // 左右子树都非空
- if(T->lChild->key < T->key &&
- T->key < T->rChild->key &&
- isBST(T->lChild) &&
- isBST(T->rChild)
- )
- return true;
- return false;
- }
- void printJudge(Node *T)
- {
- if(isBST(T))
- cout << "yes" << endl;
- else
- cout << "no" << endl;
- }
- int main()
- {
- int a[10] = {4, 5, 2, 1, 0, 9, 3, 7, 6, 8};
- int n = 10;
- BST T = NULL;
- printJudge(T); // yes
- // 并非所有的a[]都能构造出BST,所以,最好对createBST的返回值进行判断
- createBST(T, a, n);
- printJudge(T); // yes
- T = createNotBST();
- printJudge(T); // no
- return 0;
- }
- // 程序中的isBST函数的逻辑是有错误
- #include <iostream>
- using namespace std;
- // BST的结点
- typedef struct node
- {
- int key;
- struct node *lChild, *rChild;
- }Node, *BST;
- // 生成一个结点
- Node *createNode(int i)
- {
- Node * q = new Node;
- q->lChild = NULL;
- q->rChild = NULL;
- q->key = i;
- return q;
- }
- /*
- * 下面这棵树并非BST, 但程序中的isBST将其判为BST, 故isBST函数逻辑有误
- * 该树为:
- 3 // 根节点为3
- 2 // 3的左孩子结点为2, 没有右孩子结点
- 1 4 // 2的左孩子结点为1, 右孩子结点为4
- */
- Node *createTree()
- {
- Node *p1 = createNode(1);
- Node *p2 = createNode(2);
- Node *p3 = createNode(3);
- Node *p4 = createNode(4);
- p3->rChild = NULL;
- p3->lChild = p2;
- p2->lChild = p1;
- p2->rChild = p4;
- p1->lChild = p1->rChild = NULL;
- p4->lChild = p4->rChild = NULL;
- return p3;
- }
- // 判断是否为BST
- bool isBST(BST T)
- {
- // 空树是BST
- if(NULL == T)
- return true;
- // 只有一个结点的数是BST树
- if(NULL == T->lChild && NULL == T->rChild)
- return true;
- // 左子树为空(右子树不为空)
- if(NULL == T->lChild)
- {
- if(T->key < T->rChild->key && isBST(T->rChild))
- return true;
- return false;
- }
- // 右子树为空(左子树不为空)
- if(NULL == T->rChild)
- {
- if(T->key > T->lChild->key && isBST(T->lChild))
- return true;
- return false;
- }
- // 左右子树都非空
- if(T->lChild->key < T->key &&
- T->key < T->rChild->key &&
- isBST(T->lChild) &&
- isBST(T->rChild)
- )
- return true;
- return false;
- }
- void printJudge(Node *T)
- {
- if(isBST(T))
- cout << "yes" << endl;
- else
- cout << "no" << endl;
- }
- int main()
- {
- BST T = NULL;
- printJudge(T); // yes
- T = createTree();
- printJudge(T); // yes (isBST将该树错判为BST)
- return 0;
- }
下面,我们给出正确的程序(该程序没有考虑结点值为INT_MIN的情形):
- #include <iostream>
- #define N 7
- using namespace std;
- int g_min = INT_MIN;
- // BST的结点
- typedef struct node
- {
- int key;
- struct node *lChild, *rChild;
- }Node, *BST;
- // 生成一个结点
- Node *createNode(int i)
- {
- Node * q = new Node;
- q->lChild = NULL;
- q->rChild = NULL;
- q->key = i;
- return q;
- }
- /*
- * 下面这棵树并非BST, 但程序中的isBST将其判为BST, 故isBST函数逻辑有误
- * 该树为:
- 3 // 根节点为3
- 2 // 3的左孩子结点为2, 没有右孩子结点
- 1 4 // 2的左孩子结点为1, 右孩子结点为4
- */
- Node *createTree()
- {
- Node *p1 = createNode(1);
- Node *p2 = createNode(2);
- Node *p3 = createNode(3);
- Node *p4 = createNode(4);
- p3->rChild = NULL;
- p3->lChild = p2;
- p2->lChild = p1;
- p2->rChild = p4;
- p1->lChild = p1->rChild = NULL;
- p4->lChild = p4->rChild = NULL;
- return p3;
- }
- // 建立一棵非BST树
- Node *createNotBST()
- {
- Node *p[N] = {NULL};
- int i;
- for(i = 0; i < N; i++)
- p[i] = createNode(i + 1);
- for(i = 0; i < N/2; i++)
- {
- p[i]->lChild = p[i * 2 + 1];
- p[i]->rChild = p[i * 2 + 2];
- }
- return p[0];
- }
- // 在给定的BST插入element, 使之称为新的BST
- bool BSTInsert(Node * &p, int element)
- {
- if(NULL == p) // 空树
- {
- p = new Node;
- p->key = element;
- p->lChild = p->rChild = NULL;
- return true;
- }
- if(element == p->key) // BST中不能有相等的值
- return false;
- if(element < p->key) // 递归
- return BSTInsert(p->lChild, element);
- return BSTInsert(p->rChild, element); // 递归
- }
- // 建立BST
- void createBST(Node * &T, int a[], int n)
- {
- T = NULL;
- int i;
- for(i = 0; i < n; i++)
- {
- BSTInsert(T, a[i]);
- }
- }
- // 判断是否为BST
- bool isBST(BST T)
- {
- if(NULL != T)
- {
- isBST(T->lChild);
- if(T->key <= g_min)
- return false;
- g_min = T->key;
- isBST(T->rChild);
- }
- return true;
- }
- void printJudge(Node *T)
- {
- if(isBST(T))
- cout << "yes" << endl;
- else
- cout << "no" << endl;
- }
- int main()
- {
- int a[10] = {4, 5, 2, 1, 0, 9, 3, 7, 6, 8};
- int n = 10;
- BST T = NULL;
- g_min = INT_MIN;
- printJudge(T); // yes
- T = createTree();
- g_min = INT_MIN;
- printJudge(T); // no
- T = createNotBST();
- g_min = INT_MIN;
- printJudge(T); // no
- createBST(T, a, n);
- g_min = INT_MIN;
- printJudge(T); // yes
- return 0;
- }
上面这个程序的原理是什么呢?怎么感觉有点类似于中序遍历呢?确实如此,其实就是中序遍历,原理是:
(1) 空二叉树是BST
(2) 对于非空二叉树而言:中序遍历为严格递增数列 《==》该树为BST.
- #include <iostream>
- using namespace std;
- // BST的结点
- typedef struct node
- {
- int key;
- struct node *lChild, *rChild;
- }Node, *BST;
- // 在给定的BST插入element, 使之称为新的BST
- bool BSTInsert(Node * &p, int element)
- {
- if(NULL == p) // 空树
- {
- p = new Node;
- p->key = element;
- p->lChild = p->rChild = NULL;
- return true;
- }
- if(element == p->key) // BST中不能有相等的值
- return false;
- if(element < p->key) // 递归
- return BSTInsert(p->lChild, element);
- return BSTInsert(p->rChild, element); // 递归
- }
- // 建立BST
- void createBST(Node * &T, int a[], int n)
- {
- T = NULL;
- int i;
- for(i = 0; i < n; i++)
- {
- BSTInsert(T, a[i]);
- }
- }
- // BST的查找(递归)
- Node *BSTSearch(BST T, int x)
- {
- if(NULL == T)
- return NULL;
- if(x == T->key)
- return T;
- if(x < T->key)
- return BSTSearch(T->lChild, x);
- return BSTSearch(T->rChild, x);
- }
- int main()
- {
- int a[10] = {4, 5, 2, 1, 0, 9, 3, 7, 6, 8};
- int n = 10;
- BST T = NULL;
- // 并非所有的a[]都能构造出BST,所以,最好对createBST的返回值进行判断
- createBST(T, a, n);
- Node *p = NULL;
- int x; // 待查找的x
- for(x = -10; x < 20; x++)
- {
- p = BSTSearch(T, x);
- cout << x << ":";
- if(NULL == p)
- cout << "no" << endl;
- else
- cout << "yes" << endl;
- }
- return 0;
- }
结果为:
-10:no
-9:no
-8:no
-7:no
-6:no
-5:no
-4:no
-3:no
-2:no
-1:no
0:yes
1:yes
2:yes
3:yes
4:yes
5:yes
6:yes
7:yes
8:yes
9:yes
10:no
11:no
12:no
13:no
14:no
15:no
16:no
17:no
18:no
19:no
Press any key to continue
- #include <iostream>
- using namespace std;
- // BST的结点
- typedef struct node
- {
- int key;
- struct node *lChild, *rChild;
- }Node, *BST;
- // 在给定的BST插入element, 使之称为新的BST
- bool BSTInsert(Node * &p, int element)
- {
- if(NULL == p) // 空树
- {
- p = new Node;
- p->key = element;
- p->lChild = p->rChild = NULL;
- return true;
- }
- if(element == p->key) // BST中不能有相等的值
- return false;
- if(element < p->key) // 递归
- return BSTInsert(p->lChild, element);
- return BSTInsert(p->rChild, element); // 递归
- }
- // 建立BST
- void createBST(Node * &T, int a[], int n)
- {
- T = NULL;
- int i;
- for(i = 0; i < n; i++)
- {
- BSTInsert(T, a[i]);
- }
- }
- // BST的查找(非递归)
- Node *BSTSearch(BST T, int x)
- {
- Node *p = T;
- while(NULL != p && x != p->key) // 找不到,就一直找
- {
- if(x > p->key)
- p = p->rChild;
- else
- p = p->lChild;
- }
- return p;
- }
- int main()
- {
- int a[10] = {4, 5, 2, 1, 0, 9, 3, 7, 6, 8};
- int n = 10;
- BST T = NULL;
- // 并非所有的a[]都能构造出BST,所以,最好对createBST的返回值进行判断
- createBST(T, a, n);
- Node *p = NULL;
- int x; // 待查找的x
- for(x = -10; x < 20; x++)
- {
- p = BSTSearch(T, x);
- cout << x << ":";
- if(NULL == p)
- cout << "no" << endl;
- else
- cout << "yes" << endl;
- }
- return 0;
- }
结果为:
-10:no
-9:no
-8:no
-7:no
-6:no
-5:no
-4:no
-3:no
-2:no
-1:no
0:yes
1:yes
2:yes
3:yes
4:yes
5:yes
6:yes
7:yes
8:yes
9:yes
10:no
11:no
12:no
13:no
14:no
15:no
16:no
17:no
18:no
19:no
BST的中序遍历即为严格单调的遍历,故求中序遍历即可,程序如下:
- #include <iostream>
- using namespace std;
- // BST的结点
- typedef struct node
- {
- int key;
- struct node *lChild, *rChild;
- }Node, *BST;
- // 在给定的BST插入element, 使之称为新的BST
- bool BSTInsert(Node * &p, int element)
- {
- if(NULL == p) // 空树
- {
- p = new Node;
- p->key = element;
- p->lChild = p->rChild = NULL;
- return true;
- }
- if(element == p->key) // BST中不能有相等的值
- return false;
- if(element < p->key) // 递归
- return BSTInsert(p->lChild, element);
- return BSTInsert(p->rChild, element); // 递归
- }
- // 建立BST
- void createBST(Node * &T, int a[], int n)
- {
- T = NULL;
- int i;
- for(i = 0; i < n; i++)
- {
- BSTInsert(T, a[i]);
- }
- }
- void inOrderTraverse(BST T)
- {
- if(NULL != T)
- {
- inOrderTraverse(T->lChild);
- cout << T->key << endl;
- inOrderTraverse(T->rChild);
- }
- }
- int main()
- {
- int a[10] = {4, 5, 2, 1, 0, 9, 3, 7, 6, 8};
- int n = 10;
- BST T = NULL;
- // 并非所有的a[]都能构造出BST,所以,最好对createBST的返回值进行判断
- createBST(T, a, n);
- inOrderTraverse(T);
- return 0;
- }
5. BST中所有结点之和(设结点个数为n,且中序遍历为等差数列)
其实,就是要求最小值和最大值之和, 程序如下:
- #include <iostream>
- using namespace std;
- // BST的结点
- typedef struct node
- {
- int key;
- struct node *lChild, *rChild;
- }Node, *BST;
- // 在给定的BST插入element, 使之称为新的BST
- bool BSTInsert(Node * &p, int element)
- {
- if(NULL == p) // 空树
- {
- p = new Node;
- p->key = element;
- p->lChild = p->rChild = NULL;
- return true;
- }
- if(element == p->key) // BST中不能有相等的值
- return false;
- if(element < p->key) // 递归
- return BSTInsert(p->lChild, element);
- return BSTInsert(p->rChild, element); // 递归
- }
- // 建立BST
- void createBST(Node * &T, int a[], int n)
- {
- T = NULL;
- int i;
- for(i = 0; i < n; i++)
- {
- BSTInsert(T, a[i]);
- }
- }
- int minPlusMax(BST T)
- {
- Node *p = T;
- while(NULL != T->lChild) // 一路向左狂奔
- T = T->lChild;
- while(NULL != p->rChild) // 一路向右狂奔
- p = p->rChild;
- return T->key + p->key;
- }
- int main()
- {
- int a[10] = {4, 5, 2, 1, 0, 9, 3, 7, 6, 8};
- int n = 10;
- BST T = NULL;
- // 并非所有的a[]都能构造出BST,所以,最好对createBST的返回值进行判断
- createBST(T, a, n);
- // minPlusMax(T) * n 必然为偶数,所以不同担心截断
- cout << minPlusMax(T) * n / 2 << endl;
- return 0;
- }