1. 二叉树基本操作
#include <stdio.h>
#include <string.h>
#include<malloc.h>
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
} BinTNode;
BinTNode *CreateBinTree()
{
BinTNode *t;
char ch;
ch = getchar();
if (ch == '0')
t = NULL;
else
{
t = (BinTNode*)malloc(sizeof(BinTNode));
t->data = ch;
t->lchild = CreateBinTree();
t->rchild = CreateBinTree();
}
return t;
}
void ListBinTree(BinTNode *t)
{
if (t != NULL)
{
printf("%c", t->data);
if (t->lchild != NULL || t->rchild != NULL)
{
printf("(");
ListBinTree(t->lchild);
if (t->rchild != NULL)
printf(",");
ListBinTree(t->rchild);
printf(")");
}
}
}
void preorder(BinTNode *t)
{
if(t)
{
printf("%3c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(BinTNode *t)
{
if(t)
{
inorder(t->lchild);
printf("%3c",t->data);
inorder(t->rchild);
}
}
void postorder(BinTNode *t)
{
if(t)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%3c",t->data);
}
}
int Height(BinTNode *t)
{
if(!t) return 0;
return (Height(t->lchild) > Height(t->rchild) ? Height(t->lchild) : Height(t->rchild)) + 1;
}
int Size(BinTNode *t)
{
if(!t) return 0;
return 1 + Size(t->lchild) + Size(t->rchild);
}
int Leaf(BinTNode *t)
{
if(!t) return 0;
if(t->lchild == NULL && NULL == t->rchild) return 1;
return Leaf(t->lchild) + Leaf(t->rchild);
}
int Max(BinTNode *t)
{
if (t == NULL) return 0;
if (t->lchild == NULL && t->rchild == NULL)
return t->data;
int l = Max(t->lchild);
int r = Max(t->rchild);
return l > r ? l : r;
}
int main()
{
BinTNode *t = NULL;
printf("请输入先序序列,虚结点用0表示:\n");
t = CreateBinTree();
printf("广义表表示的二叉树的输出:\n");
ListBinTree(t);
printf("\n二叉树的前序遍历结果为:\n");
preorder(t);
printf("\n二叉树的中序遍历结果为:\n");
inorder(t);
printf("\n二叉树的后序遍历结果为:\n");
postorder(t);
printf("\n");
printf("二叉树的高度为:%d\n", Height(t));
printf("二叉树的总结点数为:%d\n", Size(t));
printf("二叉树的叶子结点数为:%d\n", Leaf(t));
printf("二叉树的最大值为:%c\n", Max(t));
return 0;
}
2. 二叉树的遍历与应用
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OVERFLOW 0
#define OK 1
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
} BinTNode, *BTNode;
BinTNode *CreateBinTree()
{
BinTNode *t;
char ch;
ch = getchar();
if (ch == '0')
t = NULL;
else
{
t = (BinTNode *) malloc(sizeof(BinTNode));
if (!t) exit(OVERFLOW);
t->data = ch;
t->lchild = CreateBinTree();
t->rchild = CreateBinTree();
}
return t;
}
void preorder(BTNode t)
{
if (t != NULL)
{
printf("%3c", t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(BTNode t)
{
if (t != NULL)
{
inorder(t->lchild);
printf("%3c", t->data);
inorder(t->rchild);
}
}
int Leaf(BTNode t)
{
if(!t) return 0;
if(t->lchild == NULL && NULL == t->rchild) return 1;
return Leaf(t->lchild) + Leaf(t->rchild);
}
int Max(BTNode t)
{
if(!t) return 0;
if(t->lchild == NULL && NULL == t->rchild) return t->data;
return Max(t->lchild) > Max(t->rchild) ? Max(t->lchild) : Max(t->rchild);
}
int main()
{
BTNode t = NULL;
t = CreateBinTree();
preorder(t);
printf("\n");
inorder(t);
printf("\n");
printf("%d\n", Leaf(t));
printf("%c\n", Max(t));
}
3.打印二叉树中的所有从根结点到叶子结点的路径
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
typedef int TElemType;
typedef int Status;
typedef struct Tnode{
TElemType data;
struct Tnode *leftchild;
struct Tnode *rightchild;
}TNode, *BiTree;
Status CreateBiTree1(BiTree *T, char a[], int n)
{
static int count = 0;
if(count >= n) return ERROR;
if(a[count] == '#')
{
*T = NULL;
count++;
}
else
{
*T = (BiTree)malloc(sizeof(TNode));
(*T)->data = a[count++];
CreateBiTree1(&((*T)->leftchild), a, n);
CreateBiTree1(&((*T)->rightchild), a, n);
}
return OK;
}
void PrintBiTree(BiTree T)
{
if(T)
{
printf("%c", T->data);
PrintBiTree(T->leftchild);
PrintBiTree(T->rightchild);
}
}
void printPath(char path[], int pathLen)
{
for(int i = 0; i < pathLen; ++i)
printf("%c", path[i]);
printf("\n");
}
void printAllPaths(BiTree T, char path[], int pathLen)
{
if(T == NULL) return;
path[pathLen++] = T->data;
if(T->leftchild == NULL && T->rightchild == NULL)
{
printPath(path, pathLen);
}
else
{
printAllPaths(T->leftchild, path, pathLen);
printAllPaths(T->rightchild, path, pathLen);
}
}
int main()
{
char arr[1000], ch;
int len = 0;
while((ch = getchar()) != EOF)
{
if(ch == '\n')
break;
arr[len++] = ch;
}
BiTree T = NULL;
CreateBiTree1(&T, arr, len);
char path[1000];
int pathLen = 0;
printAllPaths(T, path, pathLen);
return 0;
}
4NR010:求任意二叉树最长路径
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct TNode
{
char data;
struct TNode *lchild;
struct TNode *rchild;
}TNode, *BiTree;
void CreateBiTree(BiTree *T)
{
char ch;
ch = getchar();
if(ch == '0') *T = NULL;
else
{
*T = (BiTree)malloc(sizeof(TNode));
(*T)->data = ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
int Max(int a, int b)
{
return a > b ? a : b;
}
int Height_BiTree(BiTree T)
{
if(!T) return 0;
return Max(Height_BiTree(T->lchild), Height_BiTree(T->rchild))+ 1;
}
void PrintBiTree(BiTree T)
{
if(T)
{
PrintBiTree(T->lchild);
printf("%c", T->data);
PrintBiTree(T->rchild);
}
}
void PrintPath(char *Path, int Pathlen)
{
int i = 0;
for(i = 0;i < Pathlen;i ++)
{
printf("%c->",Path[i]);
}
printf("NULL");
}
void printLongestPath(BiTree T,char* path, int pathlen, int count)
{
if(!T) return ;
path[count ++] = T->data;
if(T->lchild == NULL && NULL == T->rchild && count == pathlen)
{
PrintPath(path,pathlen);
exit(1);
}
else
{
printLongestPath(T->lchild, path, pathlen, count);
printLongestPath(T->rchild, path, pathlen, count);
}
}
void Destroy_BiTree(BiTree *T)
{
if(*T)
{
Destroy_BiTree(&(*T)->lchild);
Destroy_BiTree(&(*T)->rchild);
free(*T);
*T = NULL;
}
}
int main()
{
BiTree T;
CreateBiTree(&T);
int pathlen = Height_BiTree(T);
char path[1000];
printLongestPath(T, path, pathlen, 0);
Destroy_BiTree(&T);
return 0;
}