这里写了一些有关二叉树的一些最基本的问题和常见的函数,如二叉树的遍历,构造等。
一、二叉树节点构造
typedef char ElemType;
#define END '#'
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 PastOrder(BtNode *p)//后序遍历
{
if(p != NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout<<p->data<<" ";
}
}
三、构造二叉树的几种方法
方法一:依次输入建立二叉树
方法二:传递字符串建立二叉树,但是要注意需要传递引用,而不能是单纯的一个字符串指针
方法三:是方法二的改进,也是传递一个字符串,参数列表写成*const,就是等于& ,也就是讲,传递了一个二级指针,这个二级指针的字符串是一个静态的不允许修改的。所以在函数内部需要先解引用,然后++,然后再取地址。
方法四:无返回值的构造二叉树的方式。
代码如下:
BtNode * CreateTree1()
{
ElemType x;
cin>>x;
BtNode *s = NULL;
if(x != END)
{
s = _Buynode();
s->data = x;
s->leftchild = CreateTree1();
s->rightchild = CreateTree1();
}
return s;
}
BtNode * CreateTree2(ElemType *&str)
{
BtNode *s = NULL;
if(str != NULL && *str != END)
{
s = _Buynode();
s->data = *str;
s->leftchild = CreateTree2(++str);
s->rightchild = CreateTree2(++str);
}
return s;
}
BtNode * CreateTree3(ElemType * * const pstr)
{
BtNode *s = NULL;
if(pstr != NULL && *pstr != NULL && **pstr != END)
{
s = _Buynode();
s->data = **pstr;
s->leftchild = CreateTree3(&++*pstr);
s->rightchild = CreateTree3(&++*pstr);
}
return s;
}
// 6
void CreateTree4(BtNode *&ptr)
{
ElemType x;
cin>>x;
if(x == END) { ptr = NULL;}
else
{
ptr = _Buynode();
ptr->data = x;
CreateTree4(ptr->leftchild);
CreateTree4(ptr->rightchild);
}
}
全部代码如下:
#include<iostream>
using namespace std;
typedef char ElemType;
#define END '#'
typedef struct BtNode
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode, *BinaryTree;
BtNode * _Buynode()
{
BtNode *s = (BtNode*)malloc(sizeof(BtNode));
if(NULL == s) exit(1);
memset(s,0,sizeof(BtNode));
return s;
}
void _Freenode(BtNode *p)
{
free(p);
}
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 PastOrder(BtNode *p)
{
if(p != NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout<<p->data<<" ";
}
}
BtNode * CreateTree1()
{
ElemType x;
cin>>x;
BtNode *s = NULL;
if(x != END)
{
s = _Buynode();
s->data = x;
s->leftchild = CreateTree1();
s->rightchild = CreateTree1();
}
return s;
}
BtNode * CreateTree2(ElemType *&str)
{
BtNode *s = NULL;
if(str != NULL && *str != END)
{
s = _Buynode();
s->data = *str;
s->leftchild = CreateTree2(++str);
s->rightchild = CreateTree2(++str);
}
return s;
}
BtNode * CreateTree3(ElemType * * const pstr)
{
BtNode *s = NULL;
if(pstr != NULL && *pstr != NULL && **pstr != END)
{
s = _Buynode();
s->data = **pstr;
s->leftchild = CreateTree3(&++*pstr);
s->rightchild = CreateTree3(&++*pstr);
}
return s;
}
// 6
void CreateTree4(BtNode *&ptr)
{
ElemType x;
cin>>x;
if(x == END) { ptr = NULL;}
else
{
ptr = _Buynode();
ptr->data = x;
CreateTree4(ptr->leftchild);
CreateTree4(ptr->rightchild);
}
}
void main()
{
char *str= "ABC##DE##F##G#H##";
BinaryTree root = NULL;
CreateTree4(root);
PreOrder(root); cout<<endl;
InOrder(root); cout<<endl;
PastOrder(root); cout<<endl;
}