Sunteți pe pagina 1din 13

DA-1

Himanshu Patel (16BEC0630)


Adaptive Huffman encoding:
Matlab Code:

close all
%Each node contains a character followed by its
occurancein the tree.
%If tree doesn't contain English Alphabets or spacebar,
comma, fullstop
%it's represented as '$'.

str=input('Enter the string ');%Enter the input string


[qwer,len]=size(str);%Length of string
flag=0;%variable for checking
j=1;
[t node(j)]= tree('$0'); %Define the root node of Tree
j=j+1;
data=[];%variable to store characters without repetition
code=[];%fixedcode for new character that appears in tree
e=4;
r1=10;
% % e=4; %%Use this if '\t'-27, '.'-28 ,','-29
% % r1=13;
nyt1='0';%NYT for new character not present in Tree, find
path to '$0'
nyt='';%NYT for repeated variable, find path to character
count=0;
%encode=nyt1+fixedcode For new character, not defined
in Tree
%encode=nyt For character that appeared if
it exists already in tree
encode=[];
for i=1:len %the loop run for total size of string
entered
flag=0;%considering by default the character not
present in tree
s1=str(1,i);%stores each character corresponds sto
its index
j1=j-1;
if(find(data==s1)>0)%is data in tree?
flag=1;%%if data in tree
for k=2:j1
%t.get() gets the content in each node of
tree
a=t.get(node(k));
s_d_tmp=size(a);
s_d1=s_d_tmp(1,2);
if(s1==a(1,1)) %compare each element of
tree with character
y=str2num(a(1,2:s_d1)); %if that
character is found update the no of occurance of thet
character in string
y=y+1;
val=[a(1,1),num2str(y)];
t = t.set(node(k),val);
end
end
end

s1_val=0+lower(s1)-96; %this assigns each character


its number i.e, a-1,b-2,c-3,...,z-26

%%use this if character set is


%%a-1,b-2,c-3,...,z-26,'\t'-27,'.'-28,','-29
%%also set e1=4; r1=13;

% if(s1==' ') %asssigning value to alphabets


% s1_val=27;
% elseif(s1=='.')
% s1_val=28;
% elseif(s1==',')
% s1_val=29;
% else
% s1_val=0+lower(s1)-96;
% end

if(flag==1)%%this implies the character in the loop


is repeated character present in tree, then only NYT for
that character is set for encoding
encode=[encode,nyt];%%use the NYT of previous
loop, initially NYT='0'
encode1=[];%%this variable is used to display
the encoded output after each character comes inside the
loop
encode1=[nyt];
end
if(flag==0)
if(j==2)
s_parent=node(1);
[t
node(j)]=t.addnode(s_parent,['$',num2str(0)]);
j=j+1;
val=[str(1,i),num2str(1)];
data=[data,s1];
[t node(j)]=t.addnode(s_parent,val);
j=j+1;
code=[];
if(s1_val<2*r1)
code=[code,dec2bin(s1_val-1,e+1)];
encode=[encode,nyt1,code];
encode1=[];
encode1=[nyt1,code];
else
code=[code,dec2bin(s1_val-r1-1,e)];
encode=[encode,nyt1,code];
encode1=[];
encode1=[nyt1,code];
end
else
s_siblings=t.getsiblings((node(j-
2)));%siblings of the leaves of tree
s_siblings1=s_siblings(1,1);
s_sib_data1=t.get(s_siblings1);
s_siblings2=s_siblings(1,2);
if(s_sib_data1(1,2)=='0')%add next new
character to the one having '$0' as its value
[t
node(j)]=t.addnode(s_siblings1,['$',num2str(0)]);%first
add new nyt node
j=j+1;
val=[str(1,i),num2str(1)];
data=[data,s1];
[t node(j)]=t.addnode(s_siblings1,val);
%add new node with vharacter and its occurance as 1
j=j+1;
code=[];
if(s1_val<2*r1)%%if k<2*r fixed code="k-1"
representation of e+1 bits.
code=[code,dec2bin(s1_val-1,e+1)];
encode=[encode,nyt1,code];%encoded
message=NYT+fixed code
encode1=[];
encode1=[nyt1,code];
else
code=[code,dec2bin(s1_val-r1-1,e)];%%if
k>2*r "k-r-1" representation of e bits
encode=[encode,nyt1,code];
encode1=[];
encode1=[nyt1,code];
end

else
[t
node(j)]=t.addnode(s_siblings2,['$',num2str(0)]);
j=j+1;
val=[str(1,i),num2str(1)];
data=[data,s1];
[t node(j)]=t.addnode(s_siblings2,val);
j=j+1;
code=[];
if(s1_val<2*r1)
code=[code,dec2bin(s1_val-1,e+1)];
encode=[encode,nyt1,code];
encode1=[];
encode1=[nyt1,code];
else
code=[code,dec2bin(s1_val-r1-1,e)];
encode=[encode,nyt1,code];
encode1=[];
encode1=[nyt1,code];
end

end

end
end

j2=j-1;%sum update
%AFTER THE CHARACTER IS INCLUDED OR UPDATED IN THE
TREE, EACH SIBLING
%SUM VALUR IS UPDATED TO PARENT
for x=j2:-2:2
s_n=getsiblings(t,node(x));%first sibling
data1=t.get(node(s_n(1,1)));
s_d_tmp=size(data1);
s_d1=s_d_tmp(1,2);
sum1=str2num(data1(1,2:s_d1));%second sibling
data2=t.get(node(s_n(1,2)));
s_d_tmp=size(data2);
s_d1=s_d_tmp(1,2);
sum2=str2num(data2(1,2:s_d1));
sum=sum1+sum2;%sum the both sibling value
val=['$',num2str(sum)];
s_p=getparent(t,node(x));%update in the parent
node
t=t.set(node(s_p),val);
end
j3=j-1;%swap function
%IF LEFT NODE SUM IS GREATER THAN RIGHT, THEN SWAP
WHOLE BRANCH TO
%RIGHT SIDE.
for x1=2:j3
s_n=getsiblings(t,node(x1));
data1=t.get(node(s_n(1,1)));%FIRST NODE
s_d_tmp=size(data1);
s_d1=s_d_tmp(1,2);
cmp1=str2num(data1(1,2:s_d1));
data2=t.get(node(s_n(1,2)));%%SECOND NODE
s_d_tmp=size(data2);
s_d1=s_d_tmp(1,2);
cmp2=str2num(data2(1,2:s_d1));
if(cmp1>=cmp2)%compare the two sum of siblings
s_p1=getparent(t,node(s_n(1,1))); %swap the
branches of tree with their parent
nt=subtree(t,node(s_n(1,1)));%store the
branch as new tree
t=t.chop(node(s_n(1,1)));%cut that branch
t = t.graft(node(s_p1), nt);%add it to the
parent tree as a branch to parent node

end
end

j4=j-1; %Find NYT for already defined alphabet


%for an already existing variable, the NYT would be
its location from
%root node to that node where it is placed
nyt=[];
for x2=2:2:j4
s_n1=t.get(node(x2));
s_n2=t.get(node(x2+1));
if(s_n1(1,1)~='$')
if(s_n1(1,1)==s1)
nyt=[nyt,'0'];%%0a
break
else
nyt=[nyt,'1'];%%1b
end
elseif(s_n2(1,1)~='$')
if(s_n2(1,1)==s1)
nyt=[nyt,'1'];%%1c
break
else
nyt=[nyt,'0'];%%0d
end
end

end

j5=j-1; %Find NYT for new alphabet (i.e, finding 0


char)
%for a new character to be added in tree, that is
the character not
%present in tree. the new character is added to the
node which
%contains zero as sum . Therefore NYT would be path
from root node to
%node which contains zero as sum.
nyt1=[];%%code=nyt+fixedcode
for x2=2:2:j5
s_n1=t.get(node(x2));
s_n2=t.get(node(x2+1));
if(s_n1(1,1)=='$')
if(s_n1(1,2)=='0')
nyt1=[nyt1,'0'];%%0A
break
else
nyt1=[nyt1,'0'];%%0B
end
elseif(s_n2(1,1)=='$')
if(s_n2(1,2)=='0')
nyt1=[nyt1,'1'];%%1C
break
else
nyt1=[nyt1,'1'];%%1D
end
end
end
end
disp(t.tostring);
encode

Tree Code:
classdef tree
properties (SetAccess = private)
% Hold the data at each node
Node = { [] };

% Index of the parent node. The root of the tree


as a parent index
% equal to 0.
Parent = [ 0 ]; %#ok<NBRAK>

end

methods

% CONSTRUCTOR

function [obj, root_ID] = tree(content, val)

if nargin < 1
root_ID = 1;
return
end

if isa(content, 'tree')
% Copy constructor
obj.Parent = content.Parent;
if nargin > 1
if strcmpi(val, 'clear')
obj.Node =
cell(numel(obj.Parent), 1);
else
cellval = cell(numel(obj.Parent),
1);
for i = 1 : numel(obj.Parent)
cellval{i} = val;
end
obj.Node = cellval;
end
else
obj.Node = content.Node;
end
else
% New object with only root content

obj.Node = { content };
root_ID = 1;
end

end

% METHODS

function [obj, ID] = addnode(obj, parent, data)

if parent < 0 || parent > numel(obj.Parent)


error('MATLAB:tree:addnode', ...
'Cannot add to unknown parent with
index %d.\n', parent)
end

if parent == 0
% Replace the whole tree by overiding the
root.
obj.Node = { data };
obj.Parent = 0;
ID = 1;
return
end

% Expand the cell by


obj.Node{ end + 1, 1 } = data;

obj.Parent = [
obj.Parent
parent ];

ID = numel(obj.Node);

end

function flag = isleaf(obj, ID)


if ID < 1 || ID > numel(obj.Parent)
error('MATLAB:tree:isleaf', ...
'No node with ID %d.', ID)
end

parent = obj.Parent;
flag = ~any( parent == ID );
end

function IDs = findleaves(obj)


%% FINDLEAVES Return the IDs of all the
leaves of the tree.
parents = obj.Parent;
IDs = (1 : numel(parents)); % All IDs
IDs = setdiff(IDs, parents); % Remove those
which are marked as parent

end

function content = get(obj, ID)


%% GET Return the content of the given node
ID.
content = obj.Node{ID};
end

function obj = set(obj, ID, content)


%% SET Set the content of given node ID and
return the modifed tree.
obj.Node{ID} = content;
end

function IDs = getchildren(obj, ID)


%% GETCHILDREN Return the list of ID of the
children of the given node ID.
% The list is returned as a line vector.
parent = obj.Parent;
IDs = find( parent == ID );
IDs = IDs';
end

function ID = getparent(obj, ID)


%% GETPARENT Return the ID of the parent of the
given node.
if ID < 1 || ID > numel(obj.Parent)
error('MATLAB:tree:getparent', ...
'No node with ID %d.', ID)
end
ID = obj.Parent(ID);
end

function IDs = getsiblings(obj, ID)


%% GETSIBLINGS Return the list of ID of the
sliblings of the
% given node ID, including itself.
% The list is returned as a column vector.
if ID < 1 || ID > numel(obj.Parent)
error('MATLAB:tree:getsiblings', ...
'No node with ID %d.', ID)
end

if ID == 1 % Special case: the root


IDs = 1;
return
end

parent = obj.Parent(ID);
IDs = obj.getchildren(parent);
end

function n = nnodes(obj)
%% NNODES Return the number of nodes in the
tree.
n = numel(obj.Parent);
end

end

% STATIC METHODS

methods (Static)

hl = decorateplots(ha)

function [lineage, duration] = example

lineage_AB = tree('AB');
[lineage_AB, id_ABa] = lineage_AB.addnode(1,
'AB.a');
[lineage_AB, id_ABp] = lineage_AB.addnode(1,
'AB.p');

[lineage_AB, id_ABal] =
lineage_AB.addnode(id_ABa, 'AB.al');
[lineage_AB, id_ABar] =
lineage_AB.addnode(id_ABa, 'AB.ar');
[lineage_AB, id_ABala] =
lineage_AB.addnode(id_ABal, 'AB.ala');
[lineage_AB, id_ABalp] =
lineage_AB.addnode(id_ABal, 'AB.alp');
[lineage_AB, id_ABara] =
lineage_AB.addnode(id_ABar, 'AB.ara');
[lineage_AB, id_ABarp] =
lineage_AB.addnode(id_ABar, 'AB.arp');

[lineage_AB, id_ABpl] =
lineage_AB.addnode(id_ABp, 'AB.pl');
[lineage_AB, id_ABpr] =
lineage_AB.addnode(id_ABp, 'AB.pr');
[lineage_AB, id_ABpla] =
lineage_AB.addnode(id_ABpl, 'AB.pla');
[lineage_AB, id_ABplp] =
lineage_AB.addnode(id_ABpl, 'AB.plp');
[lineage_AB, id_ABpra] =
lineage_AB.addnode(id_ABpr, 'AB.pra');
[lineage_AB, id_ABprp] =
lineage_AB.addnode(id_ABpr, 'AB.prp');

lineage_P1 = tree('P1');
[lineage_P1, id_P2] = lineage_P1.addnode(1,
'P2');
[lineage_P1, id_EMS] = lineage_P1.addnode(1,
'EMS');
[lineage_P1, id_P3] =
lineage_P1.addnode(id_P2, 'P3');

[lineage_P1, id_C] =
lineage_P1.addnode(id_P2, 'C');
[lineage_P1, id_Ca] =
lineage_P1.addnode(id_C, 'C.a');
[lineage_P1, id_Caa] =
lineage_P1.addnode(id_Ca, 'C.aa');
[lineage_P1, id_Cap] =
lineage_P1.addnode(id_Ca, 'C.ap');
[lineage_P1, id_Cp] =
lineage_P1.addnode(id_C, 'C.p');
[lineage_P1, id_Cpa] =
lineage_P1.addnode(id_Cp, 'C.pa');
[lineage_P1, id_Cpp] =
lineage_P1.addnode(id_Cp, 'C.pp');

[lineage_P1, id_MS] =
lineage_P1.addnode(id_EMS, 'MS');
[lineage_P1, id_MSa] =
lineage_P1.addnode(id_MS, 'MS.a');
[lineage_P1, id_MSp] =
lineage_P1.addnode(id_MS, 'MS.p');
[lineage_P1, id_E] =
lineage_P1.addnode(id_EMS, 'E');
[lineage_P1, id_Ea] =
lineage_P1.addnode(id_E, 'E.a');
[lineage_P1, id_Eal] =
lineage_P1.addnode(id_Ea, 'E.al'); %#ok<*NASGU>
[lineage_P1, id_Ear] =
lineage_P1.addnode(id_Ea, 'E.ar');
[lineage_P1, id_Ep] =
lineage_P1.addnode(id_E, 'E.p');
[lineage_P1, id_Epl] =
lineage_P1.addnode(id_Ep, 'E.pl');
[lineage_P1, id_Epr] =
lineage_P1.addnode(id_Ep, 'E.pr');

[lineage_P1, id_P4] =
lineage_P1.addnode(id_P3, 'P4');
[lineage_P1, id_Z2] =
lineage_P1.addnode(id_P4, 'Z2');
[lineage_P1, id_Z3] =
lineage_P1.addnode(id_P4, 'Z3');

[lineage_P1, id_D] =
lineage_P1.addnode(id_P3, 'D');

lineage = tree('Zygote');
lineage = lineage.graft(1, lineage_AB);
lineage = lineage.graft(1, lineage_P1);

duration = tree(lineage, 'clear');


iterator = duration.depthfirstiterator;
for i = iterator
duration = duration.set(i,
round(20*rand));
end

end

end

end

S-ar putea să vă placă și