0% found this document useful (0 votes)
8 views16 pages

Lesson 7 Trees

This document provides an overview of trees as a data structure, specifically binary trees and binary search trees. It defines important tree terms like root, parent, child, leaf nodes. It describes properties of binary trees like each node having a maximum of two children. It also covers tree traversal methods like in-order, pre-order and post-order traversal and gives an example of in-order traversal. Spanning trees and minimum spanning trees are discussed along with applications and algorithms for finding minimum spanning trees.

Uploaded by

Shiro Ein
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
8 views16 pages

Lesson 7 Trees

This document provides an overview of trees as a data structure, specifically binary trees and binary search trees. It defines important tree terms like root, parent, child, leaf nodes. It describes properties of binary trees like each node having a maximum of two children. It also covers tree traversal methods like in-order, pre-order and post-order traversal and gives an example of in-order traversal. Spanning trees and minimum spanning trees are discussed along with applications and algorithms for finding minimum spanning trees.

Uploaded by

Shiro Ein
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 16

Discrete Structure 2

Lesson 7: Trees
Prepared by: Ms. Kimberly Reyes
Trees
Tree represents the nodes connected by edges.
We will discuss binary tree or binary search tree
specifically.

Binary Tree is a special data structure used
for data storage purposes. A binary tree has
a special condition that each node can have
a maximum of two children. A binary tree
has the benefits of both an ordered array
and a linked list as search is as quick as in a
sorted array and insertion or deletion
operation are as fast as in linked list.
Binary Tree
Important Terms
Following are the important terms with respect to
tree.
Path − Path refers to the sequence of nodes along the
edges of a tree.

Root − The node at the top of the tree is called root. There
is only one root per tree and one path from the root node
to any node.

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.

Leaf − The node which does not have any child node is
called the leaf node.
Subtree − Subtree represents the descendants of a node.

Visiting − Visiting refers to checking the value of a node


when control is on the node.

Traversing − Traversing means passing through nodes in


a specific order.

Levels − Level of a node represents the generation of a


node. If the root node is at level 0, then its next child node
is at level 1, its grandchild is at level 2, and so on.

keys − Key represents a value of a node based on which


a search operation is to be carried out for a node.
Binary Search Tree Representation
Binary Search tree exhibits a special behavior. A node's left child must have a value less
than its parent's value and the node's right child must have a value greater than its parent
value.
Traversal is a process to visit all the nodes of a tree and may
print their values too. Because, all nodes are connected via
edges (links) we always start from the root (head) node. That is,
we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −

In-order Traversal
Pre-order Traversal
Post-order Traversal

Generally, we traverse a tree to search or locate a given item or


key in the tree or to print all the values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right
sub-tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.

We start from A, and following in-


order traversal, we move to its left
subtree B. B is also traversed in-
order. The process goes on until all
the nodes are visited. The output of
inorder traversal of this tree will be

D→B→E→A→F→C→G
A spanning tree is a subset of Graph G, which has all the
vertices covered with minimum possible number of edges.
Hence, a spanning tree does not have cycles and it cannot be
disconnected..

By this definition, we can draw a conclusion that every


connected and undirected Graph G has at least one spanning
tree. A disconnected graph does not have any spanning tree,
as it cannot be spanned to all its vertices.
Spanning Trees
General Properties of Spanning Tree
We now understand that one graph can have
more than one spanning tree. Following are
a few properties of the spanning tree
connected to graph G −
l A connected graph G can have more than one spanning
tree.
l All possible spanning trees of graph G, have the same
number of edges and vertices.
l The spanning tree does not have any cycle (loops).
l Removing one edge from the spanning tree will make the
graph disconnected, i.e. the spanning tree is minimally
connected.
Application of Spanning Tree
Spanning tree is basically used to find a
minimum path to connect all nodes in a
graph. Common application of spanning
trees are −
-Civil Network Planning
-Computer Network Routing Protocol
-Cluster Analysis

Let us understand this through a small example.


Consider, city network as a huge graph and now plans
to deploy telephone lines in such a way that in
minimum lines we can connect to all city nodes. This
is where the spanning tree comes into picture.
Minimum Spanning Tree (MST)
In a weighted graph, a minimum spanning tree is a
spanning tree that has minimum weight than all other
spanning trees of the same graph. In real-world
situations, this weight can be measured as distance,
congestion, traffic load or any arbitrary value denoted
to the edges.

Minimum Spanning-Tree Algorithm


We shall learn about two most important spanning
tree algorithms here −
Kruskal's Algorithm
Prim's Algorithm

Both are greedy algorithms.


Thanks!
Any questions?

You might also like