二叉树的存储结构(二叉链表)、建树、递归遍历、树的深度、树的叶子节点个数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string>
/* 树的先序遍历,空节点值为0
3
2
2
0
0
3
0
0
8
6
5
4
0
0
0
0
0
先序遍历为:3 2 2 3 8 6 5 4
中序遍历为:2 2 3 3 4 5 6 8
后序遍历为:2 3 2 4 5 6 8 3
*/
typedef struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
}BiNode, *BiTree;
//二叉树的创建
int CreateTree(BiNode*& root)
{
int val;
scanf("%d", &val);
if (val <= 0)
{
root = NULL;
return 0;
}
root = (BiNode *)malloc(sizeof(BiNode));
if (!root)
{
printf("分配失败!");
}
root->val = val;
CreateTree(root->left);
CreateTree(root->right);
return 0;
}
//先序遍历
void PreOrderTree(BiNode* root)
{
if (root == NULL)
return;
printf("%d ", root->val);
PreOrderTree(root->left);
PreOrderTree(root->right);
}
//中序遍历
void InOrderTree(BiNode* root)
{
if (root == NULL)
return;
InOrderTree(root->left);
printf("%d ", root->val);
InOrderTree(root->right);
}
//后序遍历
void PostOrderTree(BiNode* root)
{
if (root == NULL)
return;
PostOrderTree(root->left);
PostOrderTree(root->right);
printf("%d ", root->val);
}
//二叉树最大深度
int maxDepth(BiNode* root)
{
if (root == NULL)
{
return 0;
}
else
{
int maxLeft = maxDepth(root->left), maxRight = maxDepth(root->right);
if (maxLeft > maxRight)
return maxLeft + 1;
else
return maxRight + 1;
}
}
//叶子节点个数
int LeafNodeNum(BiNode* root)
{
if (root == NULL)
{
return 0;
}
if (root->left == NULL && root->right == NULL)
{
return 1;
}
else
{
return LeafNodeNum(root->left) + LeafNodeNum(root->right);
}
}
int main(void)
{
BiNode* root;// = (BiTree)malloc(sizeof(BiTree));
CreateTree(root);
printf("先序遍历为:");
PreOrderTree(root); printf("\n");
printf("中序遍历为:");
InOrderTree(root); printf("\n");
printf("后序遍历为:");
PostOrderTree(root); printf("\n");
printf("树的最大深度为:%d\n", maxDepth(root));
printf("叶子节点个数为:%d\n", LeafNodeNum(root));
system("pause");
return 0;
}
二叉树的遍历(非递归)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string>
#include <stack>
#include <queue>
using namespace std;
/*
3
2
2
0
0
3
0
0
8
6
5
4
0
0
0
0
0
先序遍历为:3 2 2 3 8 6 5 4
中序遍历为:2 2 3 3 4 5 6 8
后序遍历为:2 3 2 4 5 6 8 3
层序遍历为:3 2 8 2 3 6 5 4
*/
typedef struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
}BiNode, *BiTree;
//二叉树的创建 空节点值为0
int CreateTree(BiNode*& root)
{
int val;
scanf("%d", &val);
if (val <= 0)
{
root = NULL;
return 0;
}
root = (BiNode *)malloc(sizeof(BiNode));
if (!root)
{
printf("分配失败!");
}
root->val = val;
CreateTree(root->left);
CreateTree(root->right);
return 0;
}
//先序遍历 非递归
void PreOrderTree(BiNode* root)
{
stack<BiNode *> stack;
BiNode* p = root;
while (p || !stack.empty())
{
if (p != NULL)
{
stack.push(p);
printf("%d ", p->val);
p = p->left;
}
else
{
p = stack.top();
stack.pop();
p = p->right;
}
}
}
//中序遍历 非递归
void InOrderTree(BiNode* root)
{
stack<BiNode*> stack;
BiNode* p = root;
while (p || !stack.empty())
{
if (p != NULL)
{
stack.push(p);
p = p->left;
}
else
{
p = stack.top();
printf("%d ", p->val);
stack.pop();
p = p->right;
}
}
}
//后序遍历 非递归
typedef struct
{
BiNode* biTree;
char tag;
}BiNodePost, *BiTreePost;
void PostOrderTree(BiNode* root)
{
stack<BiNodePost*> stack;
BiNode* p = root;
BiTreePost BT;
while (p || !stack.empty())
{
//遍历左子树
while (p != NULL)
{
BT = (BiNodePost *)malloc(sizeof(BiNodePost));
BT->biTree = p;
BT->tag = 'L';
stack.push(BT);
p = p->left;
}
while (!stack.empty() && (stack.top())->tag == 'R')
{
BT = stack.top();
stack.pop();
printf("%d ", BT->biTree->val);
}
//遍历右子树
if (!stack.empty())
{
BT = stack.top();
BT->tag = 'R';
p = (BT->biTree)->right;
}
}
}
//层序遍历
void LevelOrderTree(BiNode* root)
{
queue<BiNode*> queue;
BiNode* p = root;
if (p != NULL)
queue.push(p);
else
return;
while (!queue.empty())
{
p = queue.front();
printf("%d ", p->val);
queue.pop();
if (p->left != NULL)
queue.push(p->left);
if (p->right != NULL)
queue.push(p->right);
}
}
int main(void)
{
BiNode* root;// = (BiTree)malloc(sizeof(BiTree));
CreateTree(root);
printf("先序遍历为:");
PreOrderTree(root); printf("\n");
printf("中序遍历为:");
InOrderTree(root); printf("\n");
printf("后序遍历为:");
PostOrderTree(root); printf("\n");
printf("层序遍历为:");
LevelOrderTree(root); printf("\n");
system("pause");
return 0;
}
线索二叉树:
typedef struct TBiNode
{
char data;
int ltag,rtag; //线索标记
struct TBiNode *left;
struct TBiNode *right;
}TBiNode;
//中序遍历对二叉树线索化
void InThread(TBiNode* p,TBiNode*& pre)
{
if(p!=NULL)
{
InThread(p->left,pre); //递归左子树线索化
if(p->left==NULL)
{ //建立当前节点的前驱线索
p->left=pre;
p->ltag=1;
}
if(pre!=NULL && pre->right==NULL)
{ //建立前驱结点的后继线索
pre->right=p;
pre->rtag=1;
}
pre=p;
p=p->right;
InThread(p,pre); //递归右子树线索化
}
}
//遍历中序线索二叉树
TBiNode *First(TBiNode *p)
{
while(p->ltag==0)
p=p->left;
return p;
}
TBiNode *Next(TBiNode *p)
{
if(p->rtag==0)
return First(p->right);
else
return p->right;
}
void Inorder(TBiNode *root)
{
for(TBiNode *p=First(root);p!=NULL;p=Next(p))
Visit(p);
}
//前序遍历对二叉树线索化
void PreThread(TBiNode *p,TBiNode *&pre)
{
if(p!=NULL)
{
if(p->left==NULL)
{
p->left=pre;
p->ltag=1;
}
if(pre!=NULL && pre->right==NULL)
{
pre->right=p;
pre->rtag=1;
}
pre=p;
if(p->ltag==0)
PreThread(p->left,pre);
if(p->rtag==0)
PreThread(p->right,pre);
}
}
//遍历前序线索二叉树
void Preorder(TBiNode *root)
{
if(root!=NULL)
{
TBiNode *p=root;
while(p!=NULL)
{
while(p->ltag==0)
{
Visit(p);
p=p->left;
}
Visit(p);
p=p->right;
}
}
}
//后序遍历线索化二叉树
void PostThread(TBiNode *p,TBiNode *&pre)
{
if(p!=NULL)
{
PostThread(p->left,pre);
PostThread(p->right,pre);
if(p->left==NULL)
{
p->left=pre;
p->rtag=1;
}
if(pre!=NULL && pre->right==NULL)
{
pre->right=p;
pre->rtag=1;
}
pre=p;
}
}
建哈夫曼树、求哈夫曼编码:
//哈夫曼树的结点类型
typedef struct HTreeNode
{
char data[5]; //每个结点是字符类型,最多5个字符
string code; //编码
int weight; //字符所占的权重
int parent; //双亲结点所在下标
int left; //左孩子结点所在下标
int right; //右孩子结点所在下标
}HTNode;
//哈夫曼树的构造,n个结点,最后生成的树有2n-1个结点
void CreateHTree(HTNode ht[],int n)
{
int lchild,rchild;
int min1,min2;
for(int i = 0;i < 2*n-1;i++)
{
ht[i].parent = ht[i].left = ht[i].right = -1;
}
for(int j = n;j < 2*n-1;j++) //前n个结点是已知的叶子结点,构造n之后的结点
{
min1 = min2 = 32767;
lchild = rchild = -1;
for(int k = 0;k < j;k++)
{
if(ht[k].parent == -1) //只在尚未构造二叉树的结点中查找
{
if(ht[k].weight<min1)
{
min2 = min1;rchild = lchild;
min1 = ht[k].weight;
lchild = k;
}
else if(ht[k].weight<min2)
{
min2 = ht[k].weight;
rchild = k;
}
}
}
ht[lchild].parent = j;
ht[rchild].parent = j;
ht[j].weight = ht[lchild].weight + ht[rchild].weight;
ht[j].left = lchild;
ht[j].right = rchild;
}
}
//编码过程
void Huff_Code(HTNode huffTree[], int n)
{
int i, j, k;
string s = "";
for (i = 0; i < n; i++)
{
s = "";
j = i;
while (huffTree[j].parent != -1) //从叶子往上找到根节点
{
k = huffTree[j].parent;
if (j == huffTree[k].lchild) //如果是根的左孩子,则记为0
{
s = s + "0";
}
else
{
s = s + "1";
}
j = huffTree[j].parent;
}
cout << "字符 " << huffTree[i].ch << " 的编码:";
for (int l = s.size() - 1; l >= 0; l--)
{
cout << s[l];
huffTree[i].code += s[l]; //保存编码
}
cout << endl;
}
}
//解码过程
string Huff_Decode(HTNode huffTree[], int n,string s)
{
cout << "解码后为:";
string temp = "",str="";//保存解码后的字符串
for (int i = 0; i < s.size(); i++)
{
temp = temp + s[i];
for (int j = 0; j < n; j++)
{
if (temp == huffTree[j].code)
{
str=str+ huffTree[j].ch;
temp = "";
break;
}
else if (i == s.size()-1&&j==n-1&&temp!="")//全部遍历后没有
{
str= "解码错误!";
}
}
}
return str;
}