树
二叉树
逻辑结构与内存结构
结构实现
二叉树
- 建立二叉树
- 先序遍历(递归与非递归实现)
- 中序遍历(递归与非递归实现)
- 后序遍历(递归与非递归实现)
- 层序遍历
- 求树宽(递归与非递归实现)
- 求树深(递归与非递归实现)
- 结点最远距离
- 先序数组和中序数组建树
- 中序数组和后序数组建树
#include "队列栈.h"
#include<iostream>
using namespace std;
typedef char Elemtypes;
typedef struct node
{
Elemtypes data;
struct node* Lchild, * Rchild;
}BiNode, * BiTree;
void CreatTree(BiTree& root);
void Visit(BiTree root);
void PreOrder(BiTree root);
void InOrder(BiTree root);
void PostOrder(BiTree root);
void stack_PreOrder(BiTree root);
void stack_InOrder(BiTree root);
void stack_PostOrder(BiTree root);
void FloorOrder(BiTree root);
void Pre_In_Tree(char* Pre, char* In, BiTree& root, int len);
void Post_In_Tree(char* Post, char* In, BiTree& root, int len);
void CreatTree(BiTree& root)
{
char data;
cin >> data;
if (data == '#')
{
return;
}
root = new BiNode;
root->data = data;
root->Lchild = root->Rchild = NULL;
CreatTree(root->Lchild);
CreatTree(root->Rchild);
}
void Visit(BiTree root)
{
cout << root->data << " ";
}
void PreOrder(BiTree root)
{
if (root == NULL)
return;
Visit(root);
PreOrder(root->Lchild);
PreOrder(root->Rchild);
}
void InOrder(BiTree root)
{
if (root == NULL)
return;
InOrder(root->Lchild);
Visit(root);
InOrder(root->Rchild);
}
void PostOrder(BiTree root)
{
if (root == NULL)
return;
PostOrder(root->Lchild);
PostOrder(root->Rchild);
Visit(root);
}
void stack_PreOrder(BiTree root)
{
BiTree p = root;
Stack s;
InitStack(s);
Push(s, p);
while (s.top != s.bot)
{
Pop(s, p);
Visit(p);
if (p->Rchild != NULL)
Push(s, p->Rchild);
if (p->Lchild != NULL)
Push(s, p->Lchild);
}
}
void stack_InOrder(BiTree root)
{
BiTree p = root;
Stack s;
InitStack(s);
while (p != NULL || s.top != s.bot)
{
if (p!= NULL)
{
Push(s, p);
p = p->Lchild;
}
else
{
Pop(s, p);
cout << p->data << " ";
p = p->Rchild;
}
}
}
void stack_PostOrder(BiTree root)
{
BiTree p = root, r = NULL;
Stack s;
InitStack(s);
while (p != NULL || s.bot != s.top)
{
if (p != NULL)
{
Push(s, p);
p = p->Lchild;
}
else
{
p = *(s.top - 1);
if (p->Rchild != NULL && p->Rchild != r)
{
p = p->Rchild;
}
else
{
Pop(s, p);
Visit(p);
r = p;
p = NULL;
}
}
}
}
void FloorOrder(BiTree root)
{
BiTree p = root;
Queue q;
InitQueue(q);
EnQueue(q, p);
int h = 0, w = 0;
while (q.front < q.rear)
{
int i = q.rear - q.front;
w = w < i ? i : w;
for (int j = 0; j < i; j++)
{
DeQueue(q, p);
Visit(p);
if (p->Lchild != NULL)
EnQueue(q, p->Lchild);
if (p->Rchild != NULL)
EnQueue(q, p->Rchild);
}
h++;
}
cout << endl << h << " " << w << endl;
}
void Pre_In_Tree(char* Pre, char* In, BiTree& root, int len)
{
if (len == 0)
return;
root = new BiNode;
root->Rchild = root->Lchild = NULL;
root->data = *Pre;
int i;
for (i = 0; i < len; i++)
{
if (*Pre == In[i])
break;
}
Pre_In_Tree(Pre + 1, In, root->Lchild, i);
Pre_In_Tree(Pre + i + 1, In + i + 1, root->Rchild, len - i - 1);
}
void Post_In_Tree(char* Post, char* In, BiTree& root, int len)
{
if (len == 0)
return;
root = new BiNode;
root->Rchild = root->Lchild = NULL;
root->data = *(Post + len - 1);
int i = 0;
for (i; i < len; i++)
{
if (Post[len - 1] == In[i])
break;
}
Post_In_Tree(Post, In, root->Lchild, i);
Post_In_Tree(Post + i, In + i + 1, root->Rchild, len - i - 1);
}