Sunteți pe pagina 1din 25

Binary Trees

Dr. Gurpreet Singh Lehal,


Department of Computer Science,
Punjabi University
Nature View of a Tree
leaves

branches
root
Computer Scientist’s View
root

leaves

branches
nodes
Binary Tree

A binary tree is a finite set of elements that is either


empty or is partitioned into three disjoint subsets.
 The first subset contains a single element called
the root of the tree.
 The other two subsets are themselves binary trees,
called the left and right subtrees of the original tree. A
left or right subtree can be empty.

Each element of a tree is called a node of the tree.


Parts of a binary tree
 A binary tree is composed of zero or more nodes
 Each node contains:
 A value (some sort of data item)
 A reference or pointer to a left child (may be null),
and
 A reference or pointer to a right child (may be null)
 A binary tree may be empty (contain no nodes)
 If not empty, a binary tree has a root node
 Every node in the binary tree is reachable from the
root node by a unique path
 A node with neither a left child nor a right child is
called a leaf
 In some binary trees, only the leaves contain a
value
A Picture of a binary
B C tree
D E F

G H

Abstract Tree Model


left A right

left B right left C right

left D right left E right left F right

left G right left H right

Tree Node Model


A is the root of the tree.
A is the parent of B and C.
A is an ancestor of all nodes.

B C

D E F G

N O
H I J K L M
B and C are siblings.
J is a descendent of B.

B C

D E F G

N O
H I J K L M
Binary Tree
Structures that are not binary trees

B c

E F
D

I
G H
Binary Tree
Structures that are not binary trees

B c

E F
D

G
Binary Tree
Structures that are not binary trees

B c

E F
D

H I
Size and depth
a  The size of a binary tree is the
number of nodes in it
 This tree has size 12
b c
 The depth of a node is its
distance from the root
d e f  a is at depth zero
 e is at depth 2
g h i j k  The height of a binary tree is
the depth of its deepest node
l  This tree has height 4
What is the max #nodes
at some level/depth l?

The max #nodes at level l is 2l where l=0,1,2, ...,L-1

 20
 21
 22
 23
Height of Binary Tree
 Maximum height of tree for N nodes:

Hmax = N - 1

 The minimum height of the tree :

• Hmin = log2N

 If known the height of a tree:

• Nmin = H + 1

• Nmax = 2H+1 - 1
Strictly Binary Tree
Strictly binary trees:
If every nonleaf node in a binary tree has nonempty left
and right subtrees, the tree is called a strictly binary tree.
A strictly binary tree with n leaves always contains
2n -1 nodes.
A

B C

D E

F G
Strictly Binary tree
Structure that is not a strictly binary tree:
because nodes C and E have one son each.

A
B
c
E
D
F

G
H I
A complete binary tree
Complete binary tree of height d is the strictly binary tree all
of whose leaves are at level d.
The total number of nodes = the sum of the number of nodes
at each level between 0 and d = 2 d + 1 - 1

A
B
C
D E F G

H I L M
J K N O
Almost complete binary tree
A binary tree of depth d is an almost complete binary tree if:
- 1. A node at level less than d -1 has two sons
- 2. For any node in the tree with a right descendant at level d, must have
a left son and every left descendant is either a leaf at level d or has
two sons. The strictly binary tree
is not almost complete,
A
since A has a right descendant at
B level 3 (J) but also has a left
C descendant that is a leaf at level
D E 2 (E)
F G
Violates condition 2
H I J K
Satisfies the condition1, since every
leaf node is either at level 2 or at
level 3.
Almost complete binary tree

A The strictly binary tree


is not almost complete,
B C
since it contains leaves
at levels 1, 2, and 3.
D E
Violates condition 1
F G
Almost complete binary tree

The binary tree


1 is almost complete,
A
2 Satisfies the condition1, since every
B 3 leaf node is either at level 2 or at
C level 3.
4 5 6 7
D E
F G Satisfies the condition 2
8 9
H I F 10 However, the binary tree is not
strictly binary tree, since node E has
a left son but not a right son
Linked Representation
struct node
{
int data;
struct node *left, *right;
};
typedef struct node *TREE;
data

left data right


left right
A few programming
assignments
Write down functions to :
 Count number of nodes in a binary tree

 Find the height of a binary tree

 Count number of leaves in a binary tree

 Count number of non-leaves in a binary tree


Count Number of nodes
int CountNodes(TREE tree)
{
if (tree == NULL)
return 0;
else
return CountNodes(tree->left) +
CountNodes(tree->right) + 1;
}
Get Height of Binary Tree
int height(TREE tree)
{
int lh=-1, rh=-1;
if (tree == NULL)
return -1;
lh = height(tree->left);
rh = height(tree->right);
if(lh>rh) return lh+1;
else return rh+1;
}
Count Leaves in Binary Tree
int count_leaf(TREE tree)
{
int lh=-1, rh=-1;
if (tree == NULL)
return 0;
if(tree->left == NULL && tree->right == NULL)
return 1;
return(count_leaf(tree->left) +
count_left(tree->right));
}

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