Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
东方怂天
DataStructure
提交
ac6c24d6
D
DataStructure
项目概览
东方怂天
/
DataStructure
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DataStructure
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ac6c24d6
编写于
11月 16, 2019
作者:
东方怂天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add extention about how to creat a binary tree from pre-order & in-order.
上级
fddcbdd3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
110 addition
and
2 deletion
+110
-2
树和二叉树/BinaryTree.h
树和二叉树/BinaryTree.h
+50
-1
树和二叉树/Extention.h
树和二叉树/Extention.h
+45
-0
树和二叉树/main.cpp
树和二叉树/main.cpp
+15
-1
未找到文件。
树和二叉树/BinaryTree.h
浏览文件 @
ac6c24d6
#pragma once
//Include Liberary.
#include <iostream>
#include <stack>
...
...
@@ -42,11 +44,58 @@ void CreatBiTree_Rec(BiTree &root)
//Loop to creat binary tree.
void
CreatBiTree_Loop
(
BiTree
&
root
)
{
char
c
;
stack
<
BiTree
>
BiStack
;
while
(
true
)
root
=
new
BiNode
;
BiNode
*
p
=
root
;
bool
IsRight
=
false
;
//Define a pointer to do many thing.
p
->
data
=
getchar
();
//Push in.
BiStack
.
push
(
p
);
while
(
c
=
getchar
())
{
switch
(
c
)
{
case
'#'
:
//If null stack then end.
if
(
BiStack
.
empty
())
{
return
;
}
//pB revoke.
p
=
BiStack
.
top
();
BiStack
.
pop
();
IsRight
=
true
;
break
;
default:
if
(
IsRight
)
{
p
->
RightChild
=
new
BiNode
;
p
=
p
->
RightChild
;
}
else
{
p
->
LeftChild
=
new
BiNode
;
p
=
p
->
LeftChild
;
}
//Tag the pB.
p
->
data
=
c
;
p
->
LeftChild
=
p
->
RightChild
=
NULL
;
//Push in.
BiStack
.
push
(
p
);
IsRight
=
false
;
break
;
}
}
return
;
}
enum
Type
...
...
树和二叉树/Extention.h
0 → 100644
浏览文件 @
ac6c24d6
#pragma once
#include "BinaryTree.h"
#include <string>
void
UnionPreIn
(
string
Pre
,
string
In
,
BiTree
&
root
)
{
//Pre order:ABDECFGH
//In order:DEBAGFCH
root
=
new
BiNode
;
root
->
data
=
Pre
[
0
];
string
left
,
right
;
int
rootPos
=
In
.
find
(
Pre
[
0
]);
left
=
In
.
substr
(
0
,
rootPos
);
right
=
In
.
substr
(
rootPos
+
1
,
In
.
length
());
int
lLength
=
left
.
length
();
int
rLength
=
right
.
length
();
root
->
LeftChild
=
root
->
RightChild
=
NULL
;
if
(
lLength
!=
0
)
{
cout
<<
"LEFT"
<<
endl
;
cout
<<
'\t'
<<
Pre
.
substr
(
1
,
lLength
)
<<
" "
<<
left
<<
endl
;
root
->
LeftChild
=
new
BiNode
;
cout
<<
root
->
data
<<
"l"
<<
Pre
.
substr
(
1
,
lLength
)[
0
]
<<
endl
;
root
->
LeftChild
->
data
=
Pre
.
substr
(
1
,
lLength
)[
0
];
UnionPreIn
(
Pre
.
substr
(
1
,
lLength
),
left
,
root
->
LeftChild
);
}
if
(
rLength
!=
0
)
{
cout
<<
"RIGHT"
<<
endl
;
cout
<<
'\t'
<<
Pre
.
substr
(
Pre
.
length
()
-
rLength
,
Pre
.
length
())
<<
" "
<<
right
<<
endl
;
root
->
RightChild
=
new
BiNode
;
cout
<<
root
->
data
<<
"r"
<<
Pre
.
substr
(
Pre
.
length
()
-
rLength
,
Pre
.
length
())[
0
]
<<
endl
;
root
->
RightChild
->
data
=
Pre
.
substr
(
Pre
.
length
()
-
rLength
,
Pre
.
length
())[
0
];
UnionPreIn
(
Pre
.
substr
(
Pre
.
length
()
-
rLength
,
Pre
.
length
()),
right
,
root
->
RightChild
);
}
}
\ No newline at end of file
树和二叉树/main.cpp
浏览文件 @
ac6c24d6
#include "BinaryTree.h"
#include "Extention.h"
int
main
()
{
...
...
@@ -8,8 +9,11 @@ int main()
//Pre order:ABDECFGH
//In order:DEBAGFCH
//Post order:EDBGFHCA
/*
cout << "Input ebt:" << endl;
CreatBiTree_Rec
(
root
);
//CreatBiTree_Rec(root);
CreatBiTree_Loop(root);
cout << "Pre order:" << endl;
DisplayBiTree_Stack(root, Pre);
...
...
@@ -28,6 +32,15 @@ int main()
cout << endl;
DisplayBiTree(root, Post);
cout << endl;
*/
UnionPreIn
(
"ABDECFGH"
,
"DEBAGFCH"
,
root
);
cout
<<
endl
;
DisplayBiTree
(
root
,
Pre
);
cout
<<
endl
;
DisplayBiTree
(
root
,
In
);
cout
<<
endl
;
DisplayBiTree
(
root
,
Post
);
cout
<<
endl
;
return
0
;
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录