Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Algorithms and Data

Structures

Binary Trees

Dr. Yasir Faheem


Department of Computer Science
Islamabad campus

Lecture 11 With thanks to Dr. Salman Niazi


Overview
2

 Binary Tree
 Properties of Binary Trees
 Binary Tree Representation
 Operations on Binary Tree
 Applications
 Traversals
Binary Tree
3

 A non-empty binary tree has a root element


 The remaining elements, if any, are partitioned into
two binary trees
 These are called the left and right subtrees of the
binary tree
Differences Between A Tree & A Binary
4
Tree
 No node in a binary tree may have a degree more
than 2, whereas there is no limit on the degree of a
node in a tree.
 A binary tree may be empty
 The subtrees of a binary tree are ordered; those of
a tree are not ordered.
A
B C

D E F G
Differences Between A Tree & A Binary
5
Tree
 The subtrees of a binary tree are ordered; those of
a tree are not ordered

a a

b b

• Are different when viewed as binary trees


• Are the same when viewed as trees
Binary Trees
6

 Informal defn: each node has 0, 1, or 2 children


 Formal defn: a binary tree is a structure that
 contains no nodes, or
 is comprised of three disjoint sets of nodes: A
◼ a root B C
◼ a binary tree called its left subtree
◼ a binary tree called its right subtree D E G
 A binary tree that contains no nodes is called empty
 Note: the position of a child matters!
Some More Definitions
7

 The depth of a node in a tree is one higher that the depth


of its parent (root depth being 0)
 The depth of a tree is the maximum of the depths of the
tree's nodes

depth 0

depth 3
Binary Trees
8
 Full binary tree :
 All internal nodes have two children.
 Complete binary tree :
 A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are
as far left as possible.

 A complete binary tree of depth d has 2d-1 internal nodes and


2d leaves
What is a binary tree?
9

⚫ Property1: each node can have up to two successor


nodes (children)
⚫ The predecessor node of a node is called its parent
⚫ The "beginning" node is called the root (no parent)
⚫ A node without children is called a leaf

<=20
 21
 22
 23
What is a binary tree? (cont.)
10

 Property2: a unique path exists from the root to


every other node
 Below given example is not binary tree due to multiple
paths from A to D
Some terminology
11
 Ancestor of a node: any node on the path from the root to that
node
 Descendant of a node: any node on a path from the node to the
last node in the path
 Depth of a node: number of edges in the path from the root to
that node.
 Remember, the depth of root node is 0; the depth of root’s child nodes
is 1 and so on
 Height of a node x: In the sub-tree of tree rooted at node x, find
the leaf node y which is farthest from x; start counting nodes
upwards from y till x. Height of x is the final count.
Internal and External Nodes
12

 Leaves are called External Nodes.


 Nonleaves are called Internal Nodes.
13
14
Recursive definition of a Binary Tree
 Most of concepts related to binary trees can be explained by
recursion.
 For instance, the definition of what is a binary tree can done
recursively A
B C
D E F G

 A binary tree is: H


 An external node, or
 An internal node connected to a left binary tree and a right binary tree
(called left and right subtrees)
 In programming terms we can see that our definition for a linked
list (singly) can be modified to have two links from each node
instead of one.
Mathematical Properties of Binary
15
Trees

 Let's us look at some important mathematical properties


of binary trees
 A good understanding of these properties will help the
understanding of the performance of algorithms that
process trees
 Some of the properties we'll describe relate also the
structural properties of these trees. This is the case
because performance characteristics of many algorithms
depend on these structural properties and not only the
number of nodes.
Minimum Number Of Nodes
16
 Minimum number of nodes in a binary tree whose
height is h is also h
 At least one node at each of first h levels.

minimum number of
nodes is h
Maximum Number Of Nodes
17
 All possible nodes at first d depths are present

▪ Maximum number of nodes


= 1 + 2 + 4 + 8 + … + 2 d = 2h - 1
Number Of Nodes & Height
18

 Let n be the number of nodes in a binary tree whose


height is h.
 h <= n <= 2h – 1
 log2(n+1) <= h <= n
 The max height of a tree with N nodes is N (same as
a linked list)
 The min height of a tree with N nodes is log2(N+1)
Full Binary Tree
19

 A full binary tree of a given height h has 2h – 1


nodes

Height 4 full binary tree


Depth 3 full binary tree
Numbering Nodes in a Full Binary Tree
20

 Number the nodes 1 through 2h – 1.


 Number by levels from top to bottom.
 Within a level number from left to right.
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15
Node Number Properties
1
21

2 3

4 5 6 7

8 9 10 11 12 13 14 15
 Parent of node j is node j/2
 But node 1 is the root and has no parent
 Left child of node j is node 2*j
 But if 2*j>n, node j has no left child
 Right child of node j is node2*j+1
 But if 2*j+1>n, node j has no right child
Binary Tree Representation
22

 Array representation
 Linked representation
Array Representation
23
 Number the nodes using the numbering scheme for a full binary tree
 Store the node numbered i in tree[i]
 Index 0 of array is left empty

a1

2 3
b c

4 5 6 7
d e f g
8 9 10
h i j

tree[] a b c d e f g h i j
0 5 10
Right-Skewed Binary Tree
24

 An n node binary tree needs an array


1 whose
length is between n+1 and 2 n
24
1
a
2 3
b 3
7
c 4 6
5 7
15
d
8 9 10 11 12 13 14 15

tree[] a - b - - - c - - - - - - - d
0 5 10 15
Linked Representation
25
 Each binary tree node is represented as an
object whose data type is NodeType
 The space required by an n node binary tree is n
* (space required by one node)
Linked Representation of Binary Trees
26

 A binary tree is a tree whose nodes have at most two


offspring
 Example
class NodeType
{
int Info;
NodeType* Left;
NodeType* Right;
};
Linked Representation Example
27 root a

b c

d e

g
f
leftChild
element h
rightChild
28

The End

You might also like