0% found this document useful (0 votes)
84 views

Chapter Six Tree and Graph

The document discusses trees and graphs as non-linear data structures, describing trees as a set of nodes connected by edges in a hierarchical structure and detailing common tree terminology. It also covers binary search trees as a type of ordered binary tree and methods for traversing, inserting, deleting, and searching nodes in a binary search tree while maintaining its properties.

Uploaded by

Soressa Hassen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Chapter Six Tree and Graph

The document discusses trees and graphs as non-linear data structures, describing trees as a set of nodes connected by edges in a hierarchical structure and detailing common tree terminology. It also covers binary search trees as a type of ordered binary tree and methods for traversing, inserting, deleting, and searching nodes in a binary search tree while maintaining its properties.

Uploaded by

Soressa Hassen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter Six:

Trees and Graphs

Data Structures and Algorithms


Prepared by:
Ataklti Nguse

07/07/2023 Ataklti N. 1
6.1. Tree

• A tree is a set of nodes and edges that connect pairs of


nodes.
• Trees are well-known as a non-linear data structure.
• They don’t store data in a linear way.
• They organize data hierarchically.
• One node distinguished as root.
• A tree has n-1 edges.
07/07/2023 Ataklti N. 2
6.1.1. Tree Terminologies
A

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

a maximum of two children called left child and right child.

• 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

node is either h or h-1 where h is the height of the tree.


07/07/2023 Ataklti N. 5
• The deepest level should also be filled from left to right.
6.1.2. Binary search tree (ordered binary tree)

• a binary tree that may be empty, but if it is not empty it satisfies


the following.
– Every node has a key and no two elements have the same key.
– The keys in the right subtree are larger than the keys in the
root and its parent.
– The keys in the left subtree are smaller than the keys in the
root and its parent..
– The left and the right subtrees are also binary search trees.
07/07/2023 Ataklti N. 6
BST Basic Operations

• The basic operations that can be performed on a binary search tree data

structure, are the following −

• Insert − Inserts an element in a tree/create a tree.

• Delete- remove an element from the tree.

• Search − Searches an element in a tree.

• Traversal /display-is a process to visit all the nodes of a tree and may

print their values too.

• we cannot randomly access a node in a tree.


07/07/2023 Ataklti N. 7
Insertion a Node

• Case 1: There is no data in the tree

– Make that node as root Node.


• Case 2: There is data
– Search the appropriate position.
– Insert the node in that position.

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

• 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. 18
Search Operation

• Whenever an element is to be searched, start searching from


the root node, then if the data is less than the key value, search
for the element in the left subtree.
• Otherwise, search for the element in the right subtree.
• Follow the same algorithm for each node.

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

• Algorithm for Prefix to Infix: 


• 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 between them. 
string = (operand1 + operator + operand2) 
And push the resultant string back to Stack
• Repeat the above steps until end of Prefix expression.

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

You might also like