Unit4 Trees
Unit4 Trees
Data Structures
Trees
• Tree is a hierarchical data structure which stores the information
naturally in the form of hierarchy style.
• Tree is one of the most powerful and advanced data structures.
• It is a non-linear data structure compared to arrays, linked lists,
stack and queue.
Nature View of a Tree
leaves
branches
root
Computer Scientist’s View
root
leaves
branches
nodes
Trees Terminology
• Node: It is a data element of a tree.
• Root: It is the top node in a tree.
• Parent: A node that has one or more child nodes present below.
• Child nodes: All nodes in a tree except the root node are child nodes of
their immediate predecessor nodes.
• Leaf node: A node that has no children.
• Internal nodes: All nodes in a tree except the root and leaf nodes.
• Sibling: All nodes that are at the same level and share the same parent are
called siblings (brothers).
• Level number: Levels of a node represents the number of connections
between the node and the root. It represents generation of a node. If the
root node is at level 0, its next node is at level 1, its grand child is at level 2
and so on.
Trees Terminology
• Degree: Degree of a node is equal to the number of children that a node
has.
• In-degree of a node is the number of edges arriving at that node.
• Out-degree of a node is the number of edges leaving that node.
• Edge: It is the line connecting a node N to any of its successors
• Path: A sequence of consecutive edges from source node to destination
node.
• Depth: The number of edges from the tree's root node to the node. The
depth of the root node is zero.
• Height: Height of a tree It is the total number of nodes on the path from the
root node to the deepest node in the tree. A tree with only a root node has a
height of 1. A binary tree of height h has at least h nodes and at most 2^h –
1 nodes. This is because every level will have at least one node and can
have at most 2 nodes. The height of a binary tree with n nodes is at least
log2(n+1) and at most n.
Types of Trees
➢General Trees
➢Forests
➢Binary Trees
➢Expression Trees
➢Tournament Trees
General Trees
T1 T2
2 3
•Given the binary tree, write down the expression that it represents.
Use the operator precedence chart to find the sequence in which operations will be performed.
The given expression can be written as
Exp = ((a + ((b/c) * d)) – e)
Tournament Trees
• In a tournament tree (also called a selection tree), each external node
represents a player and each internal node represents the winner of
the match played between the players represented by its children
nodes.
• These tournament trees are also called winner trees because they are
being used to record the winner at each level.
• We can also have a loser tree that records the loser at each level.
a
a e
a
d e g
a b c d e f g h
Binary Trees - Key Terms
• Similar binary trees: Given two binary trees T and T’ are said to be similar if
both these trees have the same structure.
TREE T
TREE T”
A F
B C G H
D I
E J
• Copies of binary trees: Two binary trees T and T’ are said to be copies if they
have similar structure and same content at the corresponding nodes.
TREE T
TREE T”
A A
B C B C
E
D E D
Complete Binary Trees
• 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.
• In a complete binary tree Tn, there are exactly n nodes and level r of T can have at
most 2r nodes.
– Level 0 has 2^0 = 1 node, level 1 has 2^1 = 2 nodes, level 2 has 2^2 = 4 nodes,
level 3 has 6 nodes which is less than the maximum of 2^3 = 8 nodes.
• The formula to find the parent, left child and right child can be given as:
– If K is a parent node, then its left child can be calculated as 2 * K
and its right child can be calculated as 2 * K + 1. 1
Binary tree
Extended binary tree
Sequential Representation of Binary Trees
• Sequential representation of trees is done using a single or one dimensional array.
Though, it is the simplest technique for memory representation, it is very inefficient
as it requires a lot of memory space.
• A sequential binary tree follows the rules given below:
– One dimensional array called TREE is used.
– The root of the tree will be stored in the first location. That is, TREE[0] will store
the data of the root element. Its left and right child nodes are stored at the
successive positions.
– If a node is stored at index location i then its left child node will be stored at
location (2i+1) while Right child node will be stored at location (2i+2).
– The maximum size of the array TREE is given as (2 h-1), where h is the height of the
tree.
• An empty tree or sub-tree is specified using NULL. If TREE[0] = NULL, then the
tree is empty. 20
15 35
12 39
17 21
36 45
16 18
Sequential Representation of Binary Trees
Advantages:
Direct access to all nodes (Random access)
Disadvantages:
Height of tree should be known
Memory may be wasted
Insertion and deletion of a node is difficult
Linked Representation of Binary Trees
• In computer’s memory, a binary tree can be maintained either using a linked
representation or using sequential representation.
• In linked representation of binary tree, every node will have three parts:
• the data element
• a pointer to the left node
• and a pointer to the right node.
Position
Left Data Right
0 1 Jane 2
1 3 Bob 4
2 5 Tom -
3 - Alan -
4 - Ellen -
5 - Nancy -
6 - ? -
7 - ? -
8 - ? -
Linked Representation of Binary Trees
Advantages:
•Height of tree need not be known
•No memory wastage
•Insertion and deletion of a node is done without affecting other nodes
Disadvantages:
•Direct access to node is difficult
•Additional memory required for storing address of left and right node
Creating a Binary Tree from a General Tree
The rules for converting a general tree to a binary tree are:
Rule 1: Root of the binary tree = Root of the general tree
Rule 2: Left child of a node in the binary tree = Leftmost child of the
node in the general tree
Rule 3: Right child of a node in the binary tree = Right sibling of the
node in the general tree
Step 4: Now process node C. Left child of C is F (leftmost child) and its right
child is D (right sibling in general tree).
Step 5: Now process node D. Left child of D is I (leftmost child).There will be
no right child of D because it has no right sibling in the general tree.
Creating a Binary Tree From a general Tree
Step 6: Now process node I. There will be no left child of I in the binary tree because
I has no left child in the general tree. However, I has a right sibling J, so it will be
added as the right child of I.
Step 7: Now process node J. Left child of J is K (leftmost child). There will be no
right child of J because it has no right sibling in the general tree.
Step 8: Now process all the unprocessed nodes (E, F, G, H, K) in the same
fashion, so the resultant binary tree can be given as follows.
➢ Build the binary tree for a given general Trees
Traversing a Binary Tree
• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.
• There are three different algorithms for tree traversals, which differ in
the order in which the nodes are visited.
✓ Pre-order algorithm
✓ In-order algorithm
✓ Post-order algorithm
Pre-order Algorithm
• To traverse a non-empty binary tree in preorder, the following
operations are performed recursively at each node.
• Pre-order traversal is also called as depth-first traversal or NLR
(Node-Left-Right) traversal algorithm
• The algorithm starts with the root node of the tree and continues
by:
✓ Visiting the root node
✓ Traversing the left subtree
✓ Traversing the right subtree
Pre-order Algorithm
• Pre-order traversal algorithms are used to extract a prefix notation from an
expression tree.
A
B C
D E
H I
+–ab*cd
A, B, D, C, E, F, G, H and I A, B, D, G, H, L, E, C, F, I, J, and K
% – + a b * c d / ^ f g – hi ^+ /ab*cd/%fg– hi
In-order Algorithm
• To traverse a non-empty binary tree in in-order, the following operations are
performed recursively at each node. In-order traversal is also called as
symmetric traversal or LNR traversal algorithm (Left-Node-Right).
• The algorithm starts with the root node of the tree and continues by,
✓ Traversing the left subtree
✓ Visiting the root node
✓ Traversing the right subtree
In-order Algorithm
• In-order traversal algorithms are used to extract a infix notation from an
expression tree.
A
B C
D E
H I
B, D, A, E, H, G, I, F and C
G, D, H, L, B, E, A, C, I, F, K, and J
Post-order Algorithm
• To traverse a non-empty binary tree in post-order, the following
operations are performed recursively at each node.
• Post-order algorithm is also known as the LRN traversal algorithm
(Left-Right-Node).
• The algorithm starts with the root node of the tree and continues by,
✓ Traversing the left subtree
✓ Traversing the right subtree
✓ Visiting the root node
Post-order Algorithm
• Post-order traversal algorithms are used to extract a postfix notation from
an expression tree.
A
B C
D E
H I
D, B, H, I, G, F, E, C and A
G, L, H, D, E, B, I, K, J, F, C, and A
Applications of Trees
• Trees are used to store simple as well as complex data. Here simple
means an int value, char value and complex data (structure).
• Trees are often used for implementing other types of data structures
like hash tables, sets, and maps.
• A self-balancing tree, Red-black tree is used in kernel scheduling to
preempt massively multi-processor computer operating system use.
• Another variation of tree, B-trees are used to store tree structures on
disc. They are used to index a large number of records.
• B-trees are also used for secondary indexes in databases, where the
index facilitates a select operation to answer some range criteria.
• Trees are used for compiler construction.
• Trees are also used in database design.
• Trees are used in file system directories.
• Trees are also widely used for information storage and retrieval in
symbol tables.
Binary Search Trees
• A binary search tree (BST), also known as an ordered binary tree, is a
variant of binary tree in which the nodes are arranged in order.
• In a BST, all nodes in the left sub-tree have a value less than that of
the root node and all nodes in the right sub-tree have a value either
equal to or greater than the root node.
• The same rule is applicable to every sub-tree in the tree.
• Due to its efficiency in searching elements, BSTs are widely used in
dictionary problems where the code always inserts and searches the
elements that are indexed by some key value.
Binary Search Tree
(a)Left skewed
(b)Right skewed binary search trees
•The root node is 39.
•The left sub-tree of the root node consists of nodes 9, 10, 18, 19, 21, 27, 28, 29, and 36. All
these nodes have smaller values than the root node.
•The right sub-tree of the root node consists of nodes 40, 45, 54, 59, 60, and 65. All these
nodes have greater values than the root node.
•In a sorted array, searching can be done in O(log2n) time, but insertions and deletions are
quite expensive. In contrast, inserting and deleting elements in a linked list is easier, but
searching for an element is done in O(n) time. However, in the worst case, a binary search tree
will take O(n)time to search for an element.
Creating a Binary Search from Given Values
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
45
45
45
45 45 45 45 39 56
39 56
39 56
39 39 56 39 56 12 78
12 78
12
12 34
34
45 34
32
39 56 45
45
12 78 39 56
39 56
10 34 12 54 78
12 54 78
10 34 89
32
10 34 67 89
32
32
Searching for a Value in a BST
• The search function is used to find whether a given value is
present in the tree or not.
• The function first checks if the BST is empty. If it is, then the
value we are searching for is not present in the tree, and the search
algorithm terminates by displaying an appropriate message.
• However, if there are nodes in the tree then the search function
checks to see if the key value of the current node is equal to the
value to be searched.
• If not, it checks if the value to be searched for is less than the
value of the node, in which case it should be recursively called on
the left child node.
• In case the value is greater than the value of the node, it should be
recursively called on the right child node.
Algorithm to Search a Value in a BST
searchElement (TREE, VAL) ➢ Searching a node with value 12 in the given
Step 1: IF TREE->DATA = VAL OR
binary search tree.
TREE = NULL, then
Return TREE
ELSE
IF VAL < TREE->DATA
Return
searchElement(TREE->LEFT, VAL)
ELSE
Return
➢Searching a node with value 67 in the
searchElement(TREE->RIGHT, VAL)
[END OF IF] given binary search tree
[END OF IF]
Step 2: End
Algorithm to Insert a Value in a BST
Insert (TREE, VAL)
Step 1: IF TREE = NULL, then
Allocate memory for TREE
SET TREE->DATA = VAL
SET TREE->LEFT = TREE ->RIGHT = NULL
ELSE IF VAL < TREE->DATA
Insert(TREE->LEFT, VAL)
ELSE
Insert(TREE->RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: End
➢ Inserting nodes with value 12
Deleting a Value from a BST
• The delete function deletes a node from the binary search tree.
• However, care should be taken that the properties of the BSTs do
not get violated and nodes are not lost in the process.
• The deletion of a node involves any of the three cases.
• Case 1: Deleting a node that has no children.
• Case 2: Deleting a node with one child (either left or right).
• Case 3: Deleting a node with two children.
Case 1: Deleting a node that has no children.
➢ For example, deleting node with value 78 in the tree below.
45 45 45 45
39 56 39 56 39 56 39 56
54 78 54 78 54 78 54
55 55 55 55
Deleting a Value from a BST
• Case 2: Deleting a node with one child (either left or right).
• To handle the deletion, the node’s child is set to be the child of
the node’s parent.
• Now, if the node was the left child of its parent, the node’s
child becomes the left child of the node’s parent.
• Correspondingly, if the node was the right child of its parent,
the node’s child becomes the right child of the node’s parent.
➢ For example, deleting node with value 54 in the tree below.
45 45 45 45
39 56 39 56 39 56 39 56
54 78 54 78 54 78 55 78
55 55 55
Deleting a Value from a BST
• Case 3: Deleting a node with two children.
• To handle this case of deletion, replace the node’s value with its
in-order predecessor (largest value in the left sub-tree) or
in-order successor (smallest value in the right sub-tree).
• The in-order predecessor or the successor can then be deleted
using any of the above cases.
➢ For example, deleting node with value 56 in the tree below.
Algorithm to Delete from a BST
Delete (TREE, VAL)
Step 1: IF TREE = NULL, then
Write “VAL not found in the tree”
ELSE IF VAL < TREE->DATA
Delete(TREE->LEFT, VAL)
ELSE IF VAL > TREE->DATA
Delete(TREE->RIGHT, VAL)
ELSE IF TREE->LEFT AND TREE->RIGHT
SET TEMP = findLargestNode(TREE->LEFT)
SET TREE->DATA = TEMP->DATA
Delete(TREE->LEFT, TEMP->DATA)
ELSE
SET TEMP = TREE
IF TREE->LEFT = NULL AND TREE ->RIGHT = NULL
SET TREE = NULL
ELSE IF TREE->LEFT != NULL
SET TREE = TREE->LEFT
ELSE
SET TREE = TREE->RIGHT
[END OF IF]
FREE TEMP
[END OF IF]
Step 2: End
Determining the Height of a BST
• In order to determine the height of a BST, we will calculate the height
of the left and right sub-trees. Whichever height is greater, 1 is added
to it.
• Since height of right sub-tree is greater than the height of the left sub-
tree, the height of the tree = height (right sub-tree) + 1= 2 + 1 = 3
45
Height (TREE)
totalNodes (TREE)
39 78 78 39
54 79 79 54
MirrorImage (TREE)
Step 1: IF TREE != NULL , then
MirrorImage(TREE->LEFT)
MirrorImage(TREE->RIGHT)
SET TEMP = TREE->LEFT
SET TREE->LEFT = TREE->RIGHT
SET TREE_>RIGHT = TEMP
[END OF IF]
Step 2: End
Deleting a BST
• To delete/remove the entire binary search tree from the memory,
we will first delete the elements/nodes in the left sub-tree and then
delete the right sub-tree.
deleteTree (TREE)
Step 1: IF TREE != NULL , then
deleteTree (TREE->LEFT)
deleteTree (TREE->RIGHT)
Free (TREE)
[END OF IF]
Step 2: End
Finding the Smallest Node in a BST
• The basic property of a BST states that the smaller value will occur
in the left sub-tree.
• If the left sub-tree is NULL, then the value of root node will be
smallest as compared with nodes in the right sub-tree.
• So, to find the node with the smallest value, we will find the value
of the leftmost node of the left sub-tree.
• However, if the left sub-tree is empty then we will find the value of
the root node.
findSmallestElement (TREE)
Step 1: IF TREE = NULL OR TREE->LEFT = NULL, then
Return TREE
ELSE
Return findSmallestElement(TREE->LEFT)
[END OF IF]
Step 2: End