Trees
Chapter 11
11.1 Introduction to TREE �and its Terminologies
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Graph VS TREE
Trees
Definition: A tree is a connected undirected graph with no simple circuits.
Definition: An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. A tree cannot contain multiple edges or loops.
Definition: An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.
Trees
Example: Which of these graphs are trees?
Solution: G1 and G2 are trees - both are connected and have no simple circuits. G3 is not a tree because e, b, a, d, e is a simple circuit,. G4 is not a tree because it is not connected.
Forest
Definition: A forest is a graph that has no simple circuit, but is not connected. Each of the connected components in a forest is a tree.
General form of TREEs
Applications of Trees
Section 11.2
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Trees as Models
Arthur Cayley
(1821-1895)
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Trees as Models
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Trees as Models
Applications of Trees
Trees can be used to analyze certain types of games such as tic-tac-toe, nim, checkers, and chess.
Universal Address Systems
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Prefix code
Definition: A code that has the property that the code of a character is never a prefix of the code of another character.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Huffman Coding
Huffman code is a way to encode information using variable-length strings to represent symbols depending on how frequently they appear. The idea is that symbols that are used more frequently should be shorter while symbols that appear more rarely can be longer.
Decision Trees
Definition: A rooted tree where each vertex represents a possible outcome of a decision and the leaves represent the possible solutions of a problem.
Example : A decision tree that orders the elements of the list a, b, c.
Rooted Trees
Definition: A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Rooted Tree Terminology
Rooted Tree Terminology
Rooted Tree Terminology
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Rooted Tree Terminology
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Terminology for Rooted Trees
Example: In the rooted tree T (with root a):
Solution:
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Terminology for Rooted Trees
Example: In the rooted tree T (with root a):
Solution:
Terminology for Rooted Trees
Solution:
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Level of vertices and height of trees
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Level of vertices and height of trees
Example:
(i) Find the level of each vertex in
the tree to the right.
(ii) What is the height of the tree?
Solution:
(i) The root a is at level 0.
Vertices b, j, and k are at level 1.
Vertices c, e, f, and l are at level 2.
Vertices d, g, i, m, and n are at level 3.
Vertex h is at level 4.
(ii) The height is 4, since 4 is the largest level of any vertex.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
m-ary Rooted Trees
Definition: A rooted tree is called an m-ary tree if every internal vertex has no more than m children. The tree is called a full m-ary tree if every internal vertex has exactly m children. An m-ary tree with m = 2 is called a binary tree.
Example: Are the following rooted trees full m-ary trees for some positive integer m?
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Solution:
5-ary tree.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Balanced m-Ary Trees
Definition: A rooted m-ary tree of height h is balanced if all leaves are at levels h or h − 1.
Example: Which of the rooted trees shown below is balanced?
Solution: T1 and T3 are balanced, but T2 is not because it has leaves at levels 2, 3, and 4.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Ordered Rooted Trees
Definition: An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Binary Trees
Definition: A binary tree is an ordered rooted where each internal vertex has at most two children. If an internal vertex of a binary tree has two children, the first is called the left child and the second the right child. The tree rooted at the left child of a vertex is called the left subtree of this vertex, and the tree rooted at the right child of a vertex is called the right subtree of this vertex.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example:
Consider the binary tree T.
(i) What are the left and right children of d?
(ii) What are the left and right sub trees of c?
Solution:
(i) The left child of d is f and the right child is g.
(ii) The left and right subtrees of c are displayed in (b) and (c).
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Properties of Trees
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Binary Search Tree
Definition: A binary tree in which the vertices are labeled with items so that a label of a vertex is greater than the labels of all vertices in the left subtree of this vertex and is less than the labels of all vertices in the right subtree of this vertex.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example : Form a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry (using alphabetical order).
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Tree Traversal
Section 11.3
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Traversal Algorithms Procedures for systematically visiting every vertex of an ordered rooted tree are called traversal algorithms. We will describe three of the most commonly used such algorithms, preorder traversal, inorder traversal, and postorder traversal.
11.3 Tree Traversal
Tree Traversal Shortcut Preorder:
Inorder:
Postorder:
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Tree Traversal
Definition: Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. Otherwise, suppose that T1, T2, …, Tn are the subtrees of r from left to right in T. The preorder traversal begins by visiting r, and continues by traversing T1 in preorder, then T2 in preorder, and so on, until Tn is traversed in preorder.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Inorder traversal: Visit leftmost subtree, visit root, visit other subtrees left to right
Inorder Traversal
Definition: Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T. Otherwise, suppose that T1, T2, …, Tn are the subtrees of r from left to right in T. The inorder traversal begins by traversing T1 in inorder, then visiting r, and continues by traversing T2 in inorder, and so on, until Tn is traversed in inorder.
Inorder traversal:
Visit leftmost subtree,
visit root, visit other
subtrees left to right
Postorder Traversal
Definition: Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. Otherwise, suppose that T1, T2, …, Tn are the subtrees of r from left to right in T. The postorder traversal begins by traversing T1 in postorder, then T2 in postorder, and so on, after Tn is traversed in postorder, r is visited.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Postorder traversal:
Visit subtrees left to right;
visit root
Infix, Prefix, and Postfix Notation �Expression Trees
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Infix Notation
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Tree Traversal Shortcut Preorder:
Inorder:
Postorder:
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Deep Understanding
(x + y) / (x + 3)
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Prefix Notation
is + ↑ + x y 2 / − x 4 3.
Jan Łukasiewicz (1878-1956)
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Prefix Notation
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Postfix Notation
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Postfix Notation
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Spanning Trees
Section 11.4
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
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 Tree
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Kirchhoff's Matrix tree theorem
Understanding Minimum spanning tree
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
A minimum spanning tree is a spanning tree in which the sum of the weight of the edges is as minimum as possible.
Minimum spanning tree
A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of its edges.
PRIM’S ALGORITHM
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example: Use Prims algorithm to find a minimal spanning tree for the graph below. Indicate the order in which edges are added to form the tree.�
Minimal spanning tree (MST)
Example: Use Prims algorithm to find a minimal spanning tree for the graph below. Indicate the order in which edges are added to form the tree.�
Order of adding the edges:
{a , b}, {b , c}, {c , d}, {d , e}, {e , f}, {f , g}
MST COST = 23
Minimal spanning tree (MST)
KRUSKAL’S ALGORITHM
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Minimal spanning tree (MST)
Example: Use Kruskal’s algorithm to find a minimal spanning tree for the graph below. Indicate the order in which edges are added to form the tree.
Minimal spanning tree (MST)
Example: Use Kruskal’s algorithm to find a minimal spanning tree for the graph below. Indicate the order in which edges are added to form the tree.
MST COST = 23
11.4 Spanning Trees �Depth-First Search | Breadth-First Search | Backtracking Applications | Depth-First Search in Directed Graphs
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees �Depth-First Search | Breadth-First Search | Backtracking Applications | Depth-First Search in Directed Graphs
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees �Depth-First Search | Breadth-First Search | Backtracking Applications | Depth-First Search in Directed Graphs
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees �Depth-First Search | Breadth-First Search | Backtracking Applications | Depth-First Search in Directed Graphs
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees �Depth-First Search | Breadth-First Search | Backtracking Application| Depth-First Search in Directed Graphs
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/