二叉树的基本操作
实现二叉树的基本操作:建立、遍历、计算深度、结点数、叶子数等。
输入C,先序创建二叉树,#表示空节点;
输入H:计算二叉树的高度;
输入L:计算二叉树的叶子个数;
输入N:计算二叉树节点总个数;
输入1:先序遍历二叉树;
输入2:中序遍历二叉树;
输入3:后续遍历二叉树;
输入F:查找值=x的节点的个数;
输入P:以缩格文本形式输出所有节点。
例如:
输入
C
ABC##DE#G##F###
H
L
N
1
2
3
F
A
P
Result:
Created success!
Height=5.
Leaf=3.
Nodes=7.
Preorder is:A B C D E G F .
Inorder is:C B E G D F A .
Postorder is:C G E F D B A .
The count of A is 1.
The tree is:
A
B
C
D
E
G
F
#include <bits/stdc++.h>
using namespace std;
typedef char Datatype ;
class BinaryTree;
class BinTreeNode {
friend class BinaryTree;
private:
BinTreeNode *leftChild,*rightChild;
Datatype data;
public:
BinTreeNode() {
leftChild = NULL;
rightChild = NULL;
}
BinTreeNode(Datatype x, BinTreeNode *left = NULL, BinTreeNode *right = NULL) : data(x), leftChild(left),
rightChild(right) {};
~BinTreeNode() {};
};
class BinaryTree {
private:
BinTreeNode *root;
Datatype val;
void CreatBinTree(BinTreeNode *&subTree);
BinTreeNode *Parent(BinTreeNode *subTree, BinTreeNode *current);
int Height(BinTreeNode *subTree) const;
int Size(BinTreeNode *subTree) const;
void PreOrder(BinTreeNode *subTree);
void InOrder(BinTreeNode *subTree);
void PostOrder(BinTreeNode *subTree);
void levelOrder(BinTreeNode *subTree);
void destroy(BinTreeNode *subTree);
int leaf(BinTreeNode *subTree) const;
int Countnumber(BinTreeNode *subtree, char &ch);
void Print(BinTreeNode *subtree, int dep);
public:
BinaryTree() : root(NULL) {};
BinaryTree(Datatype Val) {
val = Val;
root = NULL;
}
~BinaryTree() { destroy(root); }
void CreatBinTree() { CreatBinTree(root); };
int IsEmpty() { return root == NULL; }
BinTreeNode *Parent(BinTreeNode *current) {
return (root == NULL || root == current) ? NULL : Parent(root, current);
}
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); }
BinTreeNode *GetRoot() const { return root; }
void PreOrder() { PreOrder(root); }
void InOrder() { InOrder(root); }
void PostOrder() { PostOrder(root); }
void levelOrder() { levelOrder(root); }
int leaf() { return leaf(root); }
int Countnumber(char &ch) { return Countnumber(root, ch); }
void Print() {
Print(root, 0);
}
};
void BinaryTree::Print(BinTreeNode *subtree,int dep) {
if (subtree == NULL) return;
for (int i = 0; i < dep; i++) {
cout << " ";
}
cout << subtree->data << endl;
Print(subtree->leftChild, dep + 1);
Print(subtree->rightChild, dep + 1);
}
BinTreeNode *BinaryTree::Parent(BinTreeNode *subTree, BinTreeNode *current) {
if (subTree == NULL) return NULL;
if (subTree->leftChild == current || subTree->rightChild == current) {
return subTree;
}
BinTreeNode *p;
if ((p = Parent(subTree->leftChild, current)) != NULL) return p;
else return Parent(subTree->rightChild, current);
}
void BinaryTree::destroy(BinTreeNode * subTree) {
if (subTree != NULL) {
destroy(subTree->leftChild);
destroy(subTree->rightChild);
delete subTree;
}
}
int BinaryTree::leaf(BinTreeNode * subTree)const {
if (subTree == NULL) return 0;
if (subTree->leftChild == NULL && subTree->rightChild == NULL) return 1;
return leaf(subTree->leftChild) + leaf(subTree->rightChild);
}
int BinaryTree::Countnumber(BinTreeNode *subtree, char &ch) {
if (subtree == NULL) return 0;
return Countnumber(subtree->leftChild, ch) + Countnumber(subtree->rightChild, ch) + (ch == subtree->data);
}
int BinaryTree::Height(BinTreeNode *subTree)const {
if (subTree == NULL) return 0;
return 1 + max(Height(subTree->leftChild), Height(subTree->rightChild));
}
int BinaryTree::Size(BinTreeNode *subTree)const {
if (subTree == NULL) return 0;
return 1 + Size(subTree->leftChild) + Size(subTree->rightChild);
}
void BinaryTree::InOrder(BinTreeNode *subTree) {
if (subTree != NULL) {
InOrder(subTree->leftChild);
cout << subTree->data << " ";
InOrder(subTree->rightChild);
}
}
void BinaryTree::PreOrder(BinTreeNode *subTree) {
if (subTree != NULL) {
cout << subTree->data << " ";
PreOrder(subTree->leftChild);
PreOrder(subTree->rightChild);
}
}
void BinaryTree::PostOrder(BinTreeNode *subTree) {
if (subTree != NULL) {
PostOrder(subTree->leftChild);
PostOrder(subTree->rightChild);
cout << subTree->data << " ";
}
}
int treenum;
void BinaryTree::CreatBinTree(BinTreeNode *&subTree) {
Datatype ch;
cin >> ch;
cout << val << endl;
if (ch == val) {
subTree = NULL;
} else {
subTree = new BinTreeNode(ch);
treenum++;
CreatBinTree(subTree->leftChild);
CreatBinTree(subTree->rightChild);
}
}
void run() {
char ch;
cin >> ch;
BinaryTree Tree('#');
Tree.CreatBinTree();
cout << "Created success!" << endl;
while (cin >> ch) {
switch (ch) {
case 'H':
printf("Height=%d.\n", Tree.Height());
break;
case 'L':
printf("Leaf=%d.\n", Tree.leaf());
break;
case 'N':
printf("Nodes=%d.\n", treenum);
break;
case '1':
printf("Preorder is:");
Tree.PreOrder();
printf(".\n");
break;
case '2':
printf("Inorder is:");
Tree.InOrder();
printf(".\n");
break;
case '3':
printf("Postorder is:");
Tree.PostOrder();
printf(".\n");
break;
case 'F':
char ch1;
cin >> ch1;
printf("The count of %c is %d.\n", ch1, Tree.Countnumber(ch1));
break;
case 'P':
printf("The tree is:\n");
Tree.Print();
break;
}
}
}
int main() {
freopen("1.txt", "r", stdin);
run();
return 0;
}