KEMBAR78
Tree terminology and introduction to binary tree | PDF
TREES
A Non Linear Data Structure in which data items have hierarchical
relationship
Definition:
A tree can be theoretically defined as a finite set of one or more data
items (or nodes) such that :
1. There is a special node called the root of the tree.
2. Other nodes are partitioned into number of mutually exclusive (i.e.,
disjoined) subsets each of which is itself a tree, are called sub tree.
TREE Terminology
Root
A specially designed node in a tree
It is the first node in the tree
In above Fig. A is root node
Node
Each data item in a tree is called a node
It specifies the data and links to other nodes
A
B C
D E
Root
Root
Leave
Leave
Computational Real Tree
F
G
Parent Node
Child Node
A
B C
D E
Root
Leave
Computational
F
G
A
B C
D E
Root
Leave
F
G
Level 0
Level 1
Level 2
Degree of a Node
Number of sub-trees of a node
Degree of A node = 2
Degree of B node = 3
Degree of C node = 1
Degree of D node = 0
Degree of E node = 0
Degree of F node = 0
Degree of G node = 0
Degree of a Tree
The maximum degree of node in a tree.
Eg. The degree of above tree is 3
All other nodes can have <= degree of nodes
Means a node in this tree can have (0/ 1/2/3) no. of nodes
Terminal Node (Leaf node)
A node with degree 0 (no child)
Eg. G, D, E, and F are leaf nodes
Intermediate node
A node with non-zero degree
Eg. B and C nodes
Levels in Tree
A tree is structured in levels
Point to Remember
IF a node is at level n then its child will be at n+1 level
Depth/ Height of a Tree
Maximum level of any node in a given tree
Eg. Height of tree = 2
R
B C
D E
Root
F
G
T1 T2
A
B C
D
E F
G
Binary
A
B C
D
E F
G
H J
I
K
Binary Tree
BINARY TREE
Definition:
A Binary Tree is a tree in which
A node can have only(at most) 2 child nodes ( 0
child/ 1 child/ 2 childs)
The two children are called ‘Left Child’ and ‘Right
Child’ respectively.
A binary tree T is defined as a finite set of elements,
called nodes, such that-
T is empty, if T has no nodes (called empty tree/ null tree)
T contains a special node R, called Root node of T, and other nodes of
T, form an ordered pair of disjoint binary trees T1 and T2.
T1 and T2 are called Left and Right sub-trees of R.
If T1 is non empty, then its root is called Left Successor(child) of R
If T2 is non empty, then its root is called Right Successor(child) of R
Above Trees are Binary Trees. Now consider the following definition-
Strictly Binary Tree
A binary tree is said to be strictly binary tree if,
All of its non-leaf nodes in a binary tree has non empty left and
right sub-tree.
A
B C
D
E F
G
Strictly Binary Tree
A
B C
D
E F
G
H J
I
K
Binary Tree
*
+ C
B
A
Every node in the strictly binary tree can have either no children
or two children.
They are also called 2-tree or extended binary tree
Point to Remember
A strictly binary tree with n leaves always contains 2n– 1 nodes
Application of Binary Tree (2-Tree)
The main application of a 2-tree is to represent and compute any
algebraic expression using binary operation.
Consider following expression:
E = (( a + b ) * c)
E can be represented by using 2-Tree-
This is called Expression Tree. .
/
X Y
*
- e
d
c
/
X Y
+
b
a
/
+ *
b
a -
e
d
c
A
B C
D
E F
G
Complete Binary Tree
How to create Expression Tree?
Consider the given Expression-
E = a + b/c – d *e
1. Put parenthesis in the expression (if missing)
2. Find the center of the expression
E= (a + b) / ( (c – d ) *e )
X=(a + b)
Y=( (c – d ) *e )
E= X/Y
3. Make the center operator as Root node
4. Now expand X and Y in Tree
Complete Binary Tree
A complete binary tree at depth ‘d’ is the
strictly binary tree, where all the leaves are
at level d.
A
B C
D
E F
G
Complete Binary Tree
A
B
G
Binary Tree
A
B C
F
E
G
D
Complete Binary Tree
I
H K
J M
L O
N
Point to Remember
A binary tree with n nodes, n > 0, has exactly n – 1 edges
A binary tree of depth d, d > 0, has at least d and at most 2d – 1 nodes
in it.
If a binary tree contains n nodes at level L, then it can contains at
most 2n nodes at level L + 1
A complete binary tree of depth d is the binary tree of depth d
contains exactly 2 L nodes at each level L between 0 and d
A
B
G
Left Skewed Binary Tree
A
B
G
Right Skewed Binary Tree
If a binary tree has only left sub trees, then it is called left skewed
binary tree.
If a binary tree has only right sub trees, then it is called right skewed
binary tree.
A
B C
E F
G
BINARY TREE REPRESENTATION
There are two ways of representing binary tree T in memory :
1. Sequential representation using arrays
2. Linked list representation
ARRAY REPRESENTATION
1. An array can be used to store the nodes of a binary tree.
2. The nodes stored in an array of memory can be accessed
sequentially.
3. Suppose a binary tree T of depth d. Then at most
2d – 1 nodes can be there in T.
4. So an array of size 2d
-1 is required to represent
T.
Eg. Consider above binary tree of depth 3. Then 2d
-1 = 23
– 1 = 7. Then
the array Tree[7] is declared to hold the nodes.
Array representation of above tree is-
0 1 2 3 4 5 6
A B C G E F
A B C G E F
Children of A shaded
A B C G E F
Children of B shaded
A B C G E F
Children of C shaded
Point to Remember
1. The parent of a node having index n can be obtained by the
formula-
Parent = (n – 1)/2
Parent = (index of child node – 1)/2
Eg. C is located at index 2 so n=2; parent of C is located at –
(2-1)/2= ½ = 0.5 = 0 index; means it is A.
Parent of E is located at (4-1)/2 = 3/2 = 1.5 = 1; means it is B.
2. The left child of a node having index n can be obtained by the
formula-
Left child = (2n+1)
Left Child = (2 * Index of Parent) + 1
Eg. Left child of B is = 2*index of B +1 = (2*1)+1 = 2+1=3
3. The right child of a node having array index n can be obtained by
the formula-
Right child = (2n + 2)
Right Child = (2 * index of parent) + 2
Eg. Right child of C is = (2*index of C) +2 = (2*2)+2 = 6
4. If the left child is at array index n, then its right sibling is at (n +
1).
Right child = (n + 1)
Right Child = ( index of left child + 1 )
5. Similarly, if the right child is at index n, then its left sibling is at
(n – 1)
Left child = (n - 1)
Left Child = ( index of right child - 1 )

Tree terminology and introduction to binary tree

  • 1.
    TREES A Non LinearData Structure in which data items have hierarchical relationship Definition: A tree can be theoretically defined as a finite set of one or more data items (or nodes) such that : 1. There is a special node called the root of the tree. 2. Other nodes are partitioned into number of mutually exclusive (i.e., disjoined) subsets each of which is itself a tree, are called sub tree. TREE Terminology Root A specially designed node in a tree It is the first node in the tree In above Fig. A is root node Node Each data item in a tree is called a node It specifies the data and links to other nodes A B C D E Root Root Leave Leave Computational Real Tree F G Parent Node Child Node
  • 2.
    A B C D E Root Leave Computational F G A BC D E Root Leave F G Level 0 Level 1 Level 2 Degree of a Node Number of sub-trees of a node Degree of A node = 2 Degree of B node = 3 Degree of C node = 1 Degree of D node = 0 Degree of E node = 0 Degree of F node = 0 Degree of G node = 0 Degree of a Tree The maximum degree of node in a tree. Eg. The degree of above tree is 3 All other nodes can have <= degree of nodes Means a node in this tree can have (0/ 1/2/3) no. of nodes Terminal Node (Leaf node) A node with degree 0 (no child) Eg. G, D, E, and F are leaf nodes Intermediate node A node with non-zero degree Eg. B and C nodes Levels in Tree A tree is structured in levels Point to Remember IF a node is at level n then its child will be at n+1 level Depth/ Height of a Tree Maximum level of any node in a given tree
  • 3.
    Eg. Height oftree = 2
  • 4.
    R B C D E Root F G T1T2 A B C D E F G Binary A B C D E F G H J I K Binary Tree BINARY TREE Definition: A Binary Tree is a tree in which A node can have only(at most) 2 child nodes ( 0 child/ 1 child/ 2 childs) The two children are called ‘Left Child’ and ‘Right Child’ respectively. A binary tree T is defined as a finite set of elements, called nodes, such that- T is empty, if T has no nodes (called empty tree/ null tree) T contains a special node R, called Root node of T, and other nodes of T, form an ordered pair of disjoint binary trees T1 and T2. T1 and T2 are called Left and Right sub-trees of R. If T1 is non empty, then its root is called Left Successor(child) of R If T2 is non empty, then its root is called Right Successor(child) of R Above Trees are Binary Trees. Now consider the following definition- Strictly Binary Tree A binary tree is said to be strictly binary tree if, All of its non-leaf nodes in a binary tree has non empty left and right sub-tree.
  • 5.
    A B C D E F G StrictlyBinary Tree A B C D E F G H J I K Binary Tree * + C B A Every node in the strictly binary tree can have either no children or two children. They are also called 2-tree or extended binary tree Point to Remember A strictly binary tree with n leaves always contains 2n– 1 nodes Application of Binary Tree (2-Tree) The main application of a 2-tree is to represent and compute any algebraic expression using binary operation. Consider following expression: E = (( a + b ) * c) E can be represented by using 2-Tree- This is called Expression Tree. .
  • 6.
    / X Y * - e d c / XY + b a / + * b a - e d c A B C D E F G Complete Binary Tree How to create Expression Tree? Consider the given Expression- E = a + b/c – d *e 1. Put parenthesis in the expression (if missing) 2. Find the center of the expression E= (a + b) / ( (c – d ) *e ) X=(a + b) Y=( (c – d ) *e ) E= X/Y 3. Make the center operator as Root node 4. Now expand X and Y in Tree Complete Binary Tree A complete binary tree at depth ‘d’ is the strictly binary tree, where all the leaves are at level d.
  • 7.
    A B C D E F G CompleteBinary Tree A B G Binary Tree A B C F E G D Complete Binary Tree I H K J M L O N Point to Remember A binary tree with n nodes, n > 0, has exactly n – 1 edges A binary tree of depth d, d > 0, has at least d and at most 2d – 1 nodes in it. If a binary tree contains n nodes at level L, then it can contains at most 2n nodes at level L + 1 A complete binary tree of depth d is the binary tree of depth d contains exactly 2 L nodes at each level L between 0 and d
  • 8.
    A B G Left Skewed BinaryTree A B G Right Skewed Binary Tree If a binary tree has only left sub trees, then it is called left skewed binary tree. If a binary tree has only right sub trees, then it is called right skewed binary tree.
  • 9.
    A B C E F G BINARYTREE REPRESENTATION There are two ways of representing binary tree T in memory : 1. Sequential representation using arrays 2. Linked list representation ARRAY REPRESENTATION 1. An array can be used to store the nodes of a binary tree. 2. The nodes stored in an array of memory can be accessed sequentially. 3. Suppose a binary tree T of depth d. Then at most 2d – 1 nodes can be there in T. 4. So an array of size 2d -1 is required to represent T. Eg. Consider above binary tree of depth 3. Then 2d -1 = 23 – 1 = 7. Then the array Tree[7] is declared to hold the nodes. Array representation of above tree is- 0 1 2 3 4 5 6 A B C G E F A B C G E F Children of A shaded A B C G E F Children of B shaded A B C G E F Children of C shaded
  • 10.
    Point to Remember 1.The parent of a node having index n can be obtained by the formula- Parent = (n – 1)/2 Parent = (index of child node – 1)/2 Eg. C is located at index 2 so n=2; parent of C is located at – (2-1)/2= ½ = 0.5 = 0 index; means it is A. Parent of E is located at (4-1)/2 = 3/2 = 1.5 = 1; means it is B. 2. The left child of a node having index n can be obtained by the formula- Left child = (2n+1) Left Child = (2 * Index of Parent) + 1 Eg. Left child of B is = 2*index of B +1 = (2*1)+1 = 2+1=3 3. The right child of a node having array index n can be obtained by the formula- Right child = (2n + 2) Right Child = (2 * index of parent) + 2 Eg. Right child of C is = (2*index of C) +2 = (2*2)+2 = 6 4. If the left child is at array index n, then its right sibling is at (n + 1). Right child = (n + 1) Right Child = ( index of left child + 1 ) 5. Similarly, if the right child is at index n, then its left sibling is at (n – 1)
  • 11.
    Left child =(n - 1) Left Child = ( index of right child - 1 )