Professional Documents
Culture Documents
Reny Jose
Binary Tree
• A binary tree is a data structure that is defined as a collection of
elements called nodes.
• In a binary tree, the topmost element is called the root node, and
each node has 0, 1, or at the most 2 children.
• A node that has zero children is called a leaf node or a terminal node
• Definition : a binary tree is a finite set of nodes which is either empty
or consists of a root and two disjoint binary tree called the left sub
tree and right sub tree.
Binary Tree
Not Binary Tree
Binary Tree Representations
• A binary tree data structure is represented using two methods
• Array Representation
• Linked List Representation
Array Representation of Binary Tree
• In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
To represent a binary tree of depth 'n' using array representation, we need one
dimensional array with a maximum size of 2n + 1.
To get parent index from child we have to follow this formula − Parent=[child / 2
Example 2
• the position of two children of one node by using
this formula
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
10 5 16 8 15 20 23
Linked List Representation of Binary
Tree
• Use a double linked list to represent a binary tree.
• First field for storing left child address, second for storing actual data
and third for storing right child address.
Example 2
Example 3- memory
representation of a tree
Binary Tree Traversal
• Traversal is the process of visiting every node once
• Three recursive techniques for binary tree traversal
• Preoder,
• Inorder,
• Postorder
• In each technique, the left subtree is traversed
recursively, the right subtree is traversed recursively,
and the root is visited
CS 103 13
Preoder, Inorder, Postorder
• In Preorder, the root Preorder Traversal:
1. Visit the root
is visited before (pre)
2. Traverse left subtree
the subtrees traversals 3. Traverse right subtree
• In Inorder, the root is Inorder Traversal:
visited in-between left 1. Traverse left subtree
2. Visit the root
and right subtree traversal 3. Traverse right subtree
• In Preorder, the root Postorder Traversal:
is visited after (pre) 1. Traverse left subtree
the subtrees traversals 2. Traverse right subtree
3. Visit the root
CS 103 14
Illustrations for Traversals
1
• Assume: visiting a node
3 7
is printing its label
• Preorder:
5 8 9
1 3 5 4 6 7 8 9 10 11 12
• Inorder:
4 6 10
4 5 6 3 1 8 7 9 11 10 12
• Postorder:
11 12
4 6 5 3 8 11 12 10 9 7 1
CS 103 15
Algorithm Preoder, Inorder, Postorder
Preorder Traversal:
1. Visit the root
2. Traverse left subtree
3. Traverse right subtree
Inorder Traversal:
1. Traverse left subtree
2. Visit the root
3. Traverse right subtree
Postorder Traversal:
1. Traverse left subtree
2. Traverse right subtree
3. Visit the root
CS 103 16
Illustrations for Traversals (Contd.)
• Assume: visiting a node is printing
15
its data
8 20
• Preorder:
• 15 8 2 6 3 7 11 10 12 14 20 27 22 30 27
2 11
• Inorder:
• 2 3 6 7 8 10 11 12 14 15 20 22 27 30 6 10 12 22 30
• Postorder:
3 7 14
• 3 7 6 2 10 14 12 11 8 22 30 27 20 15
CS 103 17
Binary tree construction Expression Trees : Binary
trees used to store
algebraic expressions
Given an expression,
Exp = ((a + b) – (c * d)) % ((f ^g) / (h – i)),
construct the corresponding binary tree
Given the binary tree, write down the expression that it represents.