实现二叉树的基本操作:建立、遍历、计算深度、结点数、叶子数等。
输入C,先序创建二叉树,#表示空节点;
输入H:计算二叉树的高度;
输入L:计算二叉树的叶子个数;
输入N:计算二叉树节点总个数;
输入1:先序遍历二叉树;
输入2:中序遍历二叉树;
输入3:后续遍历二叉树;
输入F:查找值=x的节点的个数;
输入P:以缩格文本形式输出所有节点。
ABC##DE#G##F###
H
L
N
1
2
3
F
A
P
#include <iostream>
using namespace std;
class BinaryTree;
class BinTreeNode {
friend BinaryTree;
private:
BinTreeNode * leftChild, *rightChild;
char data;
public:
BinTreeNode() {
leftChild = NULL;
rightChild = NULL;
}
BinTreeNode(char x,BinTreeNode *left=NULL,BinTreeNode *right=NULL) {
data = x;
leftChild = left;
rightChild = right;
}
~BinTreeNode() {}
};
class BinaryTree {
private:
BinTreeNode * root;
char endtag;
int cnt = 0;
void createBinTree(BinTreeNode* &subTree);
BinTreeNode *parent(BinTreeNode *subTree, BinTreeNode *current);
int height(BinTreeNode *subTree);
int size(BinTreeNode *subTree);
int leaf(BinTreeNode *subTree);
void destroy(BinTreeNode *&subTree);
void preOrder(BinTreeNode *subTree);
void inOrder(BinTreeNode *subTree);
void postOrder(BinTreeNode *subTree);
void find(BinTreeNode *subTree, char x);
void show(BinTreeNode *subTree,int cnt);
public:
int cn = 0;
BinaryTree(char value) {
endtag = value;
root = NULL;
}
~BinaryTree() {
destroy(root); //why must destroy;
}
void createBinTree() {
createBinTree(root);
}
bool IsEmpty(){
return(root == NULL) ? true : false;
}
BinTreeNode *parent(BinTreeNode *current) {
return(root == NULL || root == current) ? NULL : parent(root, current); //parent wahts the
}
BinTreeNode *leftChild(BinTreeNode *current) {
return(current != NULL) ? current->leftChild : NULL;
}
BinTreeNode *rightChild(BinTreeNode *current) {
return(current != NULL) ? current->rightChild : NULL;
}
int height() {
return height(root);
}
int size() {
return size(root);
}
int leaf() {
return leaf(root);
}
void preOrder() {
preOrder(root);
}
void inOrder() {
inOrder(root);
}
void postOrder() {
postOrder(root);
}
void find(char x) {
find(root, x);
}
void show(int &cnt) {
show(root,cnt);
}
};
void BinaryTree::createBinTree(BinTreeNode *& subTree)
{
char item;
cin >> item;
if (item != endtag) {
subTree = new BinTreeNode(item);
createBinTree(subTree->leftChild);
createBinTree(subTree->rightChild);
}
else subTree = NULL; //this line can be remove;
}
BinTreeNode * BinaryTree::parent(BinTreeNode * subTree, BinTreeNode * current)
{
if (subTree == NULL)
return NULL;
if (subTree->leftChild == current || subTree->rightChild == current)
return subTree; // what the reason
BinTreeNode *p = parent(subTree->leftChild, current);
if (p != NULL)
return p;
else
return (parent(subTree->rightChild, current));
}
int BinaryTree::height(BinTreeNode * subTree)
{
if (subTree == NULL)
return 0;
else if (height(subTree->leftChild) > height(subTree->rightChild))
return 1 + height(subTree->leftChild);
else
return 1 + height(subTree->rightChild);
}
int BinaryTree::size(BinTreeNode * subTree)
{
if (subTree == NULL) {
return 0;
}
else {
return 1 + size(subTree->leftChild) + size(subTree->rightChild);
}
}
int BinaryTree::leaf(BinTreeNode * subTree)//need to be rewrite
{
if (subTree != NULL) {
if (subTree->leftChild == NULL && subTree->rightChild == NULL)
{
cnt++;
}
leaf(subTree->leftChild);
leaf(subTree->rightChild);
}
return cnt;
}
void BinaryTree::destroy(BinTreeNode *& subTree)
{
if (subTree != NULL) {
destroy(subTree->leftChild);
destroy(subTree->rightChild);
delete subTree;
}
}
void BinaryTree::preOrder(BinTreeNode * subTree)
{
if (subTree != NULL) {
cout << subTree->data<<" ";
preOrder(subTree->leftChild);
preOrder(subTree->rightChild);
}
}
void BinaryTree::inOrder(BinTreeNode * subTree)
{
if (subTree != NULL) {
inOrder(subTree->leftChild);
cout << subTree->data << " ";
inOrder(subTree->rightChild);
}
}
void BinaryTree::postOrder(BinTreeNode * subTree)
{
if (subTree != NULL) {
postOrder(subTree->leftChild);
postOrder(subTree->rightChild);
cout << subTree->data << " ";
}
}
void BinaryTree::find(BinTreeNode * subTree, char x)
{
if (subTree != NULL) {
find(subTree->leftChild,x);
find(subTree->rightChild,x);
if (subTree->data == x) {
cn++;
}
}
}
void BinaryTree::show(BinTreeNode * subTree,int cnt)
{
if (subTree != NULL) {
for (int i = 0; i < cnt; i++) {
cout << " ";
}
cout << subTree->data<<endl;
show(subTree->leftChild, cnt+1);
show(subTree->rightChild, cnt+1);
}
}
int main()
{
BinaryTree b('#');
char c;
while (cin >> c) {
if (c == 'C') {
b.createBinTree();
if (b.IsEmpty() != true) {
cout << "Created success!" << endl;
}
}
else if (c == 'H') {
cout << "Height=" << b.height() << "." << endl;
}
else if (c == 'L') {
cout << "Leaf=" << b.leaf() << "." << endl;
}
else if (c == '1') {
cout << "Preorder is:";
b.preOrder();
cout << "." << endl;
}
else if (c == 'N') {
cout << "Nodes=" << b.size() << "." << endl;
}
else if (c == '2') {
cout << "Inorder is:";
b.inOrder();
cout << "." << endl;
}
else if (c == '3') {
cout << "Postorder is:";
b.postOrder();
cout << "." << endl;
}
else if (c == 'F') {
char x;
cin >> x;
b.find(x);
cout << "The count of " << x << " is " << b.cn << "." << endl;
}
else if (c == 'P') {
cout << "The tree is:" << endl;
int cnt = 0;
b.show(cnt);
}
}
return 0;
}