Chapter Six Tree and Graph
Chapter Six Tree and Graph
07/07/2023 Ataklti N. 1
6.1. Tree
B E F G
C D H I J
K L M
07/07/2023 Ataklti N. 3
….Continued
• Root: a node without a parent Or the node at the topmost of the tree is called root. A
• Internal node: a node with at least one child. A, B, F, I, J
• External (leaf) node: is a node that does not have a child node in the tree C, D, E, H, K,
L, M, G
• Ancestors of a node: parent, grandparent, grand-grandparent, etc of a node.
Ancestors of K A, F, I
• Descendants of a node: children, grandchildren, grand-grandchildren etc of a node.
Descendants of F H, I, J, K, L, M
• Depth of a node: number of ancestors or length of the path from the root to the node.
• Depth of H 2
• Height of a tree: is the length of the longest path to a leaf. 3
• Subtree: a tree consisting of a node and its descendants.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child − The node below a given node connected by its edge downward is called its child
node
• Edge is the link between two nodes
• There is only one root per tree and one path
07/07/2023
from the root node to any node.
Ataklti N. 4
Binary tree
• Binary tree: a tree data structure has a special condition that each node can have
• Full binary tree: a binary tree where each node has either 0 or 2 children.
• Balanced binary tree: a binary tree where each node except the leaf nodes has
left and right children and all the leaves are at the same level.
• The difference between heights of left subtree and right subtree is not more than 1
level.
• Complete binary tree: a binary tree in which the length from the root to any leaf
• The basic operations that can be performed on a binary search tree data
• Traversal /display-is a process to visit all the nodes of a tree and may
07/07/2023 Ataklti N. 8
Traversing
• Binary search tree can be traversed in three ways.
– Pre order traversal- traversing binary tree in the order of parent, left and right.
– Inorder traversal- traversing binary tree in the order of left, parent and right.
– Postorder traversal- traversing binary tree in the order of left, right and parent.
• Preorder traversal
- 10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19
• Inorder traversal
- 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
==> Used to display nodes in ascending order.
• Postorder traversal
- 4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 15, 10
07/07/2023 Ataklti N. 9
07/07/2023 Ataklti N. 10
Deletion a node from the BST
• To delete a node from binary search tree (whose root node is
pointed by RootNodePtr), four cases should be considered.
• When a node is deleted the definition of binary search tree should
be preserved.
RootNodePtr
• Consider the following binary search tree.
10
6 14
3 8 12 18
2 4 7 9 11 13 16 19
15 17
1 5
07/07/2023 Ataklti N. 11
…Continued
• Case 1: Deleting a leaf node (a node having no child), e.g. 7
• Case 2: Deleting a node having only one child, e.g. 2
• Approach 1: Deletion by merging – one of the following is done
• If the deleted node is the left child of its parent and the deleted node has only the left child,
the left child of the deleted node is made the left child of the parent of the deleted node.
• If the deleted node is the left child of its parent and the deleted node has only the right child,
the right child of the deleted node is made the left child of the parent of the deleted node.
• If the deleted node is the right child of its parent and the node to be deleted has only the left
child, the left child of the deleted node is made the right child of the parent of the deleted
node.
• If the deleted node is the right child of its parent and the deleted node has only the right
child, the right child of the deleted node is made the right child of the parent of the deleted
node.
07/07/2023 Ataklti N. 12
…Continued
• Approach 2: Deletion by copying- the following is done
• Copy the node containing the largest element in the left (or the
smallest element in the right) to the node containing the
element to be deleted.
07/07/2023 Ataklti N. 13
…Continued
• Case 3: Deleting a node having two children,
• Approach 1: Deletion by merging – one of the following is done
• If the deleted node is the left child of its parent, one of the following is
done
– The left child of the deleted node is made the left child of the parent of
the deleted node, and
– The right child of the deleted node is made the right child of the node
containing largest element in the left of the deleted node
• OR
– The right child of the deleted node is made the left child of the parent
of the deleted node, and
– The left child of the deleted node is made the left child of the node
containing smallest element in the right of the deleted node
07/07/2023 Ataklti N. 14
…Continued
• node is made the left child of the node containing smallest element in the
right of the deleted node
• If the deleted node is the right child of its parent, one of the following is
done
– The left child of the deleted node is made the right child of the parent of the deleted
node, and
– The right child of the deleted node is made the right child of the node containing
largest element in the left of the deleted node
• OR
– The right child of the deleted node is made the right child of the parent of the deleted
node, and
– The left child of the deleted node is made the left child of the node containing
smallest element in the right of the deleted node
07/07/2023 Ataklti N. 15
…Continued
• Approach 2: Deletion by copying- the following is done
• Copy the node containing the largest element in the left (or the smallest
element in the right) to the node containing the element to be deleted
• Delete the copied node
07/07/2023 Ataklti N. 16
…Continued
• Case 4: Deleting the root node, 10
• Approach 1: Deletion by merging- one of the following is done
• If the tree has only one node the root node pointer is made to point to nothing
(NULL)
• If the root node has left child
– the root node pointer is made to point to the left child
– the right child of the root node is made the right child of the node containing the
largest element in the left of the root node
• If root node has right child
– the root node pointer is made to point to the right child
– the left child of the root node is made the left child of the node containing the
smallest element in the right of the root node
07/07/2023 Ataklti N. 17
…Continued
07/07/2023 Ataklti N. 18
Search Operation
07/07/2023 Ataklti N. 19
Application of binary tree traversal
• Store values on leaf nodes and operators on internal nodes
• Preorder traversal(PLR)
– used to generate mathematical expression in prefix notation.
• Inorder traversal(LPR)
– used to generate mathematical expression in infix notation.
• Postorder traversal(LRP)
– used to generate mathematical expression in postfix notation.
• Example:
+
Preorder traversal - + – A * B C + D / E F Prefix notation
Inorder traversal - A – B * C + D + E / F Infix notation
– + Postorder traversal - A B C * – D E F / + + Postfix notation
A * D /
B C E F
07/07/2023 Ataklti N. 20
Conversion of Prefix to Infix using stack
07/07/2023 Ataklti N. 21
• Algorithm for Prefix to Postfix:
• Read the Prefix expression in reverse order (from right
to left)
• If the symbol is an operand, then push it onto the Stack
• If the symbol is an operator, then pop two operands
from the Stack
Create a string by concatenating the two operands and
the operator after them.
string = operand1 + operand2 + operator
And push the resultant string back to Stack
• Repeat the above steps until end of Prefix expression.
07/07/2023 Ataklti N. 22
• How to Convert Postfix Notation to Infix Notation Using Stack
• Working from left to right, scan each character of the postfix expression,
and take one of the following two actions.
• If the character is an operand, push it to the stack.
• If the character is an operator, pop the top value from the stack for its right
operand and pop the next top value from the stack for its left operand.
Finally, insert the operator between its operands, place the infix string
within a set of parenthesis, and then push the infix string back to the stack.
• Repeat the above scanning and subsequent actions until all postfix
characters have been handled. If the postfix expression was valid, you
should be left with a single element in the stack, which is the infix
equivalent of the postfix expression.
• Example : 8 9 5 ^ 5 - *
• And its possible to convert from Infix to post fix and vice versa
• convert from prefix to infix and vice versa
• convert from prefix to post fix and vice versa
• By either Tree or stack operations
07/07/2023 Ataklti N. 23
6.2. Graph
• Graph and tree are the non-linear data structure which is used
to solve various complex problems.
• A graph is a group of vertices and edges where an edge
connects a pair of vertices whereas a tree is considered as a
minimally connected graph which must be connected and free
from loops.
07/07/2023 Ataklti N. 24
…Continued
07/07/2023 Ataklti N. 25
»I Thank You
07/07/2023 Ataklti N. 26