[go: up one dir, main page]

0% found this document useful (0 votes)
50 views3 pages

Data Structure 10th

Constructing expression trees using stacks involves initializing an empty stack, scanning a postfix expression from left to right, pushing operands onto the stack and creating operator nodes to combine subtrees on the stack. Huffman coding assigns variable-length binary codes to characters based on frequency, with shorter codes for more common characters, allowing for efficient data compression.

Uploaded by

ashu21doll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

Data Structure 10th

Constructing expression trees using stacks involves initializing an empty stack, scanning a postfix expression from left to right, pushing operands onto the stack and creating operator nodes to combine subtrees on the stack. Huffman coding assigns variable-length binary codes to characters based on frequency, with shorter codes for more common characters, allowing for efficient data compression.

Uploaded by

ashu21doll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Constructing expression trees using stacks:

Constructing expression trees using stacks is a common approach in computer science and is often used in the evaluation
of arithmetic expressions. Here's a step-by-step guide on how to construct an expression tree from a postfix expression
using a stack:

Definitions:

Postfix Expression: An expression in which the operands come after their operators. For example, 3 4 + is equivalent to
the infix expression 3 + 4.

Expression Tree: A binary tree representation of an arithmetic expression where the leaves are operands and internal
nodes are operators.

Algorithm:

Initialize an empty stack.

Scan the postfix expression from left to right.

For each symbol in the postfix expression:

If it is an operand, create a new node for it and push it onto the stack.

If it is an operator, pop the top two nodes from the stack, create a new node with the operator as the root, and push the
new node back onto the stack.

After scanning the entire expression, the stack will contain the root of the expression tree.

Example:

Let's consider the postfix expression: 3 4 + 5 * 2 -

Initialize an empty stack.

Scan the expression:

3: Operand, push a node with value 3 onto the stack.


4: Operand, push a node with value 4 onto the stack.

+: Operator, pop two nodes (4 and 3), create a new node with + as the root and push it onto the stack.

5: Operand, push a node with value 5 onto the stack.

*: Operator, pop two nodes (5 and the subtree with +), create a new node with * as the root and push it onto the stack.

2: Operand, push a node with value 2 onto the stack.

-: Operator, pop two nodes (2 and the subtree with *), create a new node with - as the root and push it onto the stack.

After scanning the entire expression, the stack will contain the root of the expression tree.

Huffman encoding for data compression:

Huffman coding is a popular algorithm used for data compression. It was developed by David A. Huffman in 1952. The
basic idea behind Huffman coding is to assign variable-length codes to input characters, with shorter codes assigned to
more frequent characters. This way, the most common characters are represented with shorter codes, resulting in more
efficient compression.

Here's a step-by-step explanation of how Huffman encoding works:

1. Frequency Calculation: Compute the frequency of each character in the input data.

2. Priority Queue:Create a priority queue (min-heap) based on the character frequencies. Each node in the heap
represents a character and its frequency.

3. Building Huffman Tree:

While there is more than one node in the priority queue:

Remove two nodes with the lowest frequency from the priority queue.

Create a new internal node with a frequency equal to the sum of the frequencies of the two nodes.

Add the new internal node back to the priority queue.

The last remaining node in the priority queue is the root of the Huffman tree.

4. Assigning Codes:Traverse the Huffman tree from the root to each leaf. Assign a binary code (e.g., 0 for left and 1 for
right) as you traverse.The codes for each character are the binary values along the path from the root to the corresponding
leaf.

5. Generate Huffman Codes:The binary codes assigned to each character in step 4 constitute the Huffman codes for
compression.

Example:

Let's consider the string "ABRACADABRA".

1. Frequency Calculation:

- A: 5 times
- B: 2 times

- R: 2 times

- C: 1 time

- D: 1 time

2. Priority Queue (initial):

- C(1) D(1) R(2) B(2) A(5)

4. Building Huffman Tree:

[C,1]

/ \

[D,1] [R,2]

/ \

[B,2] [A,5]

4. Assigning Codes:

C: 0

D: 10

R: 11

B: 100

A: 101

5. Generate Huffman Codes:

The Huffman codes for compression are C: 0, D: 10, R: 11, B: 100, A: 101.

So, the compressed representation of "ABRACADABRA" using Huffman coding would be


"010101100110110010010101". The codes are variable-length, and their lengths depend on the frequency of each
character in the original data. This variable-length property makes Huffman coding efficient for compressing data.

You might also like