Tree Unit-5
Tree Unit-5
Tree Terminology
path A path is a sequence of edges between nodes.
The root is the special node from which all other nodes "descend".
root Each tree has a single root node.
The parent of node n is the unique node with an edge to node n and
which is the first node on the path from n to the root.
Note:
The root is the only node with no parent.
parent of node n Except for the root, each node has one parent.
A child of node n is a node for which node n is the next node on the
path to the root node.
More formally, if the last edge on the path from root r of tree T to
node x is (y, x) then node y is the parent of node x and node x is
a child of node y.
Note:
child of node n Each node may have 0 or more children.
siblings Nodes with the same parent are siblings.
Any node y on the (unique) path from root r to node n is an ancestor of
node n.
ancestor of node n Every node is an ancestor of itself.
A proper ancestor of n is any node y such that node y is an ancestor of
proper ancestor of n node n and y is not the same node as n.
Any node y for which n is an ancestor of y.
descendent of n Every node is an descendent of itself.
A proper descendent of n is any node y for which n is an ancestor
proper descendent of n of y and y is not the same node as n.
subtrees of node n Subtrees of node n are the trees whose roots are the children of n.
degree of node n The degree of node n is the number of children node n has.
leaf node A leaf node is a node with no children.
external node An external node is a node with no children (same as a leaf node).
internal node An internal node is a nonleaf node.
depth of node n The depth of node n is the length of the path from root to node n.
level d Level d is the nodes at depth d.
The height of tree T is the largest depth of any node in T.
We usually think of height relating to trees and depth relating to nodes
height of tree T however for full trees the terms are sometimes used interchangably.
Binary Tree
Binary Tree is a special type of generic tree in which, each node can have at most two
children. Binary tree is generally partitioned into three disjoint subsets.
1 Pre-order Traverse the root first then traverse into the left sub-tree and right
Traversal sub-tree respectively. This procedure will be applied to each sub-
tree of the tree recursively.
2 In-order Traverse the left sub-tree first, and then traverse the root and the
Traversal right sub-tree respectively. This procedure will be applied to each
sub-tree of the tree recursively.
3 Post-order Traverse the left sub-tree and then traverse the right sub-tree and
Traversal root respectively. This procedure will be applied to each sub-tree of
the tree recursively.
1. Linked Representation
In this representation, the binary tree is stored in the memory, in the form of a linked list
where the number of nodes are stored at non-contiguous memory locations and linked
together by inheriting parent child relationship like a tree. every node contains three parts :
pointer to the left node, data element and pointer to the right node. Each binary tree has a
root pointer which points to the root node of the binary tree. In an empty binary tree, the
root pointer will point to null.
In the above figure, a tree is seen as the collection of nodes where each node contains three
parts : left pointer, data element and right pointer. Left pointer stores the address of the
left child while the right pointer stores the address of the right child. The leaf node
contains null in its left and right pointers.
The following image shows about how the memory will be allocated for the binary tree by
using linked representation. There is a special pointer maintained in the memory which
points to the root node of the tree. Every node in the tree contains the address of its left
and right child. Leaf node contains null in its left and right pointers.
2. Sequential Representation
This is the simplest memory allocation technique to store the tree elements but it is an
inefficient technique since it requires a lot of space to store the tree elements. A binary tree
is shown in the following figure along with its memory allocation.
In this representation, an array is used to store the tree elements. Size of the array will be
equal to the number of nodes present in the tree. The root node of the tree will be present
at the 1st index of the array. If a node is stored at ith index then its left and right children
will be stored at 2i and 2i+1 location. If the 1st index of the array i.e. tree[1] is 0, it means
that the tree is empty.
Definition
Huffman coding provides codes to characters such that the length of the code depends on the
relative frequency or weight of the corresponding character. Huffman codes are of variable-
length, and without any prefix (that means no code is a prefix of any other). Any prefix-free
binary code can be displayed or visualized as a binary tree with the encoded characters stored at
the leaves.
Huffman tree or Huffman coding tree defines as a full binary tree in which each leaf of the tree
corresponds to a letter in the given alphabet.
The Huffman tree is treated as the binary tree associated with minimum external path weight that
means, the one associated with the minimum sum of weighted path lengths for the given set of
leaves. So the goal is to construct a tree with the minimum external path weight.
An example is given below-
Frequency 2 7 24 32 37 42 42 120
Huffman code
Letter Freq Code Bits
e 120 0 1
d 42 101 3
l 42 110 3
u 37 100 3
c 32 1110 4
Letter Freq Code Bits
m 24 11111 5
k 7 111101 6
z 2 111100 6