This document discusses trees and their representations. It defines binary trees and general trees. Binary trees have left and right child nodes, while general trees simply have ordered child nodes. Both can be represented in memory using linked lists that store node information and pointers to child and sibling nodes. The document also describes traversing trees in different orders (preorder, inorder, postorder) and threading trees to efficiently represent null pointers.
1. Chapter4
TREE
Tree:
The hierarchical representation of data is handle by using a data structure is called as
Tree. On which data is represent indifferent levels.
The first level of a tree is called as the root of the tree and if having the left and right sub
trees.
FIG : Tree
A binarytree T is definedasa finite setof elementcallednodes.Such that
1. T isemptycallednull tree or emptytree.
2. T containsdistinguishednode Rcalledrootof the tree,and remaining nodesof T froman
orderedpairof disjointbinarytree T1 & T2.
If T1 isnon emptythen itsroot is calledleftsuccessorof R.similarlyif calledasrightsuccessor
of R.
A
B C
D E G H
F J K
L
Root +
Left Right A B
a b c d a b dc
2. Above diagram representbinarytree T which consistof 11 nodesrepresentedbylettersA to
L excluding I. The root of the T isthe node A whichisat the topof the diagram.A left
downward stranded line fromunnode N indicatesthe left successor of N and rightdown
wordslanted line from N indicatesthatrightsuccessorof N.such that,
1. B isthe leftsuccessor andC isthe right successorof node A.
2. The leftsubtree of root A consist of the node B,D,E,Fand the right subtree of A consistof
the node C,G,H,J,K,L
Any node N inbinary tree T has either 0,1,2 successor unnode A,B,C& H have to
successor the nodesD,F,G,L& K have no successor. The nodeswith no successor are
calleda terminal nodes.
Binary tree T & T’ are said to be similar if they have the same shape.
Representation of binary tree incomputermemory:
Binarytree is representationin computermemory usingthree arrayas
INFO,LEFT,RIGHT.INFO isrepresentactuallynode information.Leftrepresentleftchild
address,Rightrepresentrightchildaddress.
Root isthe special pointervariablewhichisrepresentroot node firstnode addressof
binary tree if ROOT = NULL. Then binarytree isempty.
0 A 0
0 B 0
X L X
0 C 0
X D X 0 E X X G X
0 H 0
X F X
J X0 X K X
3. Root = 1
AVAL = 12
FIG : Memoryrepresentation of binary tree
Traversing of binary tree
A
B C
D E F G
a) Preorder[ Root ,Left, Right]
ABDECFG
b) In order[Left, Root,Right]
DBEAFCG
c) Postorder [Left,Right,Root]
DEBFGCA
A
B C
D E
F
a) Preorder = ABDCEF
b) In order = BDEFC
c) Postorder = DBFECA
A
B C
D E G H
F J
a) Preorder = ABDEFCGHJ
b) In order = DBEFAGCJH
c) Postorder = DFEBGJHCA
INFO LEFT RIGHT
1 A 2 3
2 B 4 5
3 C 7 8
4 D 0 0
5 E 6 0
6 F 0 0
7 G 0 0
8 H 9 10
9 J 11 0
10 K 0 0
11 L 0 0
12 13
13 14
14 0
4. Write an algorithm to traverse a binary tree inpreorder.
Algorithm:PRIORDER[ INFO, LEFT , RIGHT , ROOT]
Consider abinary tree T already exit in computer memory. This algorithm traverse tree T
inpreorder way and apply PROCESSon each element of tree T. An array STACK isused to templar
holdaddress of a node.
Step1 : Set PTR := ROOT , TOP := 1, STACK[TOP] : = NULL
Step2 : Repeat steps(3) to (5) while PTR!= NULL.
Step3 : Apply PROCESS on INFO[PTR]
Step4 : If RIGHT [PTR] ! = NULL , then;
SetTOP := TOP+ 1 , STACK[TOP] := RIGHT [PTR]
Step5 : If LEFT [PTR] != NULL , then;
Set PTR := LEFT [PTR]
Else
Set PTR := STACK[TOP] & TOP:= TOP -1.
Step 6 : Exit.
5. Inorder
Write an algorithmto traverse tree in order
Algorithm:INORDER [INFO,LEFT,RIGHT,ROOT]
Consider T isa binary tree alreadyexitin computermemory.This algorithmtraverse tree Tin
a inorder way & apply PROCESSto each node an array STACKis usedto temporary holdthe address
of nodes.
Step1 : Set PTR := ROOT ,TOP:= 1, STACK[TOP] := NULL
Step2 : Repeatfollowingstepswhile PTR!= NULL
(a) SetTOP := TOP+1, STACK[TOP] := PTR
(b) Set PTR := LEFT [PTR]
Step3 : Set PTR := STACK[TOP],TOP:= TOP-1
Step4 : Repeatsteps(5) to (7) while PTR!= NULL
Step5: Apply process on INFO[PTR]
Step6 : Check if RIGHT [PTR] != NULL , then;
(a) SetPTR := RIGHT [PTR]
(b) Go to step(2)
Step7 : SetPTR := STACK[TOP]
Step8 : Exit
Output:
A
B C
D E F
Top = 1 2 3 4
Stack = Null A B D
PTR = A B D Null
Output: D
6. Pstorder
Write algorithm to traverse tree inpostorder
Algorithm:POSTORDER[INFO,LEFT,RIGHT,ROOT]
A binarytree T is in memory.This algorithm doesapost order traverse of T, applyingan
operation PROCESStoeach of it’s nodes.An array STACK is usedto temporary holdthe address of
nodes.
Step1 : Set TOP:= 1, STACK[1] := NULL & PTR := ROOT
Step2: Repeat step(3) to (5) while PTR!= NULL
Step3 : SetTOP := TOP+1 & STACK [TOP] := PTR
Step4: If RIGHT [PTR] != NULL , then;
SetTOP := TOP+1 & STACK [TOP] := RIGHT [PTR]
Step5 : Set PTR := LEFT[PTR]
Step6 : SetPTR := STACK[TOP] &TOP := TOP-1
Step7: Repeat while PTR>0
(a) ApplyPROCESStoINFO[PTR]
(b) SetPTR := STACK[TOP] & TOP := TOP-1
Step8 : If PTR < 0,then;
(a) SetPTR := -PTR
(b) Go to step(2)
Step9: Exit.
7. Threads& In order Threading:
HEAD
Headernode
Above figure we can observe that near about half of the entries inthe pointer field LEFT
& RIGHT will contains NULL elements. This space must be efficiently used byreplacing byNULL
entriesby some othertype of information meanswe can replace certain NULL entries by special
pointers which pointsto nodeshigher inthe tree.These special pointer are called threads &
binary tree with such pointare called threadedtree.
The threads ina threaded tree must be distinguished insome way from ordinary
pointers. The threadsina diagram of a threaded tree are usually indicatedby dottedlines.Ina
computer memory an extra1-bit TAG field may be usedto distinguished threads from ordinary
pointers. Oralternatively,threads may be denoted by negative integerswhen ordinary pointers
are denoted bypositive integers.
There are many ways to thread a binary treesT, inone way threading of T, a threadwill
appearin a right field of node & will point tothe next node in the INORDER traverse of T. In two
way threading of T. threadwill appear inthe LEFT field of the node & will point tothe preceding
node inthe INORDER traversal of T.
When T doesnot have a header node then the LEFT pointer of last node will containNULL
value whenT have a header node the LEFT pointerof first node andthe RIGHT pointerof lastnode
pointto the header node .
0
0 x
0 A 0
0 B 0 0 C 0
X D X X E X X G X X H X
8. Fig (a) : One way INORDERthreading
Fig (a) : One way INORDERthreading
Fig (a) : One way INORDERthreading
A
B C
D E G H
B C
D E G H
B C
D E G H
A
A
9. ROOT INFO LEFT RIGHT
1
2
3
4
5
6
7
8
9
10
11
12
Fig: Representationof threadinginmemory
B
C
D
E
F
4
0
0
0
0
3
4
0
6
0
1 A 2 3
10. General Tree :
The general tree issetof element iscallednode.
1. T containsa distinguishedelementR,called the rootof T.
2. The remainingelement of Tfrom an orderedcollection of zero or more disjointtrees
T1,T2,……..Tm.
The trees T1,T2,…………Tm are called subtreesof the root R, and the roots of T1,T2…..Tm are
calledsuccessorof R. If N isa node with successorsS1,S2,……….Sm, then N is calledthe
parent of the Si’s the Si’s are called children of N , and the Si’sare calledsiblingsof each
other.
General tree isrootedi.e.tree orderedie. The childrenof each node N of tree have a specific
order considerageneral tree of 13 nodes.
A
B C D
E F G H J K
L M N
Fig: General Tree
Binarytree & general tree is differentobjecttwo basic are
1. BT ismay be emptybetween GT is notempty.
2. Suppose a node has only one child when then the child is differentsheet by the left
child & right child but no such different exitinaGT.
Computer Representation of GT.
11. The GT ismay be maintain in memory by linked representation which uses three
parallel array.INFO,CHILD,SIBL & pointer variable ROOT.
A
B C D
E F
Fig : General Tree
ROOT INFO CHILD SIBL
1
2
3
4
5
6
Fig: memoryrepresentation of GT
A
B
C
D
E
F
2
5
0
0
0
0
0
3
4
0
6
0
1