Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
东方怂天
DataStructure
提交
cfe44b0a
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 搜索 >>
提交
cfe44b0a
编写于
11月 16, 2019
作者:
东方怂天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Current files
上级
7a018afd
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
105 addition
and
261 deletion
+105
-261
.vscode/settings.json
.vscode/settings.json
+2
-1
树和二叉树/BinaryTree.h
树和二叉树/BinaryTree.h
+77
-165
树和二叉树/MotherFucker.cpp
树和二叉树/MotherFucker.cpp
+0
-80
树和二叉树/main.cpp
树和二叉树/main.cpp
+24
-15
树和二叉树/main.exe
树和二叉树/main.exe
+0
-0
树和二叉树/tempCodeRunnerFile.cpp
树和二叉树/tempCodeRunnerFile.cpp
+2
-0
未找到文件。
.vscode/settings.json
浏览文件 @
cfe44b0a
...
...
@@ -7,6 +7,7 @@
"ostream"
:
"cpp"
,
"vector"
:
"cpp"
,
"sstream"
:
"cpp"
,
"cmath"
:
"cpp"
"cmath"
:
"cpp"
,
"stack"
:
"cpp"
}
}
\ No newline at end of file
树和二叉树/BinaryTree.h
浏览文件 @
cfe44b0a
/**
* Introduce:
* 1, We use char '#' to behalf the null node.
*/
//Include some library.
//Include Liberary.
#include <iostream>
#include <string>
#include <stack>
//Imp
rt the namespace to this fil
e.
//Imp
ort the namespac
e.
using
namespace
std
;
//Marco define.
#define MAXSIZE 100
//Define a struct to behalf the BinaryTree.
//Define the binary tree struction.
typedef
struct
BiNode
{
char
data
;
struct
BiNode
*
LeftChild
;
char
Data
;
struct
BiNode
*
RightChild
;
}
BiNode
,
*
BiTree
;
//Expanding the first ordered binary tree
//Creat BiNode.
//recieves:
// BiNode *&BN : The node u want to open.
// char c : The data u want to push in.
//returns:
// None
void
CreatBiNode
(
BiNode
*&
BN
,
char
c
)
//Pre-order creat binary tree.
void
CreatBiTree_Rec
(
BiTree
&
root
)
{
//Open a new node.
BN
=
new
BiNode
;
cout
<<
"Now we new a new node."
<<
c
<<
endl
;
//Push the char into data.
BN
->
Data
=
c
;
cout
<<
"Push:
\t
"
<<
BN
->
Data
<<
endl
;
}
//Generating binary tree by recursion.
//recieves:
// BiTree &B : The node we should allocate memory space.
//returns:
// None
void
CreatBiTree_Recursion
(
BiTree
&
B
)
{
//Get the new node tag.
char
s
;
cin
>>
s
;
//Get the input.
char
c
;
cin
>>
c
;
//Determine the char entered.
switch
(
s
)
switch
(
c
)
{
//The node will be null.
case
'#'
:
B
=
NULL
;
root
=
NULL
;
break
;
//The node is not null.
default:
//Creat a new node which data is s.
CreatBiNode
(
B
,
s
);
//Start recursion.
CreatBiTree_Recursion
(
B
->
LeftChild
);
CreatBiTree_Recursion
(
B
->
RightChild
);
//Creat a new root.
root
=
new
BiNode
;
//Set its left child and right child be null.
root
->
LeftChild
=
root
->
RightChild
=
NULL
;
//Set the data value.
root
->
data
=
c
;
//Recursively generated child tree.
CreatBiTree_Rec
(
root
->
LeftChild
);
CreatBiTree_Rec
(
root
->
RightChild
);
break
;
}
}
//Define a stack of BiTree.
typedef
struct
BiTreeStack
{
BiNode
*
Stack
[
MAXSIZE
];
int
Top
;
int
Size
;
}
BiTreeStack
;
//Init a BiTreeStack.
void
InitBiTreeStack
(
BiTreeStack
&
BTS
)
//Loop to creat binary tree.
void
CreatBiTree_Loop
(
BiTree
&
root
)
{
BTS
.
Top
=
0
;
BTS
.
Size
=
MAXSIZE
;
}
stack
<
BiTree
>
BiStack
;
//Push to BiTreeStakc.
void
Push
(
BiTreeStack
&
BTS
,
BiNode
*
c
)
{
//Safety inspection.
if
(
BTS
.
Top
>=
BTS
.
Size
)
while
(
true
)
{
cout
<<
"The stakc is full"
<<
endl
;
return
;
}
//Push in.
//Creat a new node which data is s
BTS
.
Stack
[
BTS
.
Top
++
]
=
c
;
return
;
}
//Pop from BiTreeStakc.
void
Pop
(
BiTreeStack
&
BTS
,
BiNode
*&
c
)
enum
Type
{
Pre
,
In
,
Post
};
void
DisplayBiTree
(
BiTree
root
,
enum
Type
t
)
{
/*
//Safety inspection.
if (BTS.Top <= 0)
if
(
!
root
)
{
cout << "The stakc is full" << endl;
return
;
}
*/
//Pop it.
c
=
BTS
.
Stack
[
--
BTS
.
Top
];
return
;
if
(
t
==
Pre
)
{
cout
<<
root
->
data
;
}
DisplayBiTree
(
root
->
LeftChild
,
t
);
if
(
t
==
In
)
{
cout
<<
root
->
data
;
}
DisplayBiTree
(
root
->
RightChild
,
t
);
if
(
t
==
Post
)
{
cout
<<
root
->
data
;
}
}
void
CreatBiTree_Ergodic
(
BiTree
&
B
)
void
DisplayBiTree_Stack
(
BiTree
root
,
enum
Type
t
)
{
//Init a stack.
BiTreeStack
BTS
;
InitBiTreeStack
(
BTS
);
//Define a pointer to do many thing.
B
=
new
BiNode
;
BiNode
*
pB
=
B
;
pB
->
Data
=
getchar
();
cout
<<
"GET
\t
"
<<
pB
->
Data
<<
endl
;
//Push in.
Push
(
BTS
,
pB
);
cout
<<
"PUSH
\t
"
<<
pB
->
Data
<<
endl
;
//This condition is not well-defined.
char
c
;
bool
isRight
=
false
;
while
(
c
=
getchar
())
stack
<
BiTree
>
BiStack
;
BiNode
*
p
=
root
,
*
temp
=
NULL
;
do
{
switch
(
c
)
if
(
!
p
)
{
case
'#'
:
//If null stack then end.
if
(
BTS
.
Top
==
0
)
p
=
BiStack
.
top
();
if
(
t
!=
Post
)
{
return
;
BiStack
.
pop
()
;
}
//pB revoke.
Pop
(
BTS
,
pB
);
cout
<<
"POP
\t
"
<<
pB
->
Data
<<
endl
;
isRight
=
true
;
break
;
default:
if
(
!
isRight
)
if
(
t
==
In
)
{
pB
->
LeftChild
=
new
BiNode
;
pB
=
pB
->
LeftChild
;
cout
<<
p
->
data
;
}
else
if
(
t
==
Post
)
{
pB
->
RightChild
=
new
BiNode
;
pB
=
pB
->
RightChild
;
if
(
!
p
->
RightChild
||
p
->
RightChild
==
temp
)
{
cout
<<
p
->
data
;
BiStack
.
pop
();
temp
=
p
;
p
=
NULL
;
//This is important.
continue
;
}
}
//Tag the pB.
pB
->
Data
=
c
;
pB
->
LeftChild
=
pB
->
RightChild
=
NULL
;
cout
<<
"GET
\t
"
<<
c
<<
endl
;
//Push in.
Push
(
BTS
,
pB
);
cout
<<
"PUSH
\t
"
<<
pB
->
Data
<<
endl
;
cout
<<
"DIR
\t
"
<<
(
isRight
?
"Right"
:
"Left"
)
<<
endl
;
isRight
=
false
;
break
;
p
=
p
->
RightChild
;
continue
;
}
}
return
;
}
void
ShowBiTree
(
BiTree
&
B
)
{
if
(
!
B
)
{
return
;
}
cout
<<
B
->
Data
<<
" "
;
ShowBiTree
(
B
->
LeftChild
);
ShowBiTree
(
B
->
RightChild
);
return
;
}
void
ShowTree_Ergodic
(
BiTree
B
)
{
//Init a stack.
BiTreeStack
BTS
;
InitBiTreeStack
(
BTS
);
//左可左入栈,tag初始0
//无根出栈一,tag++
//tag==1,转向看入栈
//tag==2,出栈继续走
//cout << p->data;
BiStack
.
push
(
p
);
if
(
t
==
Pre
)
{
cout
<<
p
->
data
;
}
p
=
p
->
LeftChild
;
//因为要维护一个Stack,所以我不写了
}
while
(
!
BiStack
.
empty
()
||
p
);
}
\ No newline at end of file
树和二叉树/MotherFucker.cpp
已删除
100644 → 0
浏览文件 @
7a018afd
#include <iostream>
#include <stack>
#include <string>
using
namespace
std
;
class
BiNode
{
public:
BiNode
(){};
BiNode
(
char
c
)
:
val
(
c
){};
char
val
;
BiNode
*
left
=
NULL
;
BiNode
*
right
=
NULL
;
};
BiNode
*
addTree
(
string
str
)
{
BiNode
*
root
=
new
BiNode
(
str
[
0
]);
stack
<
BiNode
*>
stk
;
stk
.
push
(
root
);
int
id
=
1
;
bool
A
=
false
;
while
(
!
stk
.
empty
())
{
if
(
stk
.
top
()
->
left
==
NULL
&&
str
[
id
-
1
]
!=
'#'
)
{
if
(
str
[
id
]
!=
'#'
)
{
BiNode
*
left
=
new
BiNode
(
str
[
id
]);
stk
.
top
()
->
left
=
left
;
stk
.
push
(
left
);
}
id
++
;
}
else
{
if
(
stk
.
top
()
->
right
==
NULL
)
{
if
(
str
[
id
]
!=
'#'
)
{
BiNode
*
right
=
new
BiNode
(
str
[
id
]);
stk
.
top
()
->
right
=
right
;
stk
.
push
(
right
);
}
else
{
stk
.
pop
();
}
id
++
;
}
else
{
stk
.
pop
();
}
}
}
return
root
;
}
void
printTree
(
BiNode
*
root
)
{
if
(
root
!=
NULL
)
{
cout
<<
root
->
val
;
if
(
root
->
left
!=
NULL
)
{
printTree
(
root
->
left
);
}
if
(
root
->
right
!=
NULL
)
{
printTree
(
root
->
right
);
}
}
}
int
main
()
{
}
\ No newline at end of file
树和二叉树/main.cpp
浏览文件 @
cfe44b0a
//Include some library.
#include "BinaryTree.h"
//Function entrance.
int
main
()
{
/**
* Introduce:
* 1, Test input: AB#C##DE#F##G##
*/
BiTree
root
;
//State a BiTree.
BiTree
b
;
//ABD#E###CFG###H##
//Pre order:ABDECFGH
//In order:DEBAGFCH
//Post order:EDBGFHCA
cout
<<
"Input ebt:"
<<
endl
;
CreatBiTree_Rec
(
root
);
//Recursion type.
//CreatBiTree_Recursion(b);
//ShowBiTree(b);
//cout << endl;
cout
<<
"Pre order:"
<<
endl
;
DisplayBiTree_Stack
(
root
,
Pre
);
cout
<<
endl
;
DisplayBiTree
(
root
,
Pre
);
cout
<<
endl
;
//Ergodic type.
CreatBiTree_Ergodic
(
b
);
ShowBiTree
(
b
);
cout
<<
"In order:"
<<
endl
;
DisplayBiTree_Stack
(
root
,
In
);
cout
<<
endl
;
DisplayBiTree
(
root
,
In
);
cout
<<
endl
;
cout
<<
"Post order:"
<<
endl
;
DisplayBiTree_Stack
(
root
,
Post
);
cout
<<
endl
;
DisplayBiTree
(
root
,
Post
);
cout
<<
endl
;
return
0
;
}
\ No newline at end of file
树和二叉树/main.exe
浏览文件 @
cfe44b0a
无法预览此类型文件
树和二叉树/tempCodeRunnerFile.cpp
0 → 100644
浏览文件 @
cfe44b0a
ABD
#
E
###
CFG
###
H
##
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录