Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Tree

Tree
What is Tree?
• An undirected graph is a tree if and only if
there is a unique simple path between any two
of its vertices.
• Every Tree is a Graph ,but every Graph is
not a tree.
Basic Terminology of Tree
• Node: A node is a fundamental part of a tree.
Each letter represents one node.
•Edge: The arrows from one node to
another are called edges.
Basic Terminology of Tree
•Root: The root of the tree is the
only node in the tree that has no
incoming edges.
•Here, a is the root.
•Leaf Node: A leaf node is a node
that has no children.
•The bottom nodes (with no outgoing
edges) are the leaves .
•Here, c , i , j , k , l , m are leaves
Node.
Basic Terminology of Tree
•Depth: Depth tells the
number of steps (nodes) to
get from a node back to the
root.
•Height: The height of a tree
is equal to the maximum
level of any node in the tree.
•This tree has height 5,
so the maximum depth
is 4 (height - 1).
Basic Terminology of Tree
Parent:
 a is the parent of b , c , d
 b is the parent of e
 d is the parent of f , g , h
 e is the parent of i , j
 f is the parent of k
 h is the parent of l , m

Siblings:
 b , c , d are siblings of each other
Children:
 b , c , d are children of a
 f , g , h are siblings of each other  f , g , h are children of d
 i , j are siblings of each other  e is the children of b
 l , m are siblings of each other  i , j are the children of e
 k is the children of f
 l , m are the children of h
Basic Terminology of Tree
Basic Terminology of Tree
• Sub-Tree: A sub-tree of a given
node includes one of its children
and all of that child's
descendants.
Traversing a Binary Tree
• Inorder tree walk:
– Prints the keys of a binary tree in sorted order
– Root is printed between the values of its left and
right subtrees: left, root, right
• Preorder tree walk:
– root printed first: root, left, right
• Postorder tree walk: left, right, root
– root printed last Inorder: 2 3 5 5 7 9
5 Preorder: 5 3 2 5 7 9
3 7 Postorder: 2 5 3 9 7 5
2 5 9 10
Minimum spanning trees (MST)
• It may be defined on Euclidean space points or
on a graph.
• G = (V, E): weighted connected undirected
graph
• Spanning tree : S = (V, T), T  E, undirected
tree
• Minimum spanning tree(MST) : a spanning
tree with the smallest total weight.

11
An example of MST
• A graph and one of its minimum costs
spanning tree

12
Kruskal’s algorithm for finding MST

Step 1: Sort all edges into nondecreasing order.


Step 2: Add the next smallest weight edge to the
forest if it will not cause a cycle.
Step 3: Stop if n-1 edges. Otherwise, go to Step2.

13
Kruskal’s algorithm
• In this algorithms we order the weights in ascending
order and select those routes only that may not
cause any cycle

14
An example of Kruskal’s algorithm

15
Prim’s algorithm for finding MST
Step 1: x  V, Let A = {x}, B = V - {x}.
Step 2: Select (u, v)  E, u  A, v  B such that
(u, v) has the smallest weight between A
and B.
Step 3: Put (u, v) in the tree. A = A  {v}, B = B
- {v}
Step 4: If B = , stop; otherwise, go to Step 2.

• Time complexity : O(n2), n = |V|.


(see the example on the next page)

16
An example for Prim’s algorithm

17
Prim’s Algorithm Example for MST
• A={ }
• B={1,2,3,4,5,6}

At First Step: 1

A={1}
B={2,3,4,5,6} 1-2= 10, 1-3= Nil,1-4=30,1-5=45,1-6=Nil
smallest=10 that is from 1 to 2
1 2

18
Prim’s Algorithm Example for MST
At Second Step:
A={1,2}
B={3,4,5,6}
1 2

• 1-3= Nil, 1-4= 30,1-5=45,1-6=Nil


• 2-3= 50, 2-4= Nil,2-5=40,2-6=25
1 2 6
• smallest = 25 that is from 2 to 6
19
Prim’s Algorithm Example for MST
At third Step:
1 2 6
A={1,2,6}
B={3,4,5}

• 1-3= Nil, 1-4= 30,1-5=45


• 2-3= 50, 2-4= Nil,2-5=40
• 6-3= 15, 6-4= 20,6-5=55

smallest = 15 that1is from2 6 to 36 3


20
Prim’s Algorithm Example for MST
At fourth Step: 1 2 6 3

A={1,2,6,3}
B={4,5}
• 1-4= 30,1-5=45
• 2-4= Nil,2-5=40
• 6-4= 20,6-5=55
• 3-4= Nil,3-5=35

smallest = 20 that
1
is from
2
6 to6 4 3

4 21
Prim’s Algorithm Example for MST
At fifth Step:
1 2 6 3
A={1,2,6,3,4}
4
B={5}
• 1-5=45
• 2-5=40
• 6-5=55
• 3-5=35
• 4-5=Nil
smallest = 35 that is from 3 to 5
1 2 6 3 5

4 22
An example for Prim’s algorithm

23

You might also like