二叉樹
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct BTNode
{
ElemType Data;
struct BTNode *Lchild;
struct BTNode *Rchild;
}*BTree,*Position ;
void CreateBTree(BTree &T, char *str)
{
BTree St[MaxSize];
Position P = NULL;
int Top = -1, k, j = 0;
char ch = str[j];
T = NULL;
while (ch != '\0')
{
switch (ch)
{
case '(':Top++; St[Top] = P; k = 1; break;
case ')':Top--; break;
case ',':k = 2; break;
default:P = (BTNode*)malloc(sizeof(BTNode));
P->Data = ch; P->Lchild = P->Rchild = NULL;
if (T == NULL)
T = P;
else
{
switch (k)
{
case 1:St[Top]->Lchild = P; break;
case 2:St[Top]->Rchild = P; break;
}
}
}
ch = str[++j];
}
}
Position FindNode(BTree T, ElemType X)
{
Position P;
if (T == NULL)
return NULL;
else if (T->Data == X)
return T;
else
{
P = FindNode(T->Lchild, X);
if (P != NULL)
return P;
else
return FindNode(T->Rchild, X);
}
}
Position LchildNode(Position P)
{
return P->Lchild;
}
Position RchildNode(Position P)
{
return P->Rchild;
}
int BTreeHeight(BTree T)
{
int LHeight, RHeight;
if (T == NULL)
return 0;
else
{
LHeight = BTreeHeight(T->Lchild);
RHeight = BTreeHeight(T->Rchild);
return (LHeight > RHeight) ? (LHeight + 1) : (RHeight + 1);
}
}
void DispBTree(BTree T)
{
if (T != NULL)
{
printf("%c", T->Data);
if (T->Lchild != NULL || T->Rchild != NULL)
{
printf("(");
DispBTree(T->Lchild);
if (T->Rchild != NULL) printf(",");
DispBTree(T->Rchild);
printf(")");
}
}
}
int NodeNum(BTree T)
{
if (T)
return NodeNum(T->Lchild) + NodeNum(T->Rchild) + 1;
else
return 0;
}
void PreOrder(BTree T)
{
if (T == NULL)
return;
printf("%c ", T->Data);
PreOrder(LchildNode(T));
PreOrder(RchildNode(T));
}
void InOrder(BTree T)
{
if (T == NULL)
return;
InOrder(LchildNode(T));
printf("%c ", T->Data);
InOrder(RchildNode(T));
}
void PostOrder(BTree T)
{
if (T == NULL)
return;
PostOrder(LchildNode(T));
PostOrder(RchildNode(T));
printf("%c ", T->Data);
}
void PrintNodeAtLevel(BTree T, int Level)
{
if (T == NULL || Level < 1)
return;
if (Level == 1)
{
printf("%c ", T->Data);
return;
}
PrintNodeAtLevel(T->Lchild, Level - 1);
PrintNodeAtLevel(T->Rchild, Level - 1);
}
void TravLevel(BTree T)
{
if (T == NULL)
return;
int Height = BTreeHeight(T);
int i;
for (i = 1; i <= Height; i++)
{
PrintNodeAtLevel(T, i);
printf("\n");
}
}
int main()
{
BTree T;
T = (BTNode*)malloc(sizeof(BTNode));
CreateBTree(T, "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("輸出二叉樹:");
DispBTree(T);
printf("\n");
Position N = FindNode(T, 'H');
Position P = LchildNode(N);
printf("H的左孩子是:%c ,", P->Data);
P = RchildNode(N);
printf("右孩子是: %c .\n", P->Data);
printf("二叉樹b的深度: %d.\n", BTreeHeight(T));
printf("二叉樹b的結點個數: %d.\n", NodeNum(T));
printf(" 先序遍歷序列:\n");
printf(" 遞歸算法:"); PreOrder(T); printf("\n");
printf(" 中序遍歷序列:\n");
printf(" 遞歸算法:"); InOrder(T); printf("\n");
printf(" 後序遍歷序列:\n");
printf(" 遞歸算法:"); PostOrder(T); printf("\n");
printf(" 層次遍歷序列:"); printf("\n");
TravLevel(T); printf("\n");
return 0;
}
二叉搜索樹
#include <stdio.h>
#include <malloc.h>
typedef int KeyType;
typedef char InfoType[10];
typedef struct TreeNode
{
KeyType Key;
InfoType Data;
struct TreeNode *Lchild, *Rchild;
} *BSTree,*Position;
int InsertBST(BSTree &T, KeyType K)
{
if (T == NULL)
{
T = (TreeNode*)malloc(sizeof(TreeNode));
T->Key = K;
T->Lchild = T->Rchild = NULL;
return 0;
}
if (K == T->Key)
return -1;
else if (K < T->Key)
return InsertBST(T->Lchild, K);
else if(K > T->Key)
return InsertBST(T->Rchild, K);
}
void DispBST(BSTree T)
{
if (T != NULL)
{
printf("%d", T->Key);
if (T->Lchild != NULL || T->Rchild != NULL)
{
printf("(");
DispBST(T->Lchild);
if (T->Rchild != NULL)
printf(",");
DispBST(T->Rchild);
printf(")");
}
}
}
Position CreateBST(KeyType a[], int n)
{
BSTree T = NULL;
int i = 0;
while (i < n)
{
printf(" 第%d步,插入%d元素:", i + 1, a[i]);
InsertBST(T, a[i++]);
DispBST(T); printf("\n");
}
return T;
}
Position SearchBST(BSTree T, KeyType K)
{
if (T == NULL)
return T;
if (K < T->Key)
return SearchBST(T->Lchild, K);
else if (K > T->Key)
return SearchBST(T->Rchild, K);
else
return T;
}
Position SearchBST1(BSTree T, KeyType K, BSTree f1, BSTree &f)
{
if (T == NULL)
{
f = NULL;
return NULL;
}
else if (K == T->Key)
{
f = f1;
return T;
}
else if (K < T->Key)
return SearchBST1(T->Lchild, K, T, f);
else
return SearchBST1(T->Rchild, K, T, f);
}
Position FindMin(BSTree T)
{
if (T == NULL)
return NULL;
if (T->Lchild == NULL)
return T;
else
return FindMin(T->Lchild);
}
Position FindMax(BSTree T)
{
if (T != NULL)
while (T->Rchild != NULL)
T = T->Rchild;
return T;
}
Position DeleteBST(BSTree T, KeyType K)
{
Position TmpCell;
if (T == NULL)
return T;
if (K < T->Key)
T->Lchild = DeleteBST(T->Lchild, K);
else if (K > T->Key)
T->Rchild = DeleteBST(T->Rchild, K);
else
{
if (T->Lchild && T->Rchild)
{
TmpCell = FindMin(T->Rchild);
T->Key = TmpCell->Key;
T->Rchild = DeleteBST(T->Rchild, T->Key);
}
else
{
TmpCell = T;
if (T->Lchild == NULL)
T = T->Rchild;
else if (T->Rchild == NULL)
T = T->Rchild;
free(TmpCell);
}
}
return T;
}
Position Search1(BSTree T, KeyType K)
{
if (T == NULL)
return NULL;
printf("%d ",T->Key);
if (K < T->Key)
return Search1(T->Lchild, K);
else if (K > T->Key)
return Search1(T->Rchild, K);
else
return T;
}
Position Search2(BSTree T, KeyType K)
{
if (T == NULL)
return NULL;
while (T) {
printf("%d ", T->Key);
if (K < T->Key)
T = T->Lchild;
else if (K > T->Key)
T = T->Rchild;
else
return T;
}
}
int IsBST(BSTree T)
{
static int flag = 1;
static int LastKey = 0;
if (T->Lchild && flag)
IsBST(T->Lchild);
if (T->Key < LastKey)
flag = 0;
LastKey = T->Key;
if (T->Rchild && flag)
IsBST(T->Rchild);
return flag;
}
int Compare(BSTree T, BSTree P)
{
if (T == NULL && P == NULL)
return 1;
else if (T != NULL && P != NULL)
{
if (T->Key == P->Key)
return Compare(T->Lchild, P->Lchild) && Compare(T->Rchild, P->Rchild);
else
return 0;
}
else
return 0;
}
void main()
{
BSTree T;
int x = 46, i;
KeyType a[] = { 4,9,0,1,8,6,3,5,2,7 };
int n = 10;
printf(" 創建一棵BST樹:\n");
T = CreateBST(a, n);
printf(" BST:"); DispBST(T); printf("\n");
(IsBST(T) == 1) ? printf("bt是一棵BST樹 \n") : printf("bt不是一棵BST樹\n");
printf("查找關鍵字(遞歸):");
Search1(T, 6);
printf("\n");
printf("查找關鍵字(非遞歸):");
Search2(T, 6);
printf("\n");
printf("刪除操作:\n");
printf("原BFS:");
DispBST(T); printf("\n");
printf("刪除節點4:");
DeleteBST(T, 4);
DispBST(T); printf("\n");
printf("刪除節點5:");
DeleteBST(T, 5);
DispBST(T); printf("\n");
}
平衡二叉樹
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct AvlNode
{
ElementType Element;
struct AvlNode *Left;
struct AvlNode *Right;
int Height;
}*AvlTree,*Position;
int Height(Position P)
{
if (P == NULL)
return -1;
else
return P->Height;
}
int Max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
Position SingleRotateWithLeft(Position K2)
{
Position K1;
K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;
K1->Height = Max(Height(K1->Right), Height(K1->Left)) + 1;
K2->Height = Max(Height(K2->Right), Height(K2->Left)) + 1;
return K1;
}
Position SingleRotateWithRight(Position K2)
{
Position K1;
K1 = K2->Right;
K2->Right = K1->Left;
K1->Left = K2;
K1->Height = Max(Height(K1->Left), Height(K1->Right)) + 1;
K2->Height = Max(Height(K2->Left), Height(K2->Right)) + 1;
return K1;
}
Position DoubleRotateWithLeft(Position K3)
{
K3->Left = SingleRotateWithRight(K3->Left);
return SingleRotateWithLeft(K3);
}
Position DoubleRotateWithRight(Position K3)
{
K3->Right = SingleRotateWithLeft(K3->Right);
return SingleRotateWithRight(K3);
}
AvlTree Insert(AvlTree T, ElementType X)
{
if (T == NULL)
{
T = (AvlNode*)malloc(sizeof(AvlNode));
T->Element = X;
T->Height = 0;
T->Left = T->Right = NULL;
}
else
{
if (X < T->Element)
{
T->Left = Insert(T->Left, X);
if (Height(T->Left) - Height(T->Right) == 2)
{
if (X < T->Left->Element)
T = SingleRotateWithLeft(T);
else
T = DoubleRotateWithLeft(T);
}
}
else if (X > T->Element)
{
T->Right = Insert(T->Right, X);
if (Height(T->Right) - Height(T->Left) == 2)
{
if (X > T->Right->Element)
T = SingleRotateWithRight(T);
else
T = DoubleRotateWithRight(T);
}
}
}
T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
return T;
}
Position FindMin(AvlTree T)
{
if (T == NULL)
return NULL;
if (T->Left == NULL)
return T;
else
return FindMin(T->Right);
}
Position Delete(AvlTree T,ElementType X)
{
Position Temp;
if (T == NULL)
return NULL;
else if (X < T->Element)
{
T->Left = Delete(T->Left, X);
if (Height(T->Right) - Height(T->Left) == 2)
if (Height(T->Right->Left) > Height(T->Right->Right))
T = DoubleRotateWithRight(T);
else
T = SingleRotateWithRight(T);
}
else if (X > T->Element)
{
T->Right = Delete(T->Right, X);
if (Height(T->Left) - Height(T->Right) == 2)
if (Height(T->Left->Right) > Height(T->Left->Left))
T = SingleRotateWithLeft(T);
else
T = DoubleRotateWithLeft(T);
}
else
{
if (T->Left && T->Right)
{
Temp = FindMin(T->Right);
T->Element = Temp->Element;
T->Right = Delete(T->Right, T->Element);
}
else
{
Temp = T;
if (T->Left == NULL)
T = T->Right;
else if (T->Right == NULL)
T = T->Left;
free(Temp);
}
}
if (T)
T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
return T;
}
int main()
{
AvlTree T = (AvlNode*)malloc(sizeof(AvlNode));
T = NULL;
int A[] = { 0,1,2,3,4,5,6,7,8,9 };
for (int i = 0; i < 10; i++)
T = Insert(T, A[i]);
return 0;
}