第一种写法:
使用指针的指针初始化
#include<iostream>
#include<assert.h>
using namespace std;
typedef struct Lnode
{
char data;
struct Lnode *LeftChild;
struct Lnode *RightChild;
}BiTreeNode;
//*head表示指向申请的内存区的指针
void Initiate(BiTreeNode **head)
{
(*head)=(BiTreeNode *)malloc(sizeof(BiTreeNode));
assert((*head)!=NULL); //if the judging conditio is true,this statement is not executed
(*head)->LeftChild=NULL;//如果指针没确切地址值,这句报错
(*head)->RightChild=NULL;
}
//插入左孩子节点
BiTreeNode * InsertLeftChild(BiTreeNode *head,char a )
{
if(head==NULL) return NULL;
BiTreeNode * p,*t;
t=head->LeftChild;
p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
assert(p!=NULL);
p->data=a;
p->RightChild=NULL;
p->LeftChild=t;
head->LeftChild=p;
return head->LeftChild;
}
//插入有孩子节点
BiTreeNode *InsertRightChild(BiTreeNode *head,char a)
{
if(head==NULL) return NULL;
BiTreeNode *p,*t;
t=head->RightChild;
p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
assert(p!=NULL);
p->data=a;
p->LeftChild=NULL;
p->RightChild=t;
head->RightChild=p;
return head->RightChild;
}
void Destroy(BiTreeNode *head)
{
if(head!=NULL && head->LeftChild!=NULL)
Destroy(head->LeftChild);//左子树非空,递归销毁左子树
if(head!=NULL && head->RightChild!=NULL)
Destroy(head->RightChild);//右子树非空,递归销毁右子树
free(head);
}
//中序遍历
void LDR(BiTreeNode *head)
{
if(head!=NULL)
{
LDR(head->LeftChild);
cout<<head->data<<" ";
LDR(head->RightChild);
}
}
//前序遍历
void DLR(BiTreeNode *head)
{
if(head!=NULL)
{
cout<<head->data<<" ";
DLR(head->LeftChild);
DLR(head->RightChild);
}
}
//后序遍历
void LRD(BiTreeNode *head)
{
if(head!=NULL)
{
LRD(head->LeftChild);
LRD(head->RightChild);
cout<<head->data<<" ";
}
}
//在二叉树中查找某个值
BiTreeNode * Search(BiTreeNode *head, char x)
{
BiTreeNode *p=NULL;//初始化为NULL
if(head!=NULL)
{
if(head->data==x) //如果查找到的节点是x,那么把该节点的值给p
p=head;
else
{
p=Search(head->LeftChild,x); //否则递归查找左孩子树
if(p==NULL) //如果左孩子树没找到,递归查找右孩子树
p=Search(head->RightChild,x);
}
}
return p;
}
void main()
{
BiTreeNode *head,*p,*q;
//声明了一个指向BiTreeNode的指针head,由于指针head没有地址
//所以作为实参要用&head,表示指向指针的地址
//此时调用的函数的形参需要使用指针的指针
Initiate(&head);
head->data='g';
p=InsertLeftChild(head,'a');
p=InsertLeftChild(p,'b');
p=InsertRightChild(head,'c');
DLR(head);
cout<<endl;
LDR(head);
cout<<endl;
LRD(head);
cout<<endl;
q=Search(head,'c');//Find c
cout<<"FIND :"<<q->data<<endl;
Destroy(head);
system("pause");
}
也可以不适用指针的指针初始化,那么可以这样写
第2种写法:
#include<iostream>
#include<assert.h>
using namespace std;
typedef struct Lnode
{
char data;
struct Lnode *LeftChild;
struct Lnode *RightChild;
}BiTreeNode;
//主程序的实参是一个有内存地址的值,所以不用malloc申请动态空间
void Initiate(BiTreeNode *head)
{
head->LeftChild=NULL;
head->RightChild=NULL;
}
BiTreeNode * InsertLeftChild(BiTreeNode *head,char a )
{
if(head==NULL) return NULL;
BiTreeNode * p,*t;
t=head->LeftChild;
p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
assert(p!=NULL);
p->data=a;
p->RightChild=NULL;
p->LeftChild=t;
head->LeftChild=p;
return head->LeftChild;
}
BiTreeNode *InsertRightChild(BiTreeNode *head,char a)
{
if(head==NULL) return NULL;
BiTreeNode *p,*t;
t=head->RightChild;
p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
assert(p!=NULL);
p->data=a;
p->LeftChild=NULL;
p->RightChild=t;
head->RightChild=p;
return head->RightChild;
}
void Destroy(BiTreeNode *head)
{
if(head!=NULL && head->LeftChild!=NULL)
Destroy(head->LeftChild);
if(head!=NULL && head->RightChild!=NULL)
Destroy(head->RightChild);
free(head);
}
void LDR(BiTreeNode *head)
{
if(head!=NULL)
{
LDR(head->LeftChild);
cout<<head->data<<" ";
LDR(head->RightChild);
}
}
void DLR(BiTreeNode *head)
{
if(head!=NULL)
{
cout<<head->data<<" ";
DLR(head->LeftChild);
DLR(head->RightChild);
}
}
void LRD(BiTreeNode *head)
{
if(head!=NULL)
{
LRD(head->LeftChild);
LRD(head->RightChild);
cout<<head->data<<" ";
}
}
BiTreeNode * Search(BiTreeNode *head, char x)
{
BiTreeNode *p=NULL;
if(head!=NULL)
{
if(head->data==x)
p=head;
else
{
p=Search(head->LeftChild,x);
if(p==NULL)
p=Search(head->RightChild,x);
}
}
return p;
}
void main()
{
//定义BiTreeNode变量,head,此时已经有内存空间
BiTreeNode head,*p,*q;
Initiate(&head);//实参传入head的内存地址
head.data='g';
p=InsertLeftChild(&head,'a');
p=InsertLeftChild(p,'b');
p=InsertRightChild(&head,'c');
DLR(&head);
cout<<endl;
LDR(&head);
cout<<endl;
LRD(&head);
cout<<endl;
q=Search(&head,'c');//Find c
cout<<"FIND :"<<q->data<<endl;
//此时销毁二叉树不能使用destroy函数,因为程序直接声明一个变量head,
//没有用malloc函数申请空间,而destroy函数调用了free函数。所以调用后会报错。
system("pause");
}