Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
东方怂天
DataStructure
提交
19926ca1
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 搜索 >>
提交
19926ca1
编写于
11月 19, 2019
作者:
东方怂天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Huffman Tree Basic
上级
ac6c24d6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
203 addition
and
2 deletion
+203
-2
树和二叉树/Huffman/Huyffman.h
树和二叉树/Huffman/Huyffman.h
+180
-0
树和二叉树/Huffman/main.cpp
树和二叉树/Huffman/main.cpp
+22
-0
树和二叉树/main.cpp
树和二叉树/main.cpp
+1
-2
未找到文件。
树和二叉树/Huffman/Huyffman.h
0 → 100644
浏览文件 @
19926ca1
#include <iostream>
#include <stack>
using
namespace
std
;
//Define the max size of huffman tree.
//Actually we can only use such size if as key.
//128*2-1
#define ByteSize 128
#define MAXSIZE ByteSize * 2 - 1
//Define the element type which include weight information.
typedef
struct
{
int
weight
;
int
parent
;
int
leftChild
;
int
rightChild
;
}
WeightInformation
;
//Define the huffman tree type.
typedef
struct
{
WeightInformation
Elem
[
MAXSIZE
];
int
num
,
root
;
}
HuffmanTree
;
typedef
struct
{
char
Character
;
char
*
Code
;
}
HuffmanEncode
;
//Defined
typedef
struct
{
HuffmanEncode
*
Coding
;
int
num
;
}
HuffmanCode
;
void
FindMin_2
(
HuffmanTree
HT
,
int
&
min_1
,
int
&
min_2
);
//Find the min 2 & return the count size.
void
CreateHuffmanTree
(
HuffmanTree
&
HT
,
HuffmanCode
&
HC
,
int
*
Weight
,
char
*
Character
,
int
num
);
//How 2 creat a huffman tree.
void
DisplayHuffmanTree
(
HuffmanTree
HT
);
//Show a huffman tree.
void
DisplayHuffmanCode
(
HuffmanCode
HC
);
//Show a huffman code.
//How 2 creat a huffman tree.
void
CreateHuffmanTree
(
HuffmanTree
&
HT
,
HuffmanCode
&
HC
,
int
*
Weight
,
char
*
Character
,
int
num
)
{
//Loop controller.
int
i
,
j
;
//Copy weiight to huffman tree.
HT
.
num
=
num
;
for
(
i
=
0
;
i
<
num
;
i
++
)
{
HT
.
Elem
[
i
]
=
{
Weight
[
i
],
-
1
,
-
1
,
-
1
};
}
cout
<<
"Huffman tree init successfullt!"
<<
endl
;
// Find the 2 min position.
int
min_1_pos
,
min_2_pos
;
for
(
i
=
0
;
i
<
num
-
1
;
i
++
)
{
FindMin_2
(
HT
,
min_1_pos
,
min_2_pos
);
//Union 2 one.
HT
.
Elem
[
HT
.
num
]
=
{
HT
.
Elem
[
min_1_pos
].
weight
+
HT
.
Elem
[
min_2_pos
].
weight
,
-
1
,
min_1_pos
,
min_2_pos
};
HT
.
Elem
[
min_1_pos
].
parent
=
HT
.
Elem
[
min_2_pos
].
parent
=
HT
.
num
++
;
}
cout
<<
"Huffman tree creat successfullt!"
<<
endl
;
//Creat huffmancode.
HC
.
num
=
num
;
stack
<
int
>
ReverseCode
;
WeightInformation
WI
;
int
pla
,
count
;
HC
.
Coding
=
new
HuffmanEncode
[
num
];
for
(
i
=
0
;
i
<
num
;
i
++
)
{
HC
.
Coding
[
i
].
Character
=
Character
[
i
];
WI
=
HT
.
Elem
[
i
];
pla
=
i
;
while
(
WI
.
parent
!=
-
1
)
{
if
(
HT
.
Elem
[
WI
.
parent
].
leftChild
==
pla
)
{
ReverseCode
.
push
(
0
);
}
else
{
ReverseCode
.
push
(
1
);
}
pla
=
WI
.
parent
;
WI
=
HT
.
Elem
[
pla
];
}
//Creat huffman coding table.
HC
.
Coding
[
i
].
Code
=
new
char
[
ReverseCode
.
size
()
+
1
];
count
=
ReverseCode
.
size
();
for
(
j
=
0
;
j
<
count
;
j
++
)
{
HC
.
Coding
[
i
].
Code
[
j
]
=
'0'
+
ReverseCode
.
top
();
ReverseCode
.
pop
();
}
HC
.
Coding
[
i
].
Code
[
j
]
=
'\0'
;
}
//cout << "Huffman tree creat successfullt!" << endl;
}
//To find the min of the vector.
void
FindMin_2
(
HuffmanTree
HT
,
int
&
min_1
,
int
&
min_2
)
{
int
m1
,
m2
,
num
;
min_1
=
min_2
=
m1
=
m2
=
0
;
//For each element in huffman tree.
for
(
int
i
=
0
;
i
<
HT
.
num
;
i
++
)
{
//If this elem its father do not confirm(not well-defined) then continue.
if
(
HT
.
Elem
[
i
].
parent
!=
-
1
)
{
continue
;
}
//Get the num of it.
num
=
HT
.
Elem
[
i
].
weight
;
if
(
m2
!=
0
&&
m1
!=
0
)
{
if
(
num
<
m1
)
{
m2
=
m1
;
min_2
=
min_1
;
m1
=
num
;
min_1
=
i
;
}
else
if
(
num
<
m2
)
{
m2
=
num
;
min_2
=
i
;
}
continue
;
}
if
(
m1
==
0
)
{
m1
=
num
;
min_1
=
i
;
continue
;
}
if
(
m2
==
0
)
{
m2
=
num
;
min_2
=
i
;
}
if
(
m2
<
m1
)
{
m2
+=
m1
;
m1
=
m2
-
m1
;
m2
-=
m1
;
min_2
+=
min_1
;
min_1
=
min_2
-
min_1
;
min_2
-=
min_1
;
}
}
}
//Show huffman tree.
void
DisplayHuffmanTree
(
HuffmanTree
HT
)
{
for
(
int
i
=
0
;
i
<
HT
.
num
;
i
++
)
{
WeightInformation
WT
=
HT
.
Elem
[
i
];
cout
<<
WT
.
weight
<<
"
\t
"
<<
WT
.
parent
<<
"
\t
"
<<
WT
.
leftChild
<<
"
\t
"
<<
WT
.
rightChild
<<
endl
;
}
}
void
DisplayHuffmanCode
(
HuffmanCode
HC
)
{
for
(
int
i
=
0
;
i
<
HC
.
num
;
i
++
)
{
cout
<<
HC
.
Coding
[
i
].
Character
<<
'\t'
<<
HC
.
Coding
[
i
].
Code
<<
endl
;
}
}
\ No newline at end of file
树和二叉树/Huffman/main.cpp
0 → 100644
浏览文件 @
19926ca1
#include "Huyffman.h"
int
main
()
{
HuffmanTree
HT
;
HuffmanCode
HC
;
int
WEIGHT
[
ByteSize
]
=
{
5
,
29
,
7
,
8
,
14
,
23
,
3
,
11
};
char
CHARACTER
[
ByteSize
]
=
{
65
,
66
,
67
,
68
,
69
,
70
,
71
,
72
};
int
NUM
=
8
;
CreateHuffmanTree
(
HT
,
HC
,
WEIGHT
,
CHARACTER
,
NUM
);
//cout << HT.Elem[HT.num - 1].weight;
cout
<<
"Huffman Tree:"
<<
endl
;
DisplayHuffmanTree
(
HT
);
cout
<<
endl
;
cout
<<
"Huffman Code:"
<<
endl
;
DisplayHuffmanCode
(
HC
);
return
0
;
}
\ No newline at end of file
树和二叉树/main.cpp
浏览文件 @
19926ca1
...
...
@@ -10,7 +10,6 @@ int main()
//In order:DEBAGFCH
//Post order:EDBGFHCA
/*
cout
<<
"Input ebt:"
<<
endl
;
//CreatBiTree_Rec(root);
CreatBiTree_Loop
(
root
);
...
...
@@ -32,7 +31,7 @@ int main()
cout
<<
endl
;
DisplayBiTree
(
root
,
Post
);
cout
<<
endl
;
*/
UnionPreIn
(
"ABDECFGH"
,
"DEBAGFCH"
,
root
);
cout
<<
endl
;
DisplayBiTree
(
root
,
Pre
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录