Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2012, 02:52 PM   PM User | #1
kungkungx3
New to the CF scene

 
Join Date: Jul 2012
Location: Toronto
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kungkungx3 is an unknown quantity at this point
Question Python encoding/decoding for huffman tree

Hi guys,
I have an assignment for python in my class on Huffman Tree encode and decoding.
I have problems creating my tree right now.
the print statemnts are just for me to see whats running in my function so ignore those. but this is my code so far
class HuffmanNode(object): #parent inheritence?

def __init__(self, left=None, right=None, root=None):
self.left = left
self.right = right
self.root = root
-----------------------------------------------^ thats my huffman node class within this huffman tree class

p = PriorityQueue()
print frequencies

index = 0
for value in frequencies:
if value != 0:
print value
print index
index = index + 1

tup = ()
for i in range(len(frequencies)):
if frequencies[i] != 0:
tup = (i, frequencies[i])
n = self.HuffmanNode(None, None, tup)
p.insert(n, frequencies[i]) #inserted node, freq value
print '----------------------------------------------------------'
# print node.root,node.left,node.right #root =2
total = 0
while not p.is_empty():
a = p.get_min()
if self._root == None:
a = self.HuffmanNode()

a2 = p.get_min()
total = a.root[1], a2.root[1]
node = self.HuffmanNode(a, a2, total)

print a.root
print a.left
print a.right

i faollowed wikis steps "The simplest construction algorithm uses a priority queue where the node with lowest probability is given highest priority:

Create a leaf node for each symbol and add it to the priority queue.
While there is more than one node in the queue:
Remove the two nodes of highest priority (lowest probability) from the queue
Create a new internal node with these two nodes as children and with probability equal to the sum of the two nodes' probabilities.
Add the new node to the queue.
The remaining node is the root node and the tree is complete.
"

I am supposed to manually remove first two nodes , then every other node only removes one by one . but i cant seem to get it to work, how do i remove the firsti two without having the whileeloop become taking two nodes every pass
i trid using an iff statement but i dont know if my iff statement condition is right.

Last edited by kungkungx3; 07-21-2012 at 02:54 PM.. Reason: forgot to include something
kungkungx3 is offline   Reply With Quote
Reply

Bookmarks

Tags
assignment, decoding, encoding, huffman, python

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:31 PM.


Advertisement
Log in to turn off these ads.