Lecture 12
Lecture 12
LECTURE 12
2024 - 2025
Tree
Binary tree
Huffman encoding
w e r h space .
2 5 2 1 2 1
Start with trees containing only a root node, one for every
character. Each tree has a weight, which is the frequency of
the character.
Get the two trees with the least weight (if there is a tie,
choose randomly), combine them into one tree which has as
weight the sum of the two weights.
Start parsing the code and iterate through the tree in the
following way:
Start from the root
If the current bit from the code is 0 go to the left child,
otherwise go to the right child
If we are at a leaf node we have decoded a character and have
to start over from the root
The decoded message (quotation marks added to see the
spaces at the beginning): ” wewereerww”
BSTNode:
info: TElem
left: ↑ BSTNode
right: ↑ BSTNode
BinarySearchTree:
root: ↑ BSTNode
How can we search for element 15? And for element 14?
Most BST operations will follow one single path from the root
to a leaf node (or maybe stop somewhere at an internal node,
depending on the problem), which means that their
complexity depends on the height of the tree, h.
Complexity:
Complexity:
Complexity: O(n)
Given a node, how can we find the node containing the next
value (considering the relation used for ordering the
elements)?
Given a node, how can we find the node containing the next
value (considering the relation used for ordering the
elements)?
Given a node, how can we find the node containing the next
value (considering the relation used for ordering the
elements)?
How can we find the next after 11? After 13? After 17?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
BST - Finding the successor of a node
Complexity of successor:
How can we remove the value 25? And value 2? And value
11?
How would you count how many times the value 5 is in the
tree?
How would you count how many times the value 5 is in the
tree?
How would you count how many times the value 5 is in the
tree?
How would you count now how many times the value 5 is in
the tree now?
Huffman encoding