Lecture # 11 Trees & Graphs
Lecture # 11 Trees & Graphs
Lecture # 11 Trees & Graphs
Lecture # 11
TREES & GRAPHS
(Introduction to Tree, Binary Tree, Graphs)
1
(1 – حال201( حاسب آلـى: المقـرر
INTRODUCTION TO TREES
1. What is tree?
a. Root Node
Node without parent is called root node. Except for the root (no parent), every
node has exactly one parent (by definition). A tree has only one root node.
b. Sibling Node
Nodes that are children of the same parent are called sibling.
c. Internal Node
—A node is internal if it has one or more children.
2
(1 – حال201( حاسب آلـى: المقـرر
d. Leaf Node
A node is a leaf if it has no children
2. What is subtree?
The subtree of T rooted at node v is the tree consisting of all the descendants of v in T
(including v) tree consisting of a node and its descendants
3
(1 – حال201( حاسب آلـى: المقـرر
The height of a tree is the maximum depth of its lowest level leaves. Also called maximum levels
of a tree.
Two nodes are adjacent if an edge connects them. A path is a sequence of nodes in which each
node is adjacent to the next one. Every node in the tree can be reached by following
a unique path starting from the root. The length of a path is the number of edges on the path.
5. Types of Trees
4
(1 – حال201( حاسب آلـى: المقـرر
6. Tree Traversal
There are 2 basic approaches to traverse a tree - breadth-first and depth-first. With breadth-first
you start at the top node then visit all its children before visiting its children's children - i.e. you
explore the tree layer by layer. With depth-first searching you keep going down, backtracking
when you can go no further.
With either method there's a choice of ways to order operations on the node.
In a preorder traversal, the root node of the tree is processed first, then the left subtree is
traversed, then the right subtree.
In a postorder traversal, the left subtree is traversed, then the right subtree, and then the
root node is processed.
c) Inorder Traversal:
In an inorder traversal, the left subtree is traversed first, then the root node is processed,
then the right subtree is traversed.
5
(1 – حال201( حاسب آلـى: المقـرر
7. What is Graph
A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes
to each other. The set of edges describes relationships among the vertices. As a formal
definition, a graph G is defined as follows:
G=(V,E)
There are two important sets of objects, which specify graph and its structure. First set
is V, which is called vertex-set. Each vertex can be drawn as a circle with vertex's number
inside.
Vertices
Next important set is E, which is called edge-set.
All graphs are divided into two big groups: directed and undirected graphs. The difference is that
edges in directed graphs, called arcs, have a direction.
Edge can be drawn as a line. If a graph is directed, each line has an arrow.
6
(1 – حال201( حاسب آلـى: المقـرر
- Sequence of vertices, such that there is an edge from each vertex to the next in sequence,
is called path.
- First vertex in the path is called the start vertex;
- The last vertex in the path is called the end vertex.
- If start and end vertices are the same, path is called cycle.
- Path is called simple, if it includes every vertex only once.
- Cycle is called simple, if it includes every vertex, except start (end) one, only once. Let's
see examples of path and cycle.