Sie sind auf Seite 1von 82

Trees

Linear data Structures


Here are some of the data structures we
have studied so far:
Arrays
Stacks, Queues and deques
Singly linked list and doubly linked list

These all have the property that their


elements
l
t can be
b adequately
d
t l di
displayed
l
d iin a
straight line.
Binary trees are one of the simplest
nonlinear data structures.

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)


Each node in a tree has exactly one parent
except for the root node
node, which has no
parent
Nodes
N d th
thatt h
have th
the same parentt are
siblings
A node that has no children is called a leaf
node
A generalization of the parent-child
p is the ancestor-descendent
relationship
relationship

Tree Terminology (continued)


The predecessor of a node is called its
parent
A subtree of a node is a tree whose root
i a child
is
hild off th
thatt node
d
The level of a node is a measure of its
distance from the root

Tree Terminology(continued)

Mathematically a tree is defined as:


A set of one or more nodes such that
(i) There is a specially designated node
called root
(ii) Remaining nodes are partitioned into n
( 0) di
(n>0)
disjoint
j i t sets
t T1..T
Tn, where
h
each
h Ti
is a tree and they are called subtrees of
the root.

Tree Terminology (continued)


Node: stores the actual data and links to
other nodes
Parent: immediate predecessor of a node
Root: specially designated node which has
no parentt
Child:immediate successor of a node.

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

Tree Terminology (continued)


Height (depth): Maximum number of
nodes possible in a path starting from root
node to leaf node.
node Height of a tree given
by h=lmax, lmax is the maximum level of the
tree.
tree
Degree of node maximum number of
children possible for a node
Siblingsnodes having the same parent

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

Example: UNIX Directory

Example: Expression Trees

Leaves are operands (constants or variables)


The internal nodes contain operators
Will not be a binary tree if some operators are not
binary

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

Parts of a binary tree


A binary tree is composed of zero or more
nodes.
Each node can have at most two children
children.
Each node contains:
A value (some sort of data item)
A reference or pointer to a left child (may be
null)
A reference or pointer to a right child (may be
null)
ll)

Parts of a Binary tree


A binary tree may be empty (contain no
nodes)
If not empty
empty, a binary tree has a root node
Every node in the binary tree is reachable
from the root node by a unique path
path.

A node with neither a left child nor a right


child
hild iis called
ll d a lleaff ((external
t
l node).
d )

Picture of a Binary Tree


a

Properties of Binary Trees


Level
0

Properties of Binary trees


Let
T - binary tree
n - number of nodes
ne number of external nodes
ni number of internal nodes
h height of T

Representing a binary tree


have to store items and relationships (predecessor
and successors information)
array representation
each item has a position number (0 .. n-1)
use the position number as an array index
O(1) access to parent and children of item at position i
wastes space unless binary tree is complete

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

works well if n is known in advance and there are no "missing"


missing nodes

A Vector based structure for binary trees


A Simple structure for representing a
binary tree T is based on a way of
numbering the nodes of T
T. For every node
v of T, let p(v) be the integer defined as
follows:
If v is the root of T, then p(v) = 1
If v is the left child of node u
u, then p(v) =
2p(u).
If v is the right child of node u
u, then p(v) =
2p(u) + 1.

A Vector based structure for Binary Trees


1

What if parent index


=0

Parent ?
2

10

11

12

13

14

15

A Vector based structure for Binary Trees


1

10

11

12

9 10 11 12 13 14 15

13

14

15

A linked structure for Binary


Trees
typedef struct btree{
int info;
struct
t t btree
bt
*left;
*l ft
struct btree *right;
}btree;

info
left

right

A linked structure for Binary trees


root
A

Some Types of Binary Trees


Expression tree
Each node contains an operator or an
operand

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..

Binary search trees


All elements in the left sub-tree precede those
in the right sub
sub-tree
tree
7
13

3
2

11

Properties of Binary trees


Full binary tree: contains maximum
possible number of nodes in all levels
Complete binary tree: if all its level
except
p the last level have the
maximum number of possible nodes
and all the nodes in the last level
appear as far as left as possible.

Fullness and Completeness


Trees grow from the top down
Each new value is inserted in a new leaf
node
A binary tree is full if every node has two
children
hild
exceptt for
f the
th lleaves

Full Vs Complete

Properties of Binary trees


In any binary tree maximum number of
nodes on level l is 2l , where l>=0
Maximum number of nodes possible in a
binary tree of height h is 2h 1
Minimum
Mi i
number
b off nodes
d possible
ibl iin a
binary tree of height h is h

Properties of Binary trees


For any non-empty
non empty binary tree n = e+1
e+1,
where n and e are number of nodes and
edges respectively

In any non-empty binary tree T, if n0


i th
is
the number
b off lleaff
nodes(degree=0) and n2 is number of
internal nodes (degree=2), then
n0=n2+1

Properties of Binary trees


Height of a complete binary tree with
n number of nodes is
log2(n+1)
Total number of binary tree possible
with n nodes is [1/(n+1)] 2nCn

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

Tree Traversals (continued)


Preorder: Visit root node,
node traverse TL,
TL
traverse TR
Inorder: Traverse TL
TL, visit root node
node,
traverse TR
Postorder:
P t d T
Traverse TL
TL, T
Traverse TR
TR, visit
i it
root node

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);
}

Illustrations for Traversals


Assume: visiting a node
is printing its label
1
3

5
4

9
10

6
11

12

Illustrations for Traversals


Assume: visiting a node
is printing its label
Preorder:
1 3 5 4 6 7 8 9 10 11 12
Inorder:
I d
4 5 6 3 1 8 7 9 11 10 12
Postorder:
4 6 5 3 8 11 12 10 9 7 1

1
3

5
4

9
10

6
11

12

Illustrations for Traversals (Contd.)


Assume: visiting a node
is printing its data
15
20

8
2

6 10 12
3

27

11

22
14

30

Illustrations for Traversals (Contd.)


Assume: visiting a node
is printing its data
Preorder: 15 8 2 6 3 7
11 10 12 14 20 27 22 30
Inorder:
I d 2 3 6 7 8 10 11
12 14 15 20 22 27 30
Postorder: 3 7 6 2 10 14
12 11 8 22 30 27 20 15

15
20

8
2

27

11

22

6 10 12
3

14

30

Formulation of Binary tree from


I traversall
Its
1.If preorder is given
given=>First
First node is the root
If postorder is given=>Last node is the root
2.Once the root node is identified ,all nodes
in the left subtrees and right subtrees of
the root node can be identified.
3.Same technique can be applied repeatedly
to form subtrees

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.

Example: For Given Inorder and


Preorder
Inorder:D B H E A I F J F C G
Preorder:A B D E H C F I J G
Now root is A
Left subtree:D B H E
Right subtree:I F J C G

continues.
continues
A
In:D B H E
P BDEH
Pre:B

IFJCG
CFIJG

IFJ
FIJ

HE
EH
I
H

Example: For Given Inorder and


Postorder
Inorder: n1,n2, n3, n4, n5, n6, n7, n8,
n9
Postorder: n1,n3, n5, n4, n2, n8, n7,
n9,
9 n6
6

So here n6 is the root

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

Binary Search Trees


Let S be a set whose elements have an
order relation. For example, S could be a
set of integers
integers. A binary search tree for S
is a proper binary tree such that:
Each internal node v of T, stores an
element of S,
S denoted by x(v).
x(v)

For each internal node v of T, the elements


stored in the left subtree of v are less than or
equal to x(v) and the elements stored in the
right subtree of v are greater than or equal to
x(v).
The external nodes of T do not store any
element.

Binary Search Tree(continued)


For each internal node v of T
T, the
elements stored in the left subtree of v are
less than or equal to x(v) and the
elements stored in the right subtree of v
are greater than or equal to x(v).
x(v)
The
Th external
t
l nodes
d off T do
d nott store
t
any
element.

Overview of a Binary Search


Tree
Binary search tree definition
A set of nodes T is a binary search tree
if either of the following is true
T is empty
Its root has two subtrees such that
each is a binary search tree and the
value in the root is greater than all
values of the left subtree but less than
g subtree
all values in the right

Binary Search Tree


7
13

3
2

11

An inorder traversal of the internal nodes of a binary


search tree T visits the elements in nondecreasing
order.

Binary Search Tree

Algorithm for search operation


for BST
Bstnode search(int x, Bstnode t)
{
if(t==NULL)
return NULL;
if(x<t->info)
return
t
search(x,t->left);
h( t >l ft)
else if(x>t->info)
return search(x
search(x,tt->right);
>right);
else
return t;
}
8-Apr-09

61

Searching a Binary Tree

Insertion into a Binary Search


Tree

Removing from a Binary Search


Tree

Removing from a Binary Search


Tree (continued)

Heaps and Priority Queues


In a heap
heap, the value in a node is les than
all values in its two sub trees
A heap is a complete binary tree with the
following properties
The
Th value
l in
i the
th roott is
i the
th smallest
ll t ititem iin
the tree
Every sub tree is a heap

Heap

Removing an Item from a 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

Inserting into a Heap


Implemented as an ArrayList

Inserting into a Heap


Implemented as an ArrayList
(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

Huffman Trees (continued)

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

Chapter Review (continued)


A heap is a complete binary tree in which
the data in each node is less than the data
in both its subtrees
Insertion and removal in a heap are both
O(log n)
A Huffman tree is a binary tree used to
store
t
a code
d th
thatt ffacilitates
ilit t fil
file
compression

Das könnte Ihnen auch gefallen