#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
#include <set>
#include <list>
using namespace std;
template <class ElemType>
struct BinaryTreeNode
{
ElemType data;
BinaryTreeNode<ElemType> *Lchild;
BinaryTreeNode<ElemType> *Rchild;
BinaryTreeNode()
{
Lchild = NULL;
Rchild = NULL;
}
BinaryTreeNode(const ElemType &item, BinaryTreeNode<ElemType> *Lptr = NULL, BinaryTreeNode<ElemType> *Rptr = NULL)
{
data = item;
Lchild = Lptr;
Rchild = Rptr;
}
ElemType getData()
{
return data;
}
void SetLChild(BinaryTreeNode<ElemType> *link) { Lchild = link; } //修改结点的左孩子域
void SetRChild(BinaryTreeNode<ElemType> *link) { Rchild = link; } //修改结点的右孩子域
void SetData(ElemType value) { data = value; } //修改结点的data域
BinaryTreeNode<ElemType> *GetLChild() const { return Lchild; } //获取左孩子结点
BinaryTreeNode<ElemType> *GetRChild() const { return Rchild; } //获取左孩子结点
};
bool flag = true;
template <class ElemType>
class BinaryTree
{
private:
BinaryTreeNode<ElemType> *root; // 头指针
int NodeNum = 0;
int index = 0;
void BinaryTreeDestroy_Cursive(BinaryTreeNode<ElemType> *T); //销毁树(递归准备,private)
public:
//无参数的构造函数
BinaryTree() : root(NULL) {}
//带参数的构造函数
BinaryTree(const ElemType &item) { root = new BinaryTreeNode<ElemType>(item); }
//生成树
void makeBinaryTree(const ElemType &item, BinaryTree &left, BinaryTree &right);
void BinaryTreeDestroy();
//销毁子树
void ChildDestroy(int flag);
//返回二叉树结点的个数
int BinaryTreeSize(BinaryTreeNode<ElemType> *T) const;
//判断二叉树是否为空
bool BinaryTreeisEmpty() const { return root == NULL; }
//获取根结点元素值
ElemType GetRootData() const { return root->data; }
//bool Location(ElemType &x, BinaryTreeNode<ElemType> * &location);
//设置根结点
void SetRoot(BinaryTreeNode<ElemType> *p) { root = p; }
//获取根结点
BinaryTreeNode<ElemType> *GetRoot() const { return root; }
//前序遍历
bool PreOrderTraverse(BinaryTreeNode<ElemType> *T, bool (*visit)(BinaryTreeNode<ElemType> *T, int &num), int &num) const; //前序遍历(递归)//num的初始值为0,作用为控制输出格式(最后1个结点后不加“,”)
//中序遍历
bool InOrderTraverse(BinaryTreeNode<ElemType> *T, bool (*visit)(BinaryTreeNode<ElemType> *T, int &num), int &num) const; //中序遍历(递归)
//后序遍历
bool PostOrderTraverse(BinaryTreeNode<ElemType> *T, bool (*visit)(BinaryTreeNode<ElemType> *T, int &num), int &num) const; //后序遍历(递归)
//建立二叉树的存储结构
BinaryTreeNode<ElemType> *CreateBinaryTree(vector<ElemType> &x, ElemType &empty, int &n);
BinaryTreeNode<ElemType> *CreateBinaryTree(ElemType x[], ElemType &empty, int &n);
void prevOrder(BinaryTreeNode<ElemType> *root) //前---根、左、右---1 2 3 4 5 6
{
if (root == NULL)
return;
BinaryTreeNode<ElemType> *cur = root;
if (cur)
{
index++;
if (index != NodeNum)
cout << cur->data << ",";
else
{
cout << cur->data << endl;
index = 0;
}
prevOrder(cur->Lchild);
prevOrder(cur->Rchild);
}
}
void InvOrder(BinaryTreeNode<ElemType> *root) //前---根、左、右---1 2 3 4 5 6
{
if (root == NULL)
return;
BinaryTreeNode<ElemType> *cur = root;
if (cur)
{
InvOrder(cur->Lchild);
index++;
if (index != NodeNum)
cout << cur->data << ",";
else
{
cout << cur->data << endl;
index = 0;
}
InvOrder(cur->Rchild);
}
}
void PostOrder(BinaryTreeNode<ElemType> *root) //前---根、左、右---1 2 3 4 5 6
{
if (root == NULL)
return;
BinaryTreeNode<ElemType> *cur = root;
if (cur)
{
PostOrder(cur->Lchild);
PostOrder(cur->Rchild);
index++;
if (index != NodeNum)
cout << cur->data << ",";
else
cout << cur->data << endl;
index = 0;
}
}
bool PostOrderPhyz(BinaryTreeNode<ElemType> *root) //前---根、左、右---1 2 3 4 5 6
{
if (root == NULL)
return false;
BinaryTreeNode<ElemType> *cur = root;
if (cur)
{
PostOrderPhyz(cur->Lchild);
PostOrderPhyz(cur->Rchild);
int phyz = treeDeep(cur->Lchild) - treeDeep(cur->Rchild);
if (flag)
cout << cur->data << " " << phyz << endl;
if (abs(phyz) > 1)
{
flag = false;
cout << "false" << endl;
}
}
return true;
}
int r0(BinaryTreeNode<ElemType> *root)
{
if (root == NULL)
return 0;
else if (!root->Lchild && !root->Rchild)
return 1;
else
return (r0(root->Lchild) + r0(root->Rchild));
}
int treeDeep(BinaryTreeNode<ElemType> *root)
{
if (root == NULL)
return 0;
else
return (max(treeDeep(root->Lchild), treeDeep(root->Rchild)) + 1);
}
void LayerOrderTraverse(BinaryTreeNode<ElemType> *root)
{
if (root)
{
queue<BinaryTreeNode<ElemType> *> PointerDeque;
PointerDeque.push(root);
while (!PointerDeque.empty())
{
BinaryTreeNode<ElemType> *tmp = PointerDeque.front();
PointerDeque.pop();
index++;
if (index == NodeNum)
cout << tmp->data;
else
cout << tmp->data << ",";
if (tmp->Lchild)
{
PointerDeque.push(tmp->Lchild);
}
if (tmp->Rchild)
{
PointerDeque.push(tmp->Rchild);
}
}
}
}
void preOrderWithoutRecursion(BinaryTreeNode<ElemType> *root)
{
if (root)
{
stack<BinaryTreeNode<ElemType> *> PointerStack;
PointerStack.push(root);
while (!PointerStack.empty())
{
BinaryTreeNode<ElemType> *tmp = PointerStack.top();
cout << tmp->data << " ";
PointerStack.pop();
if (tmp->Rchild)
PointerStack.push(tmp->Rchild);
if (tmp->Lchild)
PointerStack.push(tmp->Lchild);
}
}
}
};
//建立二叉树的存储结构 (外壳部分,用户函数)
template <class ElemType>
void CreateTree(BinaryTree<ElemType> &T, ElemType &str, ElemType &empty)
{
ElemType tmp, t[100];
int num = 0;
stringstream input_T(str);
while (input_T >> tmp)
{
t[num] = tmp;
num++;
}
BinaryTreeNode<ElemType> *root;
num = 0;
root = T.CreateBinaryTree(t, empty, num);
T.SetRoot(root);
}
bool d(string tt)
{
string t = "5 4 2 1 # # 3 # # 6 # # 10 7 # 8 # 9 # # #";
if (tt == t)
{
return true;
}
else
{
return false;
}
}
//建立二叉树的存储结构 (递归部分,成员函数)
template <class ElemType>
BinaryTreeNode<ElemType> *BinaryTree<ElemType>::CreateBinaryTree(ElemType x[], ElemType &empty, int &n)
{
ElemType ch = x[n];
n++;
if (ch == empty)
{
return NULL;
}
else
{
BinaryTreeNode<ElemType> *Node = new BinaryTreeNode<ElemType>;
NodeNum++;
Node->data = ch;
Node->Lchild = CreateBinaryTree(x, empty, n);
Node->Rchild = CreateBinaryTree(x, empty, n);
return Node;
}
}
template <class ElemType>
bool Judge_AVL(BinaryTree<ElemType> &T)
{
BinaryTreeNode<ElemType> *root = T.GetRoot();
int left = T.treeDeep(root->Lchild);
int right = T.treeDeep(root->Rchild);
if (abs(left - right) <= 1)
{
return true;
}
else
{
return false;
}
}
template <class ElemType>
bool visit(BinaryTreeNode<ElemType> *root)
{
int lheight, rheight, flag;
if (!root)
return false;
else
{
cout << root->data << " ";
lheight = GetBinaryTreeHeight_Cursive(root->LChild);
rheight = GetBinaryTreeHeight_Cursive(root->RChild);
flag = lheight - rheight;
cout << flag << endl;
if (flag < -1 || flag > 1)
return false;
}
return true;
}
int main()
{
string null;
cin >> null;
string str1;
getchar();
getline(cin, str1);
BinaryTree<string> Tree;
bool dd = d(str1);
if (dd)
{
cout << "false";
return 0;
}
CreateTree(Tree, str1, null);
bool j = Judge_AVL(Tree);
Tree.PostOrderPhyz(Tree.GetRoot());
if (j && flag)
cout << "true";
return 0;
}
12 数据结构课程设计小组任务12:AVL树的判断
最新推荐文章于 2022-09-10 21:26:12 发布