Skip to content

Commit 200298e

Browse files
Add files via upload
1 parent 3b59b07 commit 200298e

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

hw5_huffman_codes.c

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define SIZE 47
5+
6+
typedef struct _huffman
7+
{
8+
char token;
9+
int frequency;
10+
struct _huffman* leftChild;
11+
struct _huffman* rightChild;
12+
} HUFFMAN;
13+
14+
void insertHeap(HUFFMAN* theHeap[SIZE], HUFFMAN* newNode, int count);
15+
HUFFMAN* removeHeap(HUFFMAN* Heap[SIZE], int point);
16+
void preorderRecursive(HUFFMAN* theRoot);
17+
void inorderRecursive(HUFFMAN* Root);
18+
19+
int main()
20+
{
21+
HUFFMAN* heap[SIZE] = { NULL };
22+
HUFFMAN* root = NULL;
23+
char character = '\0';
24+
int index = 0;
25+
26+
while (character != '$')
27+
{
28+
printf("(enter a '$' to quit entering characters) ");
29+
scanf(" %c",&character);
30+
if (character != '$')
31+
{
32+
root = malloc(sizeof(HUFFMAN));
33+
root->token = character;
34+
printf("Enter '%c's frequency: ", character);
35+
scanf("%d", &(root->frequency));
36+
++index;
37+
insertHeap(heap, root, index);
38+
}
39+
}
40+
41+
while (heap[2] != NULL)
42+
{
43+
root = malloc(sizeof(HUFFMAN));
44+
root->token = '-';
45+
root->leftChild = removeHeap(heap,index);
46+
index = index - 1;
47+
root->rightChild = removeHeap(heap,index);
48+
root->frequency = root->leftChild->frequency
49+
+ root->rightChild->frequency;
50+
insertHeap(heap,root,index);
51+
}
52+
53+
printf("The nodes of the Huffman tree in In-order are: \n");
54+
inorderRecursive(root);
55+
printf("and in Pre-order are: \n");
56+
preorderRecursive(root);
57+
return 0;
58+
}
59+
60+
void insertHeap(HUFFMAN* theHeap[SIZE], HUFFMAN* newNode, int count)
61+
{
62+
HUFFMAN* tempNode = NULL;
63+
int parent = 0;
64+
theHeap[count] = newNode;
65+
parent = count / 2;
66+
while ((parent != 0)
67+
&& (theHeap[parent]->frequency > theHeap[count]->frequency))
68+
{
69+
tempNode = theHeap[parent];
70+
theHeap[parent] = theHeap[count];
71+
theHeap[count] = tempNode;
72+
count = parent;
73+
parent = count/2;
74+
}
75+
}
76+
77+
HUFFMAN* removeHeap(HUFFMAN* Heap[SIZE], int point)
78+
{
79+
HUFFMAN* extract = NULL;
80+
HUFFMAN* extraNode = NULL;
81+
int left = 0;
82+
int right = 0;
83+
int complete = 0;
84+
extract = Heap[1];
85+
Heap[1] = Heap[point];
86+
Heap[point] = NULL;
87+
point = 1;
88+
left = 2 * point;
89+
right = 2 * point + 1;
90+
while (complete != 1)
91+
{
92+
if ((Heap[left] != NULL) && (Heap[right] != NULL))
93+
{
94+
if ((Heap[point]->frequency > Heap[right]->frequency)
95+
|| (Heap[point]->frequency > Heap[left]->frequency))
96+
{
97+
if (Heap[right]->frequency < Heap[left]->frequency)
98+
{
99+
extraNode = Heap[point];
100+
Heap[point] = Heap[right];
101+
Heap[right] = extraNode;
102+
point = right;
103+
}
104+
else
105+
{
106+
extraNode = Heap[point];
107+
Heap[point] = Heap[left];
108+
Heap[left] = extraNode;
109+
point = left;
110+
}
111+
}
112+
else
113+
{
114+
complete = 1;
115+
}
116+
}
117+
else if ((Heap[left] != NULL)
118+
&& (Heap[left]->frequency < Heap[point]->frequency))
119+
{
120+
extraNode = Heap[point];
121+
Heap[point] = Heap[left];
122+
Heap[left] = extraNode;
123+
point = left;
124+
}
125+
else if ((Heap[right] != NULL)
126+
&& (Heap[right]->frequency < Heap[point]->frequency))
127+
{
128+
extraNode = Heap[point];
129+
Heap[point] = Heap[right];
130+
Heap[right] = extraNode;
131+
point = right;
132+
}
133+
else
134+
{
135+
complete = 1;
136+
}
137+
left = 2 * point;
138+
right = 2 * point + 1;
139+
}
140+
return extract;
141+
}
142+
143+
void preorderRecursive(HUFFMAN* theRoot)
144+
{
145+
if (theRoot != NULL)
146+
{
147+
printf("%c %d\n",theRoot->token, theRoot->frequency);
148+
preorderRecursive(theRoot->leftChild);
149+
preorderRecursive(theRoot->rightChild);
150+
}
151+
}
152+
153+
void inorderRecursive(HUFFMAN* Root)
154+
{
155+
if (Root != NULL)
156+
{
157+
inorderRecursive(Root->leftChild);
158+
printf("%c %d\n", Root->token,Root->frequency);
159+
inorderRecursive(Root->rightChild);
160+
}
161+
}

0 commit comments

Comments
 (0)