1)
#ifndef BiTree_H
#define BiTree_H
struct BiNode
{
chardata;
BiNode* lchild, * rchild;
};
class BiTree
{
public:
BiTree(){root=Creat(root);}
~BiTree(){Release(root);}
voidPreOrder(){PreOrder(root);}
voidInOrder(){InOrder(root);}
voidPostOrder(){PostOrder(root);}
private:
BiNode* root;
BiNode* Creat(BiNode * bt);
voidRelease(BiNode * bt);
voidPreOrder(BiNode * bt);
voidInOrder(BiNode * bt);
voidPostOrder(BiNode * bt);
};
#endif
#include<iostream>
using namespace std;
#include "Bitree.h"
BiNode * BiTree::Creat(BiNode * bt)
{
charch;
cout<<"请创建一棵二叉树的结点数据"<<endl;
cin>>ch;
if(ch=='#')returnNULL;
else{
bt=newBiNode;
bt->data=ch;
bt->lchild=Creat(bt->lchild);
bt->rchild=Creat(bt->rchild);
}
returnbt;
}
void BiTree::Release(BiNode * bt)
{
if(bt!=NULL){
Release(bt->lchild);
Release(bt->rchild);
deletebt;
}
}
void BiTree::PreOrder(BiNode * bt)
{
if(bt==NULL)return;
else{
cout<<bt->data<<"";
PreOrder(bt->lchild);
PreOrder(bt->rchild);
}
}
void BiTree::InOrder(BiNode * bt)
{
if(bt==NULL)return;
else{
InOrder(bt->lchild);
cout<<bt->data<<"";
InOrder(bt->rchild);
}
}
void BiTree::PostOrder(BiNode * bt)
{
if(bt==NULL)return;
else{
PostOrder(bt->lchild);
PostOrder(bt->rchild);
cout<<bt->data<<"";
}
}
#include<iostream>
using namespace std;
#include "Bitree.h"
int main()
{
BiTreeT;
cout<<"------前序遍历------"<<endl;
T.PreOrder( );
cout<<endl;
cout<<"------中序遍历------"<<endl;
T.InOrder( );
cout<<endl;
cout<<"------后序遍历------"<<endl;
T.PostOrder( );
cout<<endl;
return0;
}
2)
#ifndef Tree_H
#define Tree_H
const int Max=20;
struct TNode
{
chardata;
TNode* firstchild, * rightsib;
};
class Tree
{
public:
Tree();
~Tree(){Release(root);}
voidPreOrder(){PreOrder(root);}
voidPostOrder(){PostOrder(root);}
private:
TNode* root;
voidRelease(TNode * bt);
voidPreOrder(TNode * bt);
voidPostOrder(TNode * bt);
};
#endif
#include<iostream>
using namespace std;
#include "Tree.h"
Tree::Tree()
{
TNode* Q[Max]={NULL};
intfront=-1,rear=-1;
charch1='#',ch2='#';
TNode* p=NULL, * q=NULL;
cout<<"请输入根结点:";
cin>>ch1;
p=newTNode;p->data=ch1;
p->firstchild=p->rightsib=NULL;
root=p;
Q[++rear]=p;
cout<<"请输入结点对,以空格分隔:";
fflush(stdin);
ch1=getchar();getchar();ch2=getchar();
while(ch1!='#'||ch2!='#')
{
p=newTNode;p->data=ch2;
p->firstchild=p->rightsib=NULL;
Q[++rear]=p;
while(front!=rear)
{
q=Q[front+1];
if(q->data!=ch1)
front++;
else
{
if(q->firstchild==NULL)
q->firstchild=p;
else
{
while(q->rightsib!=NULL)
q=q->rightsib;
q->rightsib=p;
}
break;
}
}
cout<<"请输入结点对,以空格分隔:";
fflush(stdin);
ch1=getchar();getchar();ch2=getchar();
}
}
void Tree::Release(TNode * bt)
{
if(bt==NULL)return;
else
{
Release(bt->firstchild);
Release(bt->rightsib);
delete bt;
}
}
void Tree::PreOrder(TNode * bt)
{
if(bt==NULL)return;
else
{
cout<<bt->data;
PreOrder(bt->firstchild);
PreOrder(bt->rightsib);
}
}
void Tree::PostOrder(TNode * bt)
{
if(bt==NULL)return;
else
{
PostOrder(bt->firstchild);
PostOrder(bt->rightsib);
cout<<bt->data;
}
}
#include<iostream>
using namespace std;
#include "Tree.h"
int main()
{
Treet1;
t1.PreOrder();
cout<<endl;
t1.PostOrder();
cout<<endl;
return0;
}
3)
#include<iostream>
#include <string>
using namespace std;
typedef struct node
{
char data;
struct node *lchild,*rchild;
}binary_tree,*tree;
void creat_tree(tree &t)
{
char ch;
cin>>ch;
if(ch == '0')
{
t=NULL;
}
else
{
t = new binary_tree;
if(!t) exit(0);
t->data=ch;
creat_tree(t->lchild);
creat_tree(t->rchild);
}
}
bool printPath(tree &t, char data)
{
if (t == NULL)
return false;
if (t->data == data || printPath(t->lchild,data) ||printPath(t->rchild,data))
{
cout<<t->data;
return true;
}
return false;
}
void printLeavesDepth(tree &t, size_tdepth = 0)
{
if(t == NULL) return;
if(t->lchild == NULL && t->rchild == NULL)
{
cout<<t->data<<":"<<depth<<endl;
}
else
{
printLeavesDepth(t->lchild, depth+1);
printLeavesDepth(t->rchild, depth+1);
}
}
void DispLeaf(tree &t)
{
if(t)
{
if(t->lchild==NULL&&t->rchild==NULL)
{
cout<<t->data<<" ";
}
DispLeaf(t->lchild);
DispLeaf(t->rchild);
}
}
int main()
{
tree t;
cout<<"请输入二叉树节点:(例如输入:ab00c00)";
creat_tree(t);
cout<<"输出叶子结点"<<endl;
DispLeaf(t);
cout<<endl;
cout<<"各个叶子结点到根结点的路径长度"<<endl;
printLeavesDepth(t,0);
system("pause");
return 0;
}