Sei sulla pagina 1di 13

Tree

Definition:
A tree is a finite set of nodes with one specially designated node
called the root and the remaining nodes are partitioned into n>=0
disjoint sets T1, ..., Tn, where each of these sets is a tree. We call
T1, ..., Tn the subtrees of the root.

B C D

E F G

Fig. Tree
Null Tree:A tree with no nodes is a NULL Tree.It is also called empty tree.

Node:A node of a tree is an item of information along with the branches to other
nodes.E.g. above tree has 7 nodes.

Leaf Node:
A leaf node is a terminal node of a tree .It does not have any nodes connected to
it.All other nodes are called non leaf nodes or internal nodes.e.g. E,F and G are leaf
nodes.

Degree of node:
The number of subtrees of a node is called its degree.The degree of A is 3,
B is 2,D is 1 and C is 0.
Degree Level
3 A 1

2 2
B C D

0 E F 3
G
Degree of the tree: The degree of the tree is the maximum degree of the nodes in the
tree. Example: The degree of above tree is 3.

Parent node:A parent node is a node having other nodes connected to it.These nodes
are called the children of the node. The root is the parent of all its subtrees.
Example:A,B,D are parent nodes.

Siblings: Children of the same parent are called siblings.


Example: E and F are siblings.

Descendents : The descendents of a node are all those nodes which are reachable from
that node. Example : E and F are descendents of B.

Ancestor: The ancestors of node are all the nodes along the path from the root to that
node. Example: B and A are ancestors of E.

Level of node:This indicates the position of node in the hierarchy.The level of any
node=level of parent+1.The root is considered to be at level 1.
example: B,C level 2 and E ,F level is 3.

Height or depth of tree:The maximum level of any node in the tree is the height or
depth of the tree.
Example:Tree has height 3.This equals the length of the longest path from the root to
any leaf.
Forest: A forest is a set of n>=0 disjoint trees i.e. if we remove the root,we
get a forest of trees.
Example: A

B C D

E F G

D
B
C

E G
F
(i) (ii) (iii)
These three trees form a forest if A is removed.
Binary Trees : A binary tree is a finite set of nodes that is either empty or consists of a
root and two disjoint binary trees called the left subtree and the right subtree.It is a tree
where every node can have at most two branches(children).

B C

D E F
A

Strictly Binary Tree: A strictly binary tree is a binary tree


where all non leaf nodes have two branches. B C

D E

F G
Complete Binary Tree:A complete binary tree is a strictly A
binary tree with all its leaf nodes at the same level,
d (d is the height or depth of the tree).
In a complete binary tree all the levels are full. B C

The maximum number of nodes on


D E
level i is 2i. F G
The complete binary tree with a total of d levels
(from 0 to d-1) contains 2d-1 nodes.
A
Almost complete binary tree: A binary tree with
d levels is almost complete if
levels 1 to d-1 are full and B C
the last level i.e. level d is partially
filled from left to right.
D E F G

H I J
Skewed Binary tree:A left skewed binary tree has all nodes only in left subtrees and
the right subtrees are empty.Similarly,a right skewed binary tree has all nodes only in
the right subtrees.

A
A

B
B

C
C

D Left Skewed BT Right Skewed BT


D

Binary search tree: A binary search tree is a binary tree in which the nodes are
arranged according their values.The left node has a value less than its parent and the
right node has value greater than or equal to the parent node i.e. all nodes in the left
subtree have values less than the root and those in the right subtree have values
greater than the root.
Fig .Binary search Tree 20

10 40

8 15 35 50

5 9 16

Expression tree: It is an application of a binary tree .An expression tree can be


used to represent a binary expression containing operands and
binary operators.The tree will be strictly binary tree.
The root contains the operator to be applied +
to the result obtain by evaluating the left
and right subtrees. The operands are * c
in the leaf nodes while the operators are in
the internal nodes.
a b
Binary Expression treee for a*b+c
Binary expression tree for (a+b)*(c+d-e)

+ -

a b + e

c d

Traversing the tree in preorder and postorder gives the prefix and postfix forms
resp. of the expression.
Applications of trees:

1.Representing hierarchical information like directory structure of computer.


2.Representing dictionaries and symbol table.
3.In database to store key values.
4.Game tress.
5. Heap sort.
6.Expression and representation.
7 Decision making (Decision trees).

Representation of Binary trees:


Two ways 1. static and 2. dynamic.

1.Static representation: In this method we use an array to store the nodes of the tree.
The elements are stored in the array level-wise starting from the root. A binary tree
with d levels will have maximum of 2d-1 nodes, we can use an array of size 2d-1 to
represent binary tree.

Example:If tree has height 3 ,the maximum number of elements =23-1=7


Binary tree representation:

If a complete binary tree with n nodes (depth =


log n + 1) is represented sequentially, then for
any node with index i, 1<=i<=n, we have:
parent(i) is at i/2 if i!=1. If i=1, i is at the root and
has no parent.
left_child(i) is at 2i if 2i<=n. If 2i>n, then i has no
left child.
right_child(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n,
then i has no right child.
0 Level 0
A
1 2
Level 1
B C
3 5 6
4
Level 2
D E F G

0 1 2 3 4 5 6
A B C D E F G

NOTE: the above formula will work only for almost complete binary tree.Any
binary tree can be converted to almost complete binary tree by showing dummy
nodes.
Example:If tree has height 4 ,the maximum number of elements =24-1=15

Level 0 A

Level 1
B

Level 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
C
A B C D

Level 3 D

Potrebbero piacerti anche