#include "stdafx.h"
#define STACK_MAX_SIZE 30
#define QUEUE_MAX_SIZE 30
#ifndef elemType
typedef char elemType;
#endif
/************************************************************************/
/* 如下是關於二叉樹操做的11個簡單算法 */
/************************************************************************/
struct BTreeNode{
elemType data;
BTreeNode *left;
BTreeNode *right;
};
/* 1.初始化二叉樹 */
void initBTree(BTreeNode* *bt)
{
*bt = NULL;
return;
}
//創建二叉樹(根據a所指向的二叉樹廣義表字符串創建)
void createBTree(BTreeNode* *bt, char *a)
{
BTreeNode *p;
//定義s數組爲存儲根結點指針的棧使用
BTreeNode *s[STACK_MAX_SIZE];
//定義top做爲s棧的棧頂指針,初值爲-1,表示空棧
int top = -1;
//用k做爲處理結點的左子樹和右子樹,k = 1處理左子樹,k = 2處理右子樹
int k;
//用i掃描數組a中存儲的二叉樹廣義表字符串,初值爲0
int i = 0;
//把樹根指針置爲空,即從空樹開始創建二叉樹
*bt = NULL;
//每循環一次處理一個字符,直到掃描到字符串結束符\0爲止
while(a[i] != '\0')
{
switch(a[i])
{
case ' ':
break; /* 對空格不做任何處理 */
case '(':
if(top == STACK_MAX_SIZE - 1)
{
printf("棧空間過小!\n");
exit(1);
}
top++;
s[top] = p;
k = 1;
break;
case ')':
if(top == -1)
{
printf("二叉樹廣義表字符串錯誤!\n");
exit(1);
}
top--;
break;
case ',':
k = 2;
break;
default:
p = new BTreeNode;
p->data = a[i];
p->left = p->right = NULL;
if(*bt == NULL)
{
*bt = p;
}
else
{
if( k == 1)
{
s[top]->left = p;
}else{
s[top]->right = p;
}
}
}
//爲掃描下一個字符修改i值
i++;
}
return;
}
//檢查二叉樹是否爲空,爲空則返回1,不然返回0
int emptyBTree(BTreeNode *bt)
{
if(bt == NULL)
{
return 1;
}
else
{
return 0;
}
}
//求二叉樹深度
int BTreeDepth(BTreeNode *bt)
{
if(bt == NULL)
{
//對於空樹,返回0結束遞歸
return 0;
}
else
{
//計算左子樹的深度
int dep1 = BTreeDepth(bt->left);
//計算右子樹的深度
int dep2 = BTreeDepth(bt->right);
if(dep1 > dep2)
{
return dep1 + 1;
}
else
{
return dep2 + 1;
}
}
}
//從二叉樹中查找值爲x的結點,若存在則返回元素存儲位置,不然返回空值
elemType *findBTree(BTreeNode *bt, elemType x)
{
if(bt == NULL)
{
return NULL;
}
else
{
if(bt->data == x)
{
return &(bt->data);
}
else
{
//分別向左右子樹遞歸查找
elemType *p;
if(p = findBTree(bt->left, x))
{
return p;
}
if(p = findBTree(bt->right, x))
{
return p;
}
return NULL;
}
}
}
//弟歸遍歷輸出二叉樹(前序遍歷)
void printBTree(BTreeNode *bt)
{
//樹爲空時結束遞歸,不然執行以下操做
if(bt != NULL)
{
//根左右
printf("%c, ", bt->data);
printBTree(bt->left);
printBTree(bt->right);
}
return;
}
//清除二叉樹,使之變爲一棵空樹
void clearBTree(BTreeNode* *bt)
{
if(*bt != NULL)
{
clearBTree(&((*bt)->left));
clearBTree(&((*bt)->right));
free(*bt);
*bt = NULL;
}
return;
}
//前序遍歷
void preOrder(BTreeNode *bt)
{
if(bt != NULL)
{
//根左右
printf("%c ", bt->data);
preOrder(bt->left);
preOrder(bt->right);
}
return;
}
//中序遍歷
void inOrder(BTreeNode *bt)
{
if(bt != NULL)
{
//左根右
inOrder(bt->left);
printf("%c ", bt->data);
inOrder(bt->right);
}
return;
}
//後序遍歷
void postOrder(BTreeNode *bt)
{
if(bt != NULL)
{
//左右根
postOrder(bt->left);
postOrder(bt->right);
printf("%c ", bt->data);
}
return;
}
//按層遍歷
void levelOrder(BTreeNode *bt)
{
BTreeNode *p;
BTreeNode *q[QUEUE_MAX_SIZE];
int front = 0, rear = 0;
//將樹根指針進隊
if(bt != NULL)
{
rear = (rear + 1) % QUEUE_MAX_SIZE;
q[rear] = bt;
}
//隊列非空
while(front != rear)
{
//使隊首指針指向隊首元素
front = (front + 1) % QUEUE_MAX_SIZE;
p = q[front];
printf("%c ", p->data);
//若結點存在左孩子,則左孩子結點指針進隊
if(p->left != NULL)
{
rear = (rear + 1) % QUEUE_MAX_SIZE;
q[rear] = p->left;
}
//若結點存在右孩子,則右孩子結點指針進隊
if(p->right != NULL)
{
rear = (rear + 1) % QUEUE_MAX_SIZE;
q[rear] = p->right;
}
}
return;
}
/************************************************************************/
/* 如下是關於二叉搜索樹操做的4個簡單算法 */
/************************************************************************/
//查找 遞歸算法
elemType *findBSTree1(BTreeNode *bst, elemType x)
{
/* 樹爲空則返回NULL */
if (bst == NULL)
{
return NULL;
}
else
{
if (x == bst->data)
{
return &(bst->data);
}
else
{
if (x < bst->data)
{ //向左子樹查找並直接返回
return findBSTree1(bst->left, x);
}
else
{ //向右子樹查找並直接返回
return findBSTree1(bst->right, x);
}
}
}
}
//查找 非遞歸算法
elemType *findBSTree2(BTreeNode *bst, elemType x)
{
while (bst != NULL)
{
if (x == bst->data)
{
return &(bst->data);
}
else if (x < bst->data)
{
bst = bst->left;
}
else
{
bst = bst->right;
}
}
return NULL;
}
//插入 遞歸算法
void insertBSTree1(BTreeNode* *bst, elemType x)
{
//新建一個根結點
if (*bst == NULL)
{
BTreeNode *p = new BTreeNode;
p->data = x;
p->left = p->right = NULL;
*bst = p;
return;
}
else if (x < (*bst)->data)
{ //向左子樹完成插入運算
insertBSTree1(&((*bst)->left), x);
}
else
{ //向右子樹完成插入運算
insertBSTree1(&((*bst)->right), x);
}
}
//插入 非遞歸算法
void insertBSTree2(BTreeNode* *bst, elemType x)
{
BTreeNode *p;
BTreeNode *t = *bst, *parent = NULL;
//爲待插入的元素查找插入位置
while (t != NULL)
{
parent = t;
if (x < t->data)
{
t = t->left;
}
else
{
t = t->right;
}
}
//創建值爲x,左右指針域爲空的新結點
p = new BTreeNode;
p->data = x;
p->left = p->right = NULL;
//將新結點連接到指針爲空的位置
if (parent == NULL)
{
//做爲根結點插入
*bst = p;
}
else if (x < parent->data)
{ //連接到左指針域
parent->left = p;
}else
{
parent->right = p;
}
return;
}
//建立二叉樹
void createBSTree(BTreeNode* *bst, elemType a[], int n)
{
int i;
*bst = NULL;
for (i = 0; i < n; i++){
insertBSTree1(bst, a[i]);
}
return;
}
//刪除值爲x的結點,成功返回1,失敗返回0
int deleteBSTree(BTreeNode* *bst, elemType x)
{
BTreeNode *temp = *bst;
if (*bst == NULL)
{
return 0;
}
if (x < (*bst)->data)
{
//向左子樹遞歸
return deleteBSTree(&((*bst)->left), x);
}
if (x > (*bst)->data)
{
//向右子樹遞歸
return deleteBSTree(&((*bst)->right), x);
}
//待刪除的元素等於樹根結點值且左子樹爲空,將右子樹做爲整個樹並返回1
if ((*bst)->left == NULL)
{
*bst = (*bst)->right;
free(temp);
return 1;
}
//待刪除的元素等於樹根結點值且右子樹爲空,將左子樹做爲整個樹並返回1
if ((*bst)->right == NULL)
{
*bst = (*bst)->left;
free(temp);
return 1;
}
else
{
//中序前驅結點爲空時,把左孩子結點值賦給樹根結點,而後從左子樹中刪除根結點
if ((*bst)->left->right == NULL)
{
(*bst)->data = (*bst)->left->data;
return deleteBSTree(&((*bst)->left), (*bst)->data);
}
else
{ //定位到中序前驅結點,把該結點值賦給樹根結點,而後從以中序前驅結點爲根的樹上刪除根結點
BTreeNode *p1 = *bst, *p2 = p1->left;
while (p2->right != NULL)
{
p1 = p2;
p2 = p2->right;
}
(*bst)->data = p2->data;
return deleteBSTree(&(p1->right), p2->data);
}
}
}
int main(int argc, char *argv[])
{
elemType x, *px;
elemType a[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
BTreeNode *bst = NULL;
createBSTree(&bst, a, 10);
printf("創建的二叉搜索樹的廣義表形式爲:\r\n");
std::cout << "\r\n前序遍歷:\r\n";
printBTree(bst);
printf("\r\n\r\n中序遍歷:\r\n");
inOrder(bst);
printf("\r\n\r\n輸入待查找元素的值:");
std::cin >> x;
if (px = findBSTree1(bst, x))
{
printf("查找成功!獲得的x爲:%d\r\n", *px);
}
else
{
printf("查找失敗\r\n");
}
printf("輸入待插入的元素值:");
std::cin >> x;
insertBSTree1(&bst, x);
printf("輸入待刪除元素值:");
std::cin >> x;
if (deleteBSTree(&bst, x)){
printf("1 ");
}else{
printf("0 ");
}
printf("\r\n\r\n進行相應操做後的中序遍歷爲:\r\n");
inOrder(bst);
printf("\r\n\r\n操做後的二叉搜索樹的廣義表的形式爲:\r\n");
printBTree(bst);
printf("\r\n");
clearBTree(&bst);
system("pause");
return 0;
}html