#include <iostream>
#include<queue>
using namespace std;
typedef char ElemType;
typedef struct BtNode //定义二叉树节点
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode, *BinaryTree;
void PreOrder(BtNode *p) //先序遍历
{
if(p != NULL)
{
cout<<p->data<<" ";
PreOrder(p->leftchild);
PreOrder(p->rightchild);
}
}
void InOrder(BtNode *p) //中序遍历
{
if(p != NULL)
{
InOrder(p->leftchild);
cout<<p->data<<" ";
InOrder(p->rightchild);
}
}
void LastOrder(BtNode *p) //后序遍历
{
if(p != NULL)
{
LastOrder(p->leftchild);
LastOrder(p->rightchild);
cout<<p->data<<" ";
}
}
BtNode *BuyNode() //购买节点
{
BtNode *s = (BtNode*)malloc(sizeof(BtNode));
if(s == NULL) exit(1);
memset(s,0,sizeof(BtNode));
return s;
}
void FreeNode(BtNode *p) //释放结点
{
free(p);
}
BtNode *CreateTree(ElemType *&str)
{
BtNode *s = NULL;
if(*str != '#')
{
s = BuyNode();
s->data = *str;
s->leftchild = CreateTree(++str);
s->rightchild = CreateTree(++str);
}
return s;
}
int GetSize(BtNode *p) //求节点个数:用递归实现
{
if(p == NULL)
return 0;
else
return GetSize(p->leftchild) + GetSize(p->rightchild) + 1; //1表示根节点个数
}
int Depth(BtNode *p) //求树的深度
{
if(p == NULL)
return 0;
else
return max(Depth(p->leftchild), Depth(p->rightchild)) + 1;
}
/*
//待完成的函数(关于二叉树)
BtNode * FindValue(BtNode *p, ElemType x); //查找value值
BtNode *FindParent(BtNode *p, BtNode *child); //查找父结点
//先序中序后序遍历二叉树(不使用递归)
void NicePreOrder();
void NiceInOrder();
void NiceLastOrder();
void PrintLevel_K(BtNode *p, int k); //查找K所在的层数
bool Is_Full_BinaryTree(); //是否为满二叉树
bool Is_Comp_BinaryTree();
bool Is_BST; //是否为BST树(二叉搜索树——左孩子比根节点小,右孩子比根节点大)
bool Is_Balance_BinaryTree(); //是否为平衡二叉树
*/
void NiceLevelOrder(BtNode *p) //使用队列的方法输出树的各层结点
{
if(p == NULL) return;
queue<BtNode *> qu;
qu.push(p);
while(!qu.empty())
{
BtNode *p = qu.front();
qu.pop();
if(p->leftchild != NULL)
{
qu.push(p->leftchild);
}
if(p->rightchild != NULL)
{
qu.push(p->rightchild);
}
}
cout<<endl;
}
/*
*先序中序与中序后序遍历二叉树的测试
int main()
{
char *ps = "ABCDEFGH";
char *is = "CBEDFAGH";
char *ls = "CEFDBHGA";
int n = strlen(ps);
BinaryTree root = NULL;
//root = CreateTreePI(ps,is,n);
root = CreateTreeIL(is,ls,n);
PreOrder(root);
cout<<endl;
InOrder(root);
cout<<endl;
LastOrder(root);
cout<<endl;
return 0;
}
*/
//创建二叉树的七种方法
//法一:先序中序创建二叉树
int FindPos(ElemType *is, ElemType x, int n) //在中序序列中寻找根节点
{
int pos = -1;
for(int i = 0; i<n; i++)
{
if(x == is[i])
{
pos = i;
break;
}
}
return pos;
}
BtNode *CreatePI(ElemType *ps, ElemType *is, int n) //先序中序创建二叉树
{
BtNode *s = NULL;
if(n > 0)
{
s= BuyNode();
s->data = ps[0];
int pos = FindPos(is, ps[0], n);
if(pos == -1) exit(1);
s->leftchild = CreatePI(ps+1, is, pos);
s->rightchild = CreatePI(ps+1+pos, is+1+pos, n-pos-1);
}
return s;
}
BtNode * CreateTreePI(ElemType *ps, ElemType *is, int n)
{
if(ps==NULL || is==NULL || n<1)
return NULL;
else
return CreatePI(ps, is, n);
}
//法二:中序后序创建二叉树
BtNode *CreateIL(ElemType *is, ElemType *ls, int n)
{
BtNode *s = NULL;
if(n > 0)
{
s = BuyNode();
s->data = ls[n-1];
int pos = FindPos(is, ls[n-1], n);
if(pos == -1) exit(1);
s->leftchild = CreateIL(is, ls, pos);
s->rightchild = CreateIL(is+pos+1, ls+pos, n-pos-1);
}
return s;
}
BtNode *CreateTreeIL(ElemType *is, ElemType *ls, int n)
{
if(is==NULL || ls==NULL || n<1)
return NULL;
else
return CreateIL(is, ls, n);
}
//法三:加&成功创建二叉树
void CreateTree4(BtNode *&p, ElemType *&str)
{
if(str == NULL || *str == '#')
{
p = NULL;
}
else
{
p = BuyNode();
p->data = *str;
CreateTree4(p->leftchild, ++str);
CreateTree4(p->rightchild, ++str);
}
}
//法四:中序遍历数组创建二叉树
void ArrayInOrder(int *arr, int i, int n)
{
if(i < n)
{
ArrayInOrder(arr, i*2+1, n); //p->leftchild
cout<<arr[i]<<" ";
ArrayInOrder(arr, i*2+2, n); //p->rightchild
}
}
//法五:数组创建二叉树
BtNode *CreateArray(int *arr, int i, int n)
{
BtNode *s = NULL;
if(i < n)
{
s = BuyNode();
s->leftchild = CreateArray(arr, i*2+1, n);
s->rightchild = CreateArray(arr, i*2+2, n);
s->data = arr[i];
}
return s;
}
//法六:链表创建二叉树
void ListArray(BtNode *p, int *br, int i)
{
if(p != NULL)
{
br[i] = p->data;
ListArray(p->leftchild, br, i*2+1);
ListArray(p->rightchild, br, i*2+2);
}
}
//法七:用二分法创建平衡二叉树
BtNode * BinaryCreate(int *arr, int left, int right)
{
BtNode *s = NULL;
if(left <= right)
{
int mid = (right-left+1) / 2 + left;
s = BuyNode();
s->data = arr[mid];
s->leftchild = BinaryCreate(arr, left, mid-1);
s->rightchild = BinaryCreate(arr, mid+1, right);
}
return s;
}
int main()
{
BinaryTree root = NULL;
int arr[] = {31,23,12,66,94,5,17,70,62,49,55,88};
const int n = sizeof(arr) / sizeof(arr[0]);
int brr[n];
ArrayInOrder(arr,0,n); //法四
cout<<endl;
root = CreateArray(arr,0,n); //法五
InOrder(root);
cout<<endl;
ListArray(root,brr,0); //法六
char *str="ABC##DE##F##G#H##";
BinaryTree root1 = NULL;
CreateTree4(root1,str);
return 0;
}