一、 Binary Tree
二叉树的创建和对二叉树的基本操作。
1. 插入结点
2. 删除结点
3. 查找、查找最大和最小值
4. 前序、中序和后序遍历(无层序遍历)
5. 计算二叉树结点数、叶子结点数和结点对应的深度等
6. 合并二叉树、翻转二叉树
二、代码
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Element;
BinTree Left;
BinTree Right;
};
BinTree Insert(BinTree BST, ElementType X);
BinTree Delete(BinTree BST, ElementType X);
Position FindMin(BinTree BST);
Position FindMax(BinTree BST);
Position Find(BinTree BST, ElementType X);
Position FindUnrecur(BinTree BST, ElementType X);
void PreorderTraversal(BinTree BST); // 先序遍历
void InorderTraversal(BinTree BST); // 中序遍历
void PostorderTraversal(BinTree BST); // 后序遍历
void OutPut(BinTree BST, ElementType k); // 输出二叉树
void PreOrderJudge(BinTree BST); // 判断二叉树
int CountNodes(BinTree BST);
int LevelOfNode(BinTree BST, int W);
int CountLeave(BinTree BST);
int main()
{
BinTree BST, MinP, MaxP, Tmp;
ElementType X;
int N, i, k, level;
BST = NULL;
printf("Input the Number of TreeNode: ");
scanf("%d", &N);
for ( i = 0; i < N; i++ ) {
scanf("%d", &X);
BST = Insert(BST, X);
}
PreorderTraversal(BST);
printf(".......preorder
");
InorderTraversal(BST);
printf(".......inorder
");
PostorderTraversal(BST);
printf(".......post order
");
printf("find node large than k: ");
scanf("%d", &k);
OutPut(BST, k); // 输出大于等于k的值
level = LevelOfNode(BST, k);
printf("
Level of k: %d
", level);
// Find N Number in Tree
MinP = FindMin(BST);
MaxP = FindMax(BST);
printf("Min:%d Max:%d
", MinP->Element, MaxP->Element );
printf("Enter the number of nodes you want to search: ");
scanf("%d", &N);
for ( i = 0; i < N; i++ ) {
scanf("%d", &X);
Tmp = Find(BST, X);
if ( Tmp == NULL )
printf("%d is not found
", X);
else {
printf("%d is found
", Tmp->Element);
if ( Tmp->Element==MinP->Element )
printf("%d is the smallest key
", Tmp->Element);
if ( Tmp->Element==MaxP->Element )
printf("%d is the largest key
", Tmp->Element);
}
}
printf("Input Delete Number:");
scanf("%d", &N);
for ( i = 0; i < N; i++ ) {
scanf("%d", &X);
BST = Delete(BST, X);
printf("Num of nodes: %d
", CountNodes(BST));
}
printf("Inorder:");
PreorderTraversal(BST);
printf("
");
return 0;
}
插入、删除结点
BinTree Insert(BinTree BST, ElementType X)
{
if ( !BST ) { // 若原树为空,生成并返回一个结点的二叉搜索树
BST = (BinTree)malloc(sizeof(struct TNode));
BST->Element = X;
BST->Left = BST->Right = NULL;
}
else { // 开始找要插入元素的位置
if ( X < BST->Element )
BST->Left = Insert(BST->Left, X); // 递归插入左子树
else if ( X > BST->Element )
BST->Right = Insert(BST->Right, X); // 递归插入右子树
// 若X已经存在,则什么都不做
}
return BST;
}
BinTree Delete(BinTree BST, ElementType X)
{
Position Tmp;
if ( !BST )
printf("Empty Tree
");
else {
if ( X < BST->Element )
BST->Left = Delete(BST->Left, X); // 从左子树递归删除
else if ( X > BST->Element )
BST->Right = Delete(BST->Right, X); // 从右子树递归删除
else {
// BST就是要删除的结点
// 如果被删除结点有左右两个子结点
if ( BST->Left && BST->Right ) {
Tmp = FindMin( BST->Right ); // 从右子树中找最小的元素填充删除结点
BST->Element = Tmp->Element; // 从右子树中删除最小元素
BST->Right = Delete( BST->Right, BST->Element );
}
else { // 被删除结点有一个或无子结点
Tmp = BST;
if ( !BST->Left ) // 只有右孩子或无子结点
BST = BST->Right;
else // 只有左孩子
BST = BST->Left;
free(Tmp);
}
} // end else
}
return BST;
}
查找
Position FindMin(BinTree BST)
{
if ( !BST )
return NULL; // 空的二叉搜索树,返回NULL
else if ( !BST->Left )
return BST; // 找到最左叶结点并返回
else
return FindMin(BST->Left); // 沿左分支继续查找
}
Position FindMax(BinTree BST)
{
if ( BST )
while ( BST->Right )
BST = BST->Right;
// 沿右分支继续查找,直到最右叶结点
return BST;
}
Position Find(BinTree BST, ElementType X)
{
if ( !BST )
return NULL; // 查找失败
if ( X > BST->Element )
return Find(BST->Right, X); // 在右子树中继续查找
else if ( X < BST->Element )
return Find( BST->Left, X); // 在左子树中继续查找
else // X == BST->Element
return BST; // 查找成功,返回结点的找到结点的地址
}
Position FindUnrecur(BinTree BST, ElementType X)
{ // 非递归遍历
while ( BST != NULL ) {
if ( BST->Element > X )
BST = BST->Left; // 当前元素大于X,在左子树中继续查找
else if ( BST->Element < X )
BST = BST->Right;
else
return BST;
}
return BST;
}
二叉树遍历、结点计数、判断是否为二叉树等
void PreorderTraversal(BinTree BST)
{
if ( BST ) {
printf("%d ", BST->Element);
PreorderTraversal(BST->Left);
PreorderTraversal(BST->Right);
}
}
void InorderTraversal(BinTree BST)
{
if ( BST ) {
InorderTraversal(BST->Left);
printf("%d ", BST->Element);
InorderTraversal(BST->Right);
}
}
void PostorderTraversal(BinTree BST)
{
if ( BST ) {
PostorderTraversal(BST->Left);
PostorderTraversal(BST->Right);
printf("%d ", BST->Element);
}
}
void OutPut(BinTree BST, ElementType k)
{
if ( BST == NULL )
return;
else { // in order
if ( BST->Right != NULL )
OutPut(BST->Right, k);
if ( BST->Element >=k )
printf("%d ", BST->Element);
if ( BST->Left != NULL )
OutPut(BST->Left, k);
}
}
void PreOrderJudge(BinTree BST)
{ // Judge whether it's a BST
if ( BST == NULL ) {
printf("Empty Tree!");
return;
} else if ( BST ) {
if ( BST->Left ) {
//左儿子更大
if ( BST->Left->Element >= BST->Element )
return;
}
if ( BST->Right ) {
// 右儿子更小
if ( BST->Right->Element <= BST->Element )
return;
}
PreOrderJudge(BST->Left);
PreOrderJudge(BST->Right);
}
}
int LevelOfNode(BinTree BST, int W)
{
int n = 0; // 结点的深度
if ( BST != NULL ) {
n++;
while ( W != BST->Element ) {
if ( W < BST->Element )
BST = BST->Left;
else if ( W > BST->Element )
BST = BST->Right;
n++;
}
}
return n;
}
int CountLeave(BinTree BST)
{ // 叶子结点计数
if ( BST == NULL )
return 0;
if ( BST->Left == NULL && BST->Right == NULL )
return 1;
else
return (CountLeave(BST->Left) + CountLeave(BST->Right));
}
int CountNodes(BinTree BST)
{ // 结点计数
if ( BST == NULL )
return 0;
else if ( BST ) {
if ( BST->Left == NULL && BST->Right == NULL )
return 1;
else
return 1 + CountNodes(BST->Left) + CountNodes(BST->Right);
}
}
struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2) {
if (t1 == NULL && t2 == NULL) {
return t1;
} else if (t1 == NULL && t2 != NULL) {
return t2;
} else if (t1 != NULL && t2 == NULL) {
return t1;
} else if (t1 != NULL && t2 != NULL) {
t1->val = t1->val + t2->val;
t1->left = mergeTrees(t1->left, t2->left);
t1->right = mergeTrees(t1->right, t2->right);
return t1;
}
return 0;
}