前言
最近学到了二叉树,就学着将二叉树构造,并尝试三种遍历操作。本次主要使用递归,回头会整理非递归的方法。
定义二叉树
typedef int TelemType;
typedef struct BinaryTree
{
TelemType data;
struct BinaryTree *lchild;
struct BinaryTree *rchild;
}*Node,node;
其中要注意Node是结构体指针,这样定义以后使用会方便很多。
构造二叉树
Node CreatTree()
{
Node p;
TelemType a;
cin >> a;
if(a == 0)
p = NULL;
else
{
p = (Node)malloc(sizeof(node));
p->data = a;
p->lchild = CreatTree();
p->rchild = CreatTree();
}
return p;
}
注意:创建二叉树的顺序为先根节点,然后是左子树,最后是右子树,另外叶子结点要分别赋0。
如: 1
2 3 需要输入1 2 0 0 3 0 0
二叉树节点数
int NodeNumber(Node root)
{
if(root == NULL)
return 0;
else
return 1+NodeNumber(root->lchild) + NodeNumber(root->rchild);
}
二叉树深度
int DepthTree(Node root)
{
if(root)
return DepthTree(root->lchild) > DepthTree(root->rchild) ? DepthTree(root->lchild) + 1 : DepthTree(root->rchild) + 1;
else
return 0;
}
二叉树叶子结点数
int LeafNumber(Node root)
{
if(!root)
return 0;
else if( (root->lchild == NULL) && (root->rchild == NULL) )
return 1;
else
return ( LeafNumber(root->lchild) + LeafNumber(root->rchild) );
}
先序遍历
void PreorderTraverse(Node root)
{
if(root)
{
cout << root->data<<' ';
PreorderTraverse(root->lchild);
PreorderTraverse(root->rchild);
}
}
中序遍历
void InorderTraverse(Node root)
{
if(root)
{
InorderTraverse(root->lchild);
cout<<root->data<<' ';
InorderTraverse(root->rchild);
}
}
后序遍历
void LastorderTraverse(Node root)
{
if(root)
{
LastorderTraverse(root->lchild);
LastorderTraverse(root->rchild);
cout<<root->data<<' ';
}
}
完整代码
#include<cstring>
#include<cstdlib>
#include <iostream>
using namespace std;
typedef int TelemType;
typedef struct BinaryTree
{
TelemType data;
struct BinaryTree *lchild;
struct BinaryTree *rchild;
}*Node,node;
Node CreatTree()
{
Node p;
TelemType a;
cin >> a;
if(a == 0)
p = NULL;
else
{
p = (Node)malloc(sizeof(node));
p->data = a;
p->lchild = CreatTree();
p->rchild = CreatTree();
}
return p;
}
void PreorderTraverse(Node root)
{
if(root)
{
cout << root->data<<' ';
PreorderTraverse(root->lchild);
PreorderTraverse(root->rchild);
}
}
void InorderTraverse(Node root)
{
if(root)
{
InorderTraverse(root->lchild);
cout<<root->data<<' ';
InorderTraverse(root->rchild);
}
}
void LastorderTraverse(Node root)
{
if(root)
{
LastorderTraverse(root->lchild);
LastorderTraverse(root->rchild);
cout<<root->data<<' ';
}
}
int NodeNumber(Node root)
{
if(root == NULL)
return 0;
else
return 1+NodeNumber(root->lchild) + NodeNumber(root->rchild);
}
int DepthTree(Node root)
{
if(root)
return DepthTree(root->lchild) > DepthTree(root->rchild) ? DepthTree(root->lchild) + 1 : DepthTree(root->rchild) + 1;
else
return 0;
}
int LeafNumber(Node root)
{
if(!root)
return 0;
else if( (root->lchild == NULL) && (root->rchild == NULL) )
return 1;
else
return ( LeafNumber(root->lchild) + LeafNumber(root->rchild) );
}
int main()
{
Node root=NULL;
cout<<"请输入数据:"<<endl;
root=CreatTree();
cout<<"二叉树建立成功!"<<endl;
cout<<"二叉树节点数为:"<<NodeNumber(root)<<endl;
cout<<"二叉树深度为:"<<DepthTree(root)<<endl;
cout<<"二叉树叶子结点数为:"<<LeafNumber(root)<<endl;
cout<<"前序遍历:"<<endl;
PreorderTraverse(root);
cout<<endl;
cout<<"中序遍历:"<<endl;
InorderTraverse(root);
cout<<endl;
cout<<"后序遍历:"<<endl;
LastorderTraverse(root);
cout<<endl;
return 0;
}
测试结果
1
2 3