提交 0955102a 编写于 作者: cosmicing's avatar cosmicing

上传新文件

上级 ab5baacd
#include<iostream>
#include<string>
#include<string.h>
#include<stack>
using namespace std;
typedef char DataType;
class BinTreeNode {
public:
DataType data;
BinTreeNode *leftChild;
BinTreeNode *rightChild;
BinTreeNode() {
leftChild = NULL;
rightChild = NULL;
}
};
class BinaryTree {
public:
BinTreeNode *root;
//int index;
BinaryTree() {
//index = 0;
root = NULL;
}
~BinaryTree()
{
DeleteTree();
}
void PreOrder(BinTreeNode *current);
void InOrder(BinTreeNode *current);
void PostOrder(BinTreeNode *current);
void Destory(BinTreeNode *current);
void DeleteTree() {
Destory(root);
root = NULL;
}
bool IsEmpty() {
return root == NULL;
}
BinTreeNode *CreateBinTree();
BinTreeNode *CreateBinTree(char *str);
int GetBinTreeDepth(BinTreeNode *Tree);
int BTreeHigh(BinTreeNode *Tree);
int IsBalancedTree(BinTreeNode *Tree);
};
/******************BinaryTree class function realization Start***************************/
void BinaryTree::PreOrder(BinTreeNode *current) {
if(current != NULL) {
cout << current->data << " ";
PreOrder(current->leftChild);
PreOrder(current->rightChild);
}
}
BinTreeNode *BinaryTree::CreateBinTree() {
BinTreeNode *Tree = NULL;
char str;
cin >> str;
if(str != '#') {
Tree = new BinTreeNode;
Tree->data = str;
Tree->leftChild = CreateBinTree();
Tree->rightChild = CreateBinTree();
}
}
BinTreeNode *BinaryTree::CreateBinTree(char *str) {
BinTreeNode *Tree = NULL;
static int index = 0;
char value = str[index]; //可以将index放入到类的成员变量里面,创建之前记得将index = 0;这样后续再创建就不会出错.
if(str[index++] == '#') {
return NULL;
}
Tree = new BinTreeNode;
Tree->data = value;
Tree->leftChild = CreateBinTree(str);
Tree->rightChild = CreateBinTree(str);
if(str[index] == '\0') {
index = 0;
}
return Tree;
}
int BinaryTree::GetBinTreeDepth(BinTreeNode *Tree) {
int leftDepth = 0;
int rightDepth = 0;
int depth = 0;
if(Tree == NULL) {
return 0;
}
leftDepth = GetBinTreeDepth(Tree->leftChild);
rightDepth = GetBinTreeDepth(Tree->rightChild);
depth = leftDepth > rightDepth ? leftDepth : rightDepth;
return depth + 1;
}
int BinaryTree::BTreeHigh(BinTreeNode *Tree) {
if(Tree == NULL) {
return 0;
}
int hleft = BTreeHigh(Tree->leftChild);
int hright = BTreeHigh(Tree->rightChild);
return hleft > hright ? hleft + 1 : hright + 1;
}
int BinaryTree::IsBalancedTree(BinTreeNode *Tree) {
if(Tree == NULL) {
return 1;
}
int hleft = BTreeHigh(Tree->leftChild);
int hright = BTreeHigh(Tree->rightChild);
int gap = hleft - hright;
if(gap > 1 || gap < -1) {
return 0;
}
return IsBalancedTree(Tree->leftChild) && IsBalancedTree(Tree->rightChild);
}
void BinaryTree::Destory(BinTreeNode *current) {
if(current != NULL) {
Destory(current->leftChild);
Destory(current->rightChild);
delete current;
}
}
/**************树的结构体:算法思想*********************/
/*
* 1) char Tsrc[]={"ABC##D##E#F#G##"};
*
* A
* / \
* / \
* B E
* / \ /
* C D F
* \
* G
*
* 2) char Tsrc[]={"ABDG##H###CE#I##F##"};
*
* A
* / \
* / \
* / \
* / \
* / \
* B C
* / \ / \
* / \ / \
* / \ / \
* D # E F
* / \ / \ / \
* / \ / \ / \
* G H # I # #
* / \ / \ / \
* # # # # # #
*
*
* 3) char Tsrc[]={"ABDG##H###CE#I##F##"};
*
* A
* / \
* / \
* / \
* / \
* / \
* B C
* / \ / \
* / \ / \
* / \ / \
* D E F G
* / \ / \ / \
* / \ / \ / \
* H I # J # #
* / \ / \ / \
* # # # # # #
*/
#define N 200
int main()
{
#if 0
char str[1000] = { 0 };
cin.getline(str, 1000);
cout << str << endl;
#else
char str[N] = {"ABDG##H###CE#I##F##"};
char strBl[N] = {"ABDH##I##E##CF#J##G##"};
#endif
char *p;
p = str;
BinaryTree Tree;
BinaryTree Tree1;
//Tree.root = Tree.CreateBinTree();
Tree.root = Tree.CreateBinTree(p);
Tree1.root = Tree1.CreateBinTree(strBl);
//Tree1.root = Tree1.CreateBinTree(p);
Tree.PreOrder(Tree.root);
cout << endl;
int treeDepth = 0;
treeDepth = Tree.GetBinTreeDepth(Tree.root);
cout << "Tree Depth = " << treeDepth << endl;
if(Tree.IsBalancedTree(Tree.root)) {
cout << "The Tree is a Balanced Tree" << endl ;
} else {
cout << "The Tree is not a Balanced Tree" << endl ;
}
if(Tree1.IsBalancedTree(Tree1.root)) {
cout << "The Tree1 is a Balanced Tree" << endl ;
} else {
cout << "The Tree1 is not a Balanced Tree" << endl ;
}
return EXIT_SUCCESS;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册