Hello, i have create a human tree, the only remains is to get the codes in Huffman tree (0-Left, 1-Right)

How i can traverse the tree and for every letter-weight in tree to get the path of 0101

How my code works

1st Part - Nodes Initialisation
- Create Tree Noodes BTree* Nodes
- for(int i=0; i < MAX_ALPHABET_LETTERS; i++) Initialise Nodes with theyr Weight & Letters (Weights and Letters are defined)
- For every node i create, i add it to a sorted list

2nd Part - Building the tree
As i say, i store nodes to the sorted list, that means always the first 2 elemnts on list are the smallers (No need extra code to get smallest elements)

What i need?
I need help to get & store codes in Builded Tree

This maby will help you
- All nodes of tree are stored in global list, So, what you think about:
"descent the list (from higer to smaller) and foreach node in list check if it has Left or Right"
"If it has Right then go to the right node and add 1 to node struct"
"If it has Left then go to the left node and add 0 to node data"

The lowest nodes in tree has as code the value of his upper node + "0" or "1" (if it is left or right)

Edited 3 Years Ago by Cwww

Would you please post what you've done so far? Without being able to see the code, it would be difficult if not impossible to give you any advice.

Yes ofc
PS: I use header files but i follow C-Style coding because the lesson is "Data structures with C"

There are 5 Files
- Tree.h
- Tree.cpp
- List.h
- List.cpp
- Main.cpp

Code - Links
Tree.h
Tree.cpp

List.h
List.cpp

Main

Edited 3 Years Ago by Cwww

Im also thinking to create an stack and search for a node in tree

While i get down from root Push(0or1) when i recieve to leaf and is not the leaf we want then go back, When we go back then Pop() from stack

This is a big headpain :) Hufff

This article has been dead for over six months. Start a new discussion instead.