Beruflich Dokumente
Kultur Dokumente
Tree Terminology
A tree consists of a collection of elements
or nodes, with each node linked to its
successors
The node at the top of a tree is called its
root
The links from a node to its successors
are called
ll d b
branches
h
The successors of a node are called its
children
Tree Terminology(continued)
Tree Terminology(continued)
Leaf: node without any child
Level: rank of the hierarchy and root node
has level zero(0). Node at level l has the
level
e e l+1 for
o its
ts cchild
da
and
d l-1 for
o its
ts parent.
pa e t
This is true for all nodes except the root
Trees
A tree is a collection of nodes
The collection can be empty
(recursive definition) If not empty
empty, a tree
consists of a distinguished node r (the root),
and zero or more nonempty subtrees T1,
T2, ...., Tk, each of whose roots are connected
by a directed edge from r
Some Terminologies
Binary Trees
In a binary tree,
tree each node has at most
two subtrees
A set of nodes T is a binary tree if either of
the following is true
T is
i empty
t
Its root node has two subtrees, TL and TR,
such that TL and TR are binary trees
linked representation
each item stored in a node which contains:
the item
a pointer to the left subtree
a pointer to the right subtree
Array representation
Each item has a position number
root is at position 0
root's left child is at position 1
root's right child is at position 2
0
1
3
2
4
In general:
left child of i is at 2i + 1
right child of i is at 2i + 2
parent of i is at (i-1)/2
int tree[n]=
- - - - - - 0 1 2 3 4 5 6
Parent ?
2
10
11
12
13
14
15
10
11
12
9 10 11 12 13 14 15
13
14
15
info
left
right
Huffman tree
Represents Huffman codes for characters that
might appear in a text file
Huffman code uses different numbers of bits
to encode letters as opposed to ASCII or
Unicode
Examples..
Continues..
3
2
11
Full Vs Complete
General Trees
Nodes of a general tree can have any
number of subtrees
A general tree can be represented using a
bi
binary
ttree
Example
Tree Traversals
A binary tree is defined recursively: it
consists of a root, a left subtree and a right
subtree
To traverse (or walk) the binary tree is to
visit each node in the binary tree exactly
once.
Tree
T
traversals
t
l are naturally
t ll recursive.
i
Tree Traversal
Different traversing methods are as follows:
Preorder
P
d
: root,
t left,
l ft right
i ht
Inorder
: left, root, right
Postorder : left, right, root
Preorder Traversal
Algorithm:preorder(root)
g
p
(
) //root p
points to root node
1.ptr=root
2.if(ptr!=NULL)
then
{ visit(ptr);
preorder(ptr.LC);
preorder(ptr.RC);
}
Inorder Traversal
Inorder(root)
1.Ptr=root;
2 if(ptr!=NULL) then
2.if(ptr!=NULL)
{
I d ( t LC)
Inorder(ptr.LC);
visit(ptr);
Inorder(ptr.RC);
}
Postorder Traversal
Postorder(root)
1.ptr=root
2 if(ptr!=NULL)
2.if(ptr!=NULL)
{ Postorder(ptr.LC);
P t d ( t RC)
Postorder(ptr.RC);
visit(ptr);
}
5
4
9
10
6
11
12
1
3
5
4
9
10
6
11
12
8
2
6 10 12
3
27
11
22
14
30
15
20
8
2
27
11
22
6 10 12
3
14
30
4.Two
4
Two traversals are essential out of which
one should inorder,another may be
preorder or postorder
5.But we cant form a binary tree if only
preorder and postorder has given.
continues.
continues
A
In:D B H E
P BDEH
Pre:B
IFJCG
CFIJG
IFJ
FIJ
HE
EH
I
H
n6
In:n1,n2,n3,n4,n5
, , , ,
n7,n8,n9
, ,
Post:n1,n3,n5,n4,n2
n8,n7,n9
n1
n3
n3,n4,n5
n7,n8
n3,n5,n4
n8,n7
n5
n8
3
2
11
61
Heap
Implementing a Heap
Because a heap is a complete binary tree
tree,
it can be implemented efficiently using an
array instead of a linked data structure
First element for storing a reference to the
root data
Use next two elements for storing the two
children
hild
off th
the roott
Use elements with subscripts 3, 4, 5, and
6 for storing the four children of these two
nodes and so on
Continued
Continued
Huffman Trees
A Huffman tree can be implemented using
a binary tree and a PriorityQueue
A straight binary encoding of an alphabet
assigns
i
a unique
i
bi
binary number
b tto each
h
symbol in the alphabet
Unicode for example
Continued
Continued
The message go
go eagles
eagles requires 144
bits in Unicode but only 38 using Huffman
coding
Chapter Review
A tree is a recursive,
recursive nonlinear data
structure that is used to represent data
that is organized as a hierarchy
A binary tree is a collection of nodes with
three components: a reference to a data
object, a reference to a left subtree, and a
reference to a right subtree
In a binary tree used for arithmetic
expressions,
i
th
the roott node
d should
h ld store
t
the operator that is evaluated last
Continued..
Continued
A binary search tree is a tree in which the
data stored in the left subtree of every
node is less than the data stored in the
root node, and the data stored in the right
subtree is greater than the data stored in
the root node