Binary Trees - Tree Terminologies and representation
1. Introduction to Tree
1
So far we have discussed many linear data structures
Lecture 15 – Tree Terminologies
2. Data Structure
Unit 3: Linked List
• A non-linear data structure - Tree
• Trees are mainly used to represent data containing a hierarchical
relationship between elements, for example, records, family trees and table
of contents.
Introduction to Tree continued..
Lecture 15 – Tree Terminologies
3. Introduction to Tree continued..
3
Tree consists of a finite set of elements called nodes and a finite set of directed
lines, called edges that connect the nodes.
A tree is an abstract model of a hierarchical structure that consists of nodes with a
parent-child relationship.
There is a starting node known as a root node.
There is one and only one path between every pair of nodes in a tree.
A tree with n nodes has exactly (n-1) edges.
edge
root
node
Lecture 15 – Tree Terminologies
4. Tree Terminologies
4
root
A is parent of B and C
C is parent of D and E
D and E are children of C
Root:- The first node from where the tree originates is called as a root
node.
In any tree, there must be only one root node.
Parent node:- A node is parent node, if it has successor nodes
Child node:- A node with predecessor is called as a child node
Leaf:- A leaf node is any node with no children.
B, D and E are leaf nodes
Lecture 15 – Tree Terminologies
5. Terminologies
5
root
Leaf Node: D,I,J,F,K,H
Internal nodes:- The nodes that are not a root or a leaf are known as internal nodes.
Sibling:- two or more nodes with same parents are called as siblings.
D,E and F are Siblings
Internal Node: B,C,E,G
G and H are Siblings
B and C are Siblings
Lecture 15 – Tree Terminologies
6. Tree Terminologies contiuned..
6
e. g. A – B -– D
A – B – E – I
Path:- A path is a sequence of nodes in which each node is adjacent to the next one.
It is the sequence of consecutive edges from source node to destination node.
Lecture 15 – Tree Terminologies
7. Tree Terminologies contiuned..
7
Consider a path A- B- E- I
Here for node E , node B and A
are ancestors
Node E and I are
descendent of node B
Ancestor:- An ancestor is any node in the path from root to the node.
Descendent:- a descendent is any node in the path below the parent node. That
means all nodes in the paths from a given node to leaf are descendent of the node.
Lecture 15 – Tree Terminologies
8. Tree Terminologies contiuned..
8
Level of node A is 0
Level of node B and C is 1
Level:- The level of a node is its distance from the root node
i.e. root is at level 0, children of root at level 1 and so on.
Level of node D,E,F,G and H is 2
Level of node I , J and K is 3
Lecture 15 – Tree Terminologies
9. Tree Terminologies contiuned..
9
Degree of node A=2
Degree of node B=3
Degree: Degree of a node is equal to the number of children that a node has.
The degree of a leaf node is zero.
In-degree: In-degree of a node is the number of edges arriving at that node.
Out-degree: Out-degree of a node is the number of edges leaving that node.
Degree of node E=2
Degree of node J=0
Lecture 15 – Tree Terminologies
10. Tree Terminologies contiuned..
10
Height of node B is 2
Height of node E is 1
Height of a node: The height of a node is the max path length form that node to a leaf
node.
Height of a tree: The height of a tree is the height of the root
Height of tree = Height of
node A=3
Lecture 15 – Tree Terminologies
11. Tree Terminologies contiuned..
11
Depth of node B is 1
Depth of node E is 2
Total number of edges from root node to a particular node is called as depth of that
node.
Depth of a tree: Depth of a tree is the max level of any leaf in the tree
Depth of tree is 3
Lecture 15 – Tree Terminologies
13. Binary Tree
13
•In general, tree nodes can have any number of children.
• In a binary tree, each node can have at most two children. A binary tree is either
empty or consists of a node called the root together with two binary trees called
the left subtree and the right subtree.
•A tree with no nodes is called as a null tree.
.
Lecture 15 – Tree Terminologies
16. Types of Binary Tree
16
1. Strictly Binary Tree
If every non-leaf node in a binary tree consists of non-empty left sub-tree and
right sub-tree then such a tree is called as strictly binary tree
Lecture 15 – Tree Terminologies
17. Types of Binary Tree
17
2. Complete Binary Tree
A binary tree with n nodes and of depth d is a strictly binary tree in which all leaf
nodes are at level d is called as complete binary tree.
Lecture 15 – Tree Terminologies
18. Array Representation
Binary trees can also be stored in arrays
In this arrangement, if a node has an index i, its children are found at
indices 2i+1 and 2i+2, while its parent (if any) is found at index floor((i-
1)/2) (assuming the root of the tree stored in the array at an index
zero).
more compact storage and better locality of reference
requires contiguous memory
expensive to grow and wastes space proportional
Binary Tree Representation
Lecture 15 – Tree Terminologies
18
19. Index element
Example 1 : Array Representation
Lecture 15 – Tree Terminologies
19
0 A
1 B
2
C
3 D
4 E
5
F
6 G
7
H
8
I
Children at indices 2i+1 and 2i+2, while its
parent (if any) is found at index floor((i-1)/2)
9
10
20. Index element
Example 2: Array Representation
Lecture 15 – Tree Terminologies
20
0 A
1 B
2
-
3 C
4 -
5 -
6 -
7 D
8
Disadvantages : (1) waste space
Children at indices 2i+1 and 2i+2, while its
parent (if any) is found at index floor((i-1)/2)
-
21. Array representation is good for complete binary tree, but it is wasteful for
many other binary trees.
The representation suffers from insertion and deletion of node from the
middle of the tree.
To overcome this difficulty we represent the binary tree in linked
representation.
Limitations: Array Representation
Lecture 15 – Tree Terminologies
21
22. In linked representation each node in a binary has three fields, the left
child field denoted as LeftChild, data field denoted as data and the right
child field denoted as RightChild.
If any sub-tree is empty then the corresponding pointer’s LeftChild and
RightChild will store a NULL value.
If the tree itself is empty the root pointer will store a NULL value.
Linked Representation
Lecture 15 – Tree Terminologies
22
LeftChild Data RightChild