UNIT-5 Tree
UNIT-5 Tree
UNIT-5 Tree
CHAPTER 5 11
Representation of Binary Tree
There are two primary ways to represent binary trees in memory:
• Linked Node Representation (Linked list representation)
• Array Representation (Sequential representation)
Linked Node Representation
• In linked node representation of a binary tree, structure of node
contains 3 elements-
1) Data item that is the data stored in the node
2) Address of the left child
3) Address of the right child
Array Representation
• Array Representation is useful when the tree is complete (all levels are
fully filled except possibly the last, which is filled from left to right).
In this method:
• The tree is stored in an array.
• For any node at index i:
Left Child located at index=2 * i + 1
Right Child located at index=2 * i + 2
• Root Node: Stored at index 0
• If child node index is known then parent node index is: int ((i-1)/2)
Properties of Binary Tree
• The level or height of root node is 0.
• At each level- i, the maximum number of nodes is 2 i.
• The height of the tree is defined as the longest path from the root node to the leaf
node. The maximum number of nodes at height 3 is equal to (1+2+4+8) = 15. In
general, the maximum number of nodes possible at height h is-
20 + 21 + 22+….+2h = 2h+1 -1.
• Maximum possible internal (non-leaf) nodes are= 2 h-1
• Maximum total possible external (leaf) nodes are= 2 h
• Maximum possible total nodes= internal nodes + external nodes= 2 h+2h-1=2h+1-1
• Minimum number of nodes required for tree with height h is= h+1.
Cntd…
Height of a binary Tree
• Consider N= number of nodes and h= height of a tree
If N is maximum then we can say that,
N= 2h+1 -1
N+1 = 2h+1
Taking log on both the sides,
log2(N+1) = log2(2h+1)
log2(N+1) = h+1
h= log2(N+1) -1
• In general for any value of N, h = log2(N+1) - 1
If N is minimum then we can say that,
N = h+1
h= N-1
Types of Binary Tree
• Full/proper/strict Binary tree: The full binary tree is also known as a
strict binary tree. The tree can only be considered as the full binary tree if
each node must contain either 0 or 2 children. The full binary tree can
also be defined as the tree in which each node must contain 2 children
except the leaf nodes.
• In full Binary Tree:
Number of Leaf nodes = Number of Internal nodes + 1
Cntd…
• Complete Binary tree: The complete binary tree is a tree in which all
the levels are completely filled except the last level. In the last level,
all the nodes must be as left as possible. In a complete binary tree, the
nodes are added from the left.
Cntd…
• Perfect Binary tree: A tree is a perfect binary tree if all the internal
nodes have 2 children, and all the leaf nodes are at the same level.
• Number of nodes= 2h+1-1 where h is height
Cntd…
• Degenerate (Pathological) Binary tree: The degenerate binary tree is
a tree in which all the internal nodes have only one child. Degenerate
trees are inefficient for search, insertion, and deletion operations.
Cntd…
• Skewed Binary Tree: It is a special case of a degenerate tree where all
nodes lean to one side, either left or right.
• Two Types of skewed tree:
• Left-Skewed Tree: Each node has only a left child.
• Right-Skewed Tree: Each node has only a right child.
Cntd…
• Balanced Binary Tree: It tree is a tree in which height of the left and
the right sub-trees of every node may differ by at most 1. AVL and
Red-Black trees are balanced binary tree. The given tree is a balanced
binary tree because the difference between the left subtree and right
subtree is one.
Tree Traversal
• Inorder=9, 8, 4, 2, 10, 5, 10, 1, 6, 3, 13, 12, 7
• Preorder=1, 2, 4, 8, 9, 5, 10, 10, 3, 6, 7, 12, 13
• Preorder Traversal-
• 100 , 20 , 10 , 30 , 200 , 150 , 300
• Inorder Traversal-
• 10 , 20 , 30 , 100 , 150 , 200 , 300
Tree Traversal
• inorder = {4, 8, 2, 5, 1, 6, 3, 7}
• postorder = {8, 4, 5, 2, 6, 7, 3, 1}
• Inorder : { 4, 2, 1, 7, 5, 8, 3, 6 }
Postorder : { 4, 2, 7, 8, 5, 6, 3, 1 }
Binary Search Tree (BST)
• Definition: It is the binary tree in which the value of left node must be
smaller (or equal) than the parent node, and the value of right node
must be greater than the parent node.
• This rule is applied recursively to the left and right subtrees of the
root.
• Advantages of Binary search tree
• Searching an element in the Binary search tree is easy as we always
have a hint that which subtree has the desired element.
• As compared to array and linked lists, insertion and deletion
operations are faster in BST.
Operations on BST
• Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
• Create binary search tree from above elements.
• Operations to be performed on binary search tree:
1) Create
2) Search
3) Insert
4) Delete
Example
• Construct a Binary Search Tree by inserting the following sequence of
numbers: 10, 12, 5, 4, 20, 8, 7, 15 and 13
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the
link fields contain NULL values which results in wastage of storage
space. If a binary tree consists of n nodes then n+1 link fields contain
NULL values.
• So in order to effectively manage the space, a method was devised by
Perlis and Thornton in which the NULL links are replaced with special
links known as threads.
• Such binary trees with threads are known as threaded binary trees.
Each node in a threaded binary tree either contains a link to its child
node or thread to other nodes in the tree.
Types of Threaded Binary Tree
• There are two types of threaded Binary Tree:
• One-way threaded Binary Tree
• Two-way threaded Binary Tree
One-way threaded Binary trees
• In one-way threaded binary trees, a thread will appear either in the right or left
link field of a node.
• If it appears in the right link field of a node then it will point to the node which
is Inorder successor. Such trees are called Right threaded binary trees.
• If thread appears in the left field of a node then it will point to the node which
is Inorder predecessor. Such trees are called Left threaded binary trees.
• Left threaded binary trees are used less often as they don't yield the last
advantages of right threaded binary trees.
• In one-way threaded binary trees, the right link field of last node and left link
field of first node contains a NULL. In order to distinguish threads from normal
links they are represented by dotted lines.
Cntd…
Two-way threaded Binary Trees
• In two-way threaded Binary trees, the right link field of a node
containing NULL values is replaced by a thread that points to nodes
inorder successor and left field of a node containing NULL values is
replaced by a thread that points to nodes inorder predecessor.
Cntd…