Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cosmicing
Prj-Cpps
提交
0955102a
P
Prj-Cpps
项目概览
cosmicing
/
Prj-Cpps
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Prj-Cpps
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0955102a
编写于
4月 23, 2021
作者:
cosmicing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
上传新文件
上级
ab5baacd
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
232 addition
and
0 deletion
+232
-0
11-BinTree/21-IsBlancedTree/BinTree.cpp
11-BinTree/21-IsBlancedTree/BinTree.cpp
+232
-0
未找到文件。
11-BinTree/21-IsBlancedTree/BinTree.cpp
0 → 100644
浏览文件 @
0955102a
#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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录