# 二叉树相关操作之一

//erchashu.h

#include<iostream>
#include<stdio.h>
#include<queue>

using namespace std;

template <class T>
struct BinTreeNode {	      //二叉树结点类定义
T data;	 		      //数据域
BinTreeNode<T> *leftChild, *rightChild;
//左子女、右子女链域
BinTreeNode ()                //构造函数
{
leftChild = NULL;
rightChild = NULL;
}
BinTreeNode (T x)
{
BinTreeNode<T> *l = NULL;
BinTreeNode<T> *r = NULL;
data = x;
leftChild = l;
rightChild = r;
}
};

template <class T>
class BinaryTree
{
public:
void CreateBinTree1()
{
CreateBinTree(root);
}
void PreOrder1()
{
PreOrder(root);
}
void InOrder1()
{
InOrder(root);
}
void PostOrder1()
{
PostOrder(root);
}
void levelOrder1()
{
levelOrder(root);
}
int Hight1()
{
return Hight(root);
}
int Size1()
{
return Size(root);
}
int leafSize1()
{
return leafSize(root);
}
int doubleSize1()
{
return doubleSize(root);
}
int singleSize1()
{
return singleSize(root);
}
private:
void CreateBinTree(BinTreeNode<T> *& subTree);    //建立二叉树
void PreOrder(BinTreeNode<T> *& subTree);			//前序、
void InOrder(BinTreeNode<T> *& subTree);				//中序、
void PostOrder(BinTreeNode<T> *& subTree);			//后序
void levelOrder(BinTreeNode<T> *& root);					//层次顺序遍历二叉树的算法
int Hight(BinTreeNode<T> *  subTree);               //二叉树深度、
int Size(BinTreeNode<T> *  subTree);					//所有结点总数、
int leafSize(BinTreeNode<T> *  subTree);				//叶子结点数、
int doubleSize(BinTreeNode<T> *  subTree);			//双孩子结点个数、
int singleSize(BinTreeNode<T> *  subTree);			//单孩子结点个数的算法
BinTreeNode<T> *root;
char ch;
};

template <class T>
void BinaryTree<T>::CreateBinTree(BinTreeNode<T> *& subTree)    //建立二叉树
{
scanf("%c", &ch);
if (ch == 'a')
{
subTree = NULL;
}
else
{
subTree = new BinTreeNode<T>;
if (subTree == NULL)
{
cerr << "存储分配错!" << endl;
exit (1);
}
subTree->data = ch;
CreateBinTree(subTree->leftChild);
CreateBinTree(subTree->rightChild);
}
}

template <class T>
void BinaryTree<T>::PreOrder(BinTreeNode<T> *& subTree)			//前序、
{
if (subTree != NULL)
{
cout << subTree->data;
PreOrder(subTree->leftChild);
PreOrder(subTree->rightChild);
}
}

template <class T>
void BinaryTree<T>::InOrder(BinTreeNode<T> *& subTree)				//中序、
{
if (subTree != NULL)
{
InOrder(subTree->leftChild);
cout << subTree->data;
InOrder(subTree->rightChild);
}
}

template <class T>
void BinaryTree<T>::PostOrder(BinTreeNode<T> *& subTree)			//后序
{
if (subTree != NULL)
{
PostOrder(subTree->leftChild);
PostOrder(subTree->rightChild);
cout << subTree->data;
}
}

template <class T>
void BinaryTree<T>::levelOrder(BinTreeNode<T> *& root)					//层次顺序遍历二叉树的算法
{
if (root == NULL) return;
queue<BinTreeNode<T> * > Q;
Q.push(root);
while (!Q.empty())
{
cout << Q.front()->data;
if (Q.front()->leftChild != NULL)
{
Q.push(Q.front()->leftChild);
}
if (Q.front()->rightChild != NULL)
{
Q.push(Q.front()->rightChild);
}
Q.pop();
}
}

template <class T>
int BinaryTree<T>::Hight(BinTreeNode<T> *  subTree)               //二叉树深度、
{
if (subTree == NULL)
return 0;
else
{
int i = Hight(subTree->leftChild);
int j = Hight(subTree->rightChild);
return i > j ? i + 1 : j + 1;
}
}

template <class T>
int BinaryTree<T>::Size(BinTreeNode<T> *  subTree)					//所有结点总数、
{
if (subTree == NULL)
return 0;
else
return 1 + Size(subTree->leftChild) + Size(subTree->rightChild);
}

template <class T>
int BinaryTree<T>::leafSize(BinTreeNode<T> *  subTree)				//叶子结点数、
{
if (subTree == NULL)
{
return 0;
}
if (subTree->leftChild == NULL && subTree->rightChild == NULL)
{
return 1;
}
else
{
return leafSize(subTree->leftChild) + leafSize(subTree->rightChild);
}
}

template <class T>
int BinaryTree<T>::doubleSize(BinTreeNode<T> *  subTree)			//双孩子结点个数、
{
if (subTree == NULL)
return 0;
else
{
if (subTree->leftChild != NULL && subTree->rightChild != NULL)
return 1;
else
return doubleSize(subTree->leftChild) + doubleSize(subTree->rightChild);
}
}

template <class T>
int BinaryTree<T>::singleSize(BinTreeNode<T> *  subTree)			//单孩子结点个数的算法
{
if (subTree == NULL)
return 0;
else
{
if (subTree->leftChild == NULL || subTree->rightChild == NULL)
return 1;
else
return singleSize(subTree->leftChild) + singleSize(subTree->rightChild);
}
}

//main.h

#include"erchashu.h"

int main()
{
BinaryTree<char> s;
s.CreateBinTree1();
cout << "前序" << endl;
s.PreOrder1();
cout << endl;
cout << "中序" << endl;
s.InOrder1();
cout << endl;
cout << "后序" << endl;
s.PostOrder1();
cout << endl;
cout << "层次顺序遍历" << endl;
s.levelOrder1();
cout << endl;
cout << "二叉树深度、" << endl;
cout << s.Hight1() << endl;
cout << "所有结点总数" << endl;
cout << s.Size1() << endl;
cout << "叶子结点数" << endl;
cout << s.leafSize1() << endl;
cout << "双孩子结点个数" << endl;
cout << s.doubleSize1() << endl;
cout << "单孩子结点个数" << endl;
cout << s.singleSize1() << endl;
return 0;
}


• 本文已收录于以下专栏：

## Java实现二叉树的相关操作

• 2015年08月22日 17:05
• 7KB
• 下载

## 二叉树操作的相关代码

• 2014年02月28日 19:13
• 8KB
• 下载

## 二叉树的相关操作（二）

package dubletree; import java.util.Scanner; import java.util.Stack; import java.util.concurrent....

## 二叉树相关操作

• 2016年01月04日 14:14
• 4KB
• 下载

## 二叉树相关操作代码

• 2012年05月08日 16:17
• 61KB
• 下载

## 二叉树的相关操作

public class BinarySearchTree {/** * 二叉树节点结构 */ private static class BinaryNode { Integer ele...

## 数据结构 二叉树相关操作

• 2012年03月28日 21:35
• 9KB
• 下载

## 用java实现二叉树相关操作（前序建树，前中后递归非递归遍历，层序遍历）

java实现二叉树相关操作，欢迎讨论

## 很全面的二叉树相关操作

举报原因： 您举报文章：二叉树相关操作之一 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)