0% found this document useful (0 votes)
20 views56 pages

Review Graph and Trees and New Topic On Trees

The document provides an overview of trees, defining them as directed, acyclic structures of linked nodes, and explains key terminology such as nodes, edges, root, parent, child, and leaf nodes. It also discusses special types of trees like binary trees and binary search trees, along with concepts of tree balance and height. Additionally, it introduces graph theory, defining graphs and their types, including directed and undirected graphs, and weighted and unweighted graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views56 pages

Review Graph and Trees and New Topic On Trees

The document provides an overview of trees, defining them as directed, acyclic structures of linked nodes, and explains key terminology such as nodes, edges, root, parent, child, and leaf nodes. It also discusses special types of trees like binary trees and binary search trees, along with concepts of tree balance and height. Additionally, it introduces graph theory, defining graphs and their types, including directed and undirected graphs, and weighted and unweighted graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Let’s Review: Tree

Tree - a directed, acyclic structure of linked nodes


□ Directed - one-way links between nodes (no cycles)
□ Acyclic - no path wraps back around to the same node
twice (typically represents hierarchical data)

CS 5002: Discrete ©Northeastern Fall 1


Tree Terminology:
Nodes
□ Tree - a directed, acyclic structure of linked nodes
□ Node - an object containing a data value and links to
other nodes
□ All the blue circles

CS 5002: Discrete ©Northeastern Fall 2


Tree Terminology:
Edges
□ Tree - a directed, acyclic structure of linked nodes
□ Edge - directed link, representing relationships
between nodes
□ All the grey lines

CS 5002: Discrete ©Northeastern Fall 3


Root
Node
□ Tree - a directed, acyclic structure of
linked nodes
□ Root - the start of the tree tree)
□ The top-most node in the tree
□ Node without parents

CS 5002: Discrete ©Northeastern Fall 4


Parent
Nodes
□ Tree - a directed, acyclic structure of linked
nodes
□ Parent (ancestor) - any node with at least
one child
□ The blue nodes

CS 5002: Discrete ©Northeastern Fall 5


Children Nodes

□ Tree - a directed, acyclic structure of


linked nodes
□ Child (descendant) - any node with a
parent
□ The blue nodes

CS 5002: Discrete ©Northeastern Fall 6


Sibling
Nodes
□ Tree - a directed, acyclic structure of
linked nodes
□ Siblings - all nodes on the same level
□ The blue nodes

CS 5002: Discrete ©Northeastern Fall 7


Internal
Nodes

□ Tree - a directed, acyclic structure of linked nodes


□ Internal node - a node with at least one children
(except root)
□ All the orange nodes

CS 5002: Discrete ©Northeastern Fall 8


Leaf (External)
Nodes

□ Tree - a directed, acyclic structure of


linked nodes
□ External node - a node without children
□ All the orange nodes

CS 5002: Discrete ©Northeastern Fall 9


Tree Path

□ Tree - a directed, acyclic structure of linked


nodes
□ Path - a sequence of edges that connects
two nodes
□ All the orange nodes

CS 5002: Discrete ©Northeastern Fall 10


Node
Level
□ Node level - 1 + [the number of connections between
the node and the root]
□ The level of node 1 is 1
□ The level of node 11 is 4

CS 5002: Discrete ©Northeastern Fall 11


Node Height

□ Node height - the length of the longest path from the


node to some leaf
□ The height of any leaf node is 0
□ The height of node 8 is 1
□ The height of node 1 is 3
□ The height of node 11 is 0

CS 5002: Discrete ©Northeastern Fall 12


Tree Height

Tree height
□ The height of the root of the
tree, or
□ The number of levels of a tree
-1.
□ The height of the given tree
is 3.

CS 5002: Discrete ©Northeastern Fall 13


What is Not a
Tree?
Problems:
□ Cycles: the only node has a cycle
□ No root: the only node has a parent (itself, because of
the cycle), so there is no root

CS 5002: Discrete ©Northeastern Fall 14


What is Not a
Tree?
Problems:
□ Cycles: there is a cycle in the tree
□ Multiple parents: node 3 has multiple parents on
different levels

CS 5002: Discrete ©Northeastern Fall 15


What is Not a
Tree?
Problems:
□ Cycles: there is an undirected cycle in the tree
□ Multiple parents: node 5 has multiple parents on
different levels

CS 5002: Discrete ©Northeastern Fall 16


What is Not a
Tree?
Problems:
□ Disconnected components: there are two unconnected
groups of nodes

CS 5002: Discrete ©Northeastern Fall 17


Summary: What is a Tree?

□ A tree is a set of nodes, and that set can be empty


□ If the tree is not empty, there exists a special node called a
root
□ The root can have multiple children, each of which can be
the root of a
subtree

CS 5002: Discrete ©Northeastern Fall 18


Section 4

Special
Trees

CS 5002: Discrete ©Northeastern Fall 19


Special
Trees

□ Binary Tree
□ Binary Search Tree
□ Balanced Tree
□ Binary Heap/Priority
Queue
□ Red-Black Tree

CS 5002: Discrete ©Northeastern Fall 20


Binary
Trees
Binary tree - a tree where every node has at most
two children

CS 5002: Discrete ©Northeastern Fall 21


Binary Search
Trees

□ Binary search tree (BST) - a tree where nodes are


organized in a sorted order to make it easier to search
□ At every node, you are guaranteed:

□ All nodes rooted at the left child are smaller than the
current node value
□ All nodes rooted at the right child are smaller than the
current node value

CS 5002: Discrete ©Northeastern Fall 22


Example: Binary Search
Trees?
Binary search tree (BST) - a tree where nodes are organized
in a sorted
order to make it easier to search

□ Left tree is a BST


□ Right tree is not a BST - node 7 is on the left hand-side
of the root node, and yet it is greater than it
CS 5002: Discrete ©Northeastern Fall 23
Example: Using BSTs

Suppose we want to find who has the score


of 15…

CS 5002: Discrete ©Northeastern Fall 24


Example: Using BSTs

Suppose we want to find who has the


score of 15:
□ Start at the root

CS 5002: Discrete ©Northeastern Fall 25


Example: Using
BSTs
Suppose we want to find who has the
score of 15:
□ Start at the root
□ If the score is > 15, go to the left

CS 5002: Discrete ©Northeastern Fall 26


Example: Using
BSTs
Suppose we want to find who has the
score of 15:
□ Start at the root
□ If the score is > 15, go to the left
□ If the score is < 15, go to the right

CS 5002: Discrete ©Northeastern Fall 27


Example: Using
BSTs
Suppose we want to find who has the
score of 15:
□ Start at the root
□ If the score is > 15, go to the left
□ If the score is < 15, go to the right
□ If the score == 15, stop

CS 5002: Discrete ©Northeastern Fall 28


Balanced
Trees
Consider the following two trees. Which tree would it make it
easier for us to search for an element?

CS 5002: Discrete ©Northeastern Fall 29


Balanced
Trees
Consider the following two trees. Which tree would it make it
easier for us to search for an element?

Observation: height is often key for how fast functions on


our trees are.
So, if we can, we want to choose a balanced tree.

CS 5002: Discrete ©Northeastern Fall 30


Tree Balance and
Height
How do we define balance?
□ If the heights of the left and right trees are balanced,
the tree is balanced, so:

CS 5002: Discrete ©Northeastern Fall 31


Tree Balance and Height
How do we define balance?
□ If the heights of the left and right trees are balanced,
the tree is balanced, so:
|(height(left) − height(right))|

CS 5002: Discrete ©Northeastern Fall 32


Tree Balance and Height
How do we define balance?
□ If the heights of the left and right trees are balanced,
the tree is balanced, so:
|(height(left) − height(right))|
□ Anything wrong with this approach?

CS 5002: Discrete ©Northeastern Fall 33


Tree Balance and Height
How do we define balance?
□ If the heights of the left and right trees are balanced,
the tree is balanced, so:
|(height(left) − height(right))|
□ Anything wrong with this approach?
□ Are these trees balanced?

CS 5002: Discrete ©Northeastern Fall 34


Tree Balance and Height

□ Observation: it is not enough to balance only root, all


nodes should be balanced.
□ The balancing condition: the heights of all left and
right subtrees differ by at most 1

CS 5002: Discrete ©Northeastern Fall 35


Example: Balanced
Trees?

□ The left tree is balanced.


□ The right tree is not balanced. The height difference
between nodes 2 and 8 is two.
CS 5002: Discrete ©Northeastern Fall 36
BFS Example

Find element with value 15 in the tree below.


□ BFS: traverse all of the nodes on the same level first, and
then move on to the next (lower) level

CS 5002: Discrete ©Northeastern Fall 37


BFS Example
Find element with value 15 in the tree below using BFS.
□ BFS: traverse all of the nodes on the same level first, and
then move on to the next (lower) level

25 – 10 – 12 – 7 – 8 –
15 – 5
CS 5002: Discrete ©Northeastern Fall 38
DFS Example

Find element with value 15 in the tree below using DFS.


□ DFS: traverse one side of the tree all the way to the leaves,
followed by the other side

CS 5002: Discrete ©Northeastern Fall 39


DFS Example
Find element with value 15 in the tree below using DFS.
□ DFS: traverse one side of the tree all the way to the leaves,
followed by the other side

25 – 10 – 7 –8 – 12 –
15 – 5
CS 5002: Discrete ©Northeastern Fall 40
Tree Traversals
Example
Traverse the tree below, using:
□ Pre-order traversal: 25 – 10 – 7 – 8 – 12
– 15 – 5

CS 5002: Discrete ©Northeastern Fall 41


Tree Traversals
Example
Traverse the tree below, using:
□ Pre-order traversal: 25 – 10 – 7 – 8 – 12
– 15 – 5
□ In-order traversal: 7 – 10 – 8 – 25 – 15 –
12 – 5

CS 5002: Discrete ©Northeastern Fall 42


Tree Traversals
Example
Traverse the tree below, using:
□ Pre-order traversal: 25 – 10 – 7 – 8 – 12
– 15 – 5
□ In-order traversal: 7 – 10 – 8 – 25 – 15 –
12 – 5
□ Post-order traversal: 7 – 8 –10 – 15 –5 –
12 – 25

CS 5002: Discrete ©Northeastern Fall 43


Lets Review
Graphs

CS 5002: Discrete ©Northeastern Fall 44


What is a Graph?

Formal Definition:
□ A graph G is a pair (V, E ) where
□ V is a set of vertices or nodes
□ E is a set of edges that connect vertices
Simply put:
□ A graph is a collection of nodes (vertices) and
edges
□ Linked lists, trees, and heaps are all special cases
of graphs

CS 5002: Discrete ©Northeastern Fall 45


An Example

Here is a graph G = (V,


E)
□ Each edge is a pair (v1, F

where v1 , v are vertices


E

v2), B
in□V V =2 { A , B , C , D, E , F }
□ E = { ( A , B ) , (A, D ) , ( B , C

C), D

(C, D ) , (C, E ) , ( D, E ) } A

CS 5002: Discrete ©Northeastern Fall 46


Terminology: Undirected
Graph

□ Two vertices u and v are adjacent in an undirected graph G if


{u, v } is an edge in G
□ edge e = {u, v } is incident with vertex u and vertex v
□ The degree of a vertex in an undirected graph is the
number of edges incident with it
□ a self-loop counts twice (both ends count)
□ denoted with deg(v)

CS 5002: Discrete ©Northeastern Fall 47


Terminology: Directed
Graph

□ Vertex u is adjacent to vertex v in a directed graph G if


(u, v) is an edge in G
□ vertex u is the initial vertex of (u, v)
□ Vertex v is adjacent from vertex u
□ vertex v is the terminal (or end) vertex of (u, v)
□ Degree
□ in-degree is the number of edges with the vertex as the
terminal vertex
□ out-degree is the number of edges with the vertex as the
initial vertex

CS 5002: Discrete ©Northeastern Fall 48


CS 5002: Discrete ©Northeastern Fall 49
Kinds of Graphs

□ directed vs
undirected
□ weighted vs
unweighted
□ simple vs non-
simple
□ sparse vs dense
□ cyclic vs acyclic
□ labeled vs unlabeled

CS 5002: Discrete ©Northeastern Fall 50


Directed vs
Undirected

□ Undirected if edge (x, y) F

implies edge (y, x).


E

□ otherwise directed
C

□ Roads between cities


D

are usually undirected F

(go both ways) E

□ Streets in cities tend


to be directed (one-
C

D
way) A

CS 5002: Discrete ©Northeastern Fall 51


Weighted vs Unweighted

□ Each edge or vertex is F


6

20 E
assigned a numerical B
10
value (weight). 8
4

□ A road network 15 C
2

might be labeled D

with: A
7

□ length
□ drive-time
□ speed-limit
□ In an unweighted graph,
there is no distinction
between edges.

CS 5002: Discrete ©Northeastern Fall 52


Simple vs Not
simple

□ Some kinds of edges F

E
make working with B

graphs complicated
□ A self-loop is an edge C

(x, x) (one vertex). D

□ An edge (x, y) is a A

multiedge if it occurs
more than once in a
graph.

CS 5002: Discrete ©Northeastern Fall 53


Sparse vs
Dense

□ Graphs are sparse when a F

E
small fraction of vertex B

pairs have edges between


them C

□ Graphs are dense when a D

large fraction of vertex A

pairs have edges


□ There’s no formal
distinction between
sparse and dense

CS 5002: Discrete ©Northeastern Fall 54


Cyclic vs
Acyclic

□ An acyclic graph F

E
contains no cycles B

□ A cyclic graph contains a C

cycle D

□ Trees are connected, A

acyclic, undirected graphs E

□ Directed acyclic graphs


are called DAGs C

CS 5002: Discrete ©Northeastern Fall 55


Labeled vs Unlabeled

□ Each vertex is assigned a


unique name or
identifier in a labeled
graph
□ In an unlabeled graph,
there
are no named nodes
□ Graphs usually have
names— e.g., city names
in a transportation
network
□ We might ignore names in
graphs to determine if
they are isomorphic
(similar in structure)
CS 5002: Discrete ©Northeastern Fall 56

You might also like