Data Structures: Trees and Graphs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Data Structures:

Trees and Graphs


Trees

¤ A tree is a hierarchical data structure composed of nodes.


¤ Root: the top-most node (unlike real trees, trees in computer science
grow downward!). Every (non-empty) tree has one.
¤ Parent: the node connected directly above the current one. Every node
(except for the root) has one.
¤ Child: a node connected below the current one. Each node can have 0
or more.
¤ Leaf: a node that has no children.
¤ Depth/Level: the length of the path (edges) from the root to a node
(depth/level of the root is 0).
¤ Tree Height: the maximum depth from of any node in the tree.

¤ A tree commonly used in computing is a binary tree.


¤ A binary tree consists of nodes that have at most 2 children.
¤ Commonly used in: data compression, file storage, game trees

2
Binary Tree Example

84

65 96

24 50 53

48 37
Which node is the root?
Which nodes are the leaves?
Which nodes are internal nodes?
What is the height of this tree?

3
Binary Tree Example

84 Level 0

65 96 Level 1

24 50 53 Level 2

48 37 Level 3

The root contains the data value 84.


There are 4 leaves in this binary tree: nodes containing 48, 37, 50, 53.
There are 4 internal nodes in this binary tree: containing 84, 65, 96, 24
This binary tree has height 3 – considering root is at level 0,
the length of the longest path from root to a leaf is 3
4
Binary Trees: A recursive structure!

¤ The yellow node with the key 65 can be viewed as the


root of the left subtree, which in turn has
¤ a left subtree of blue nodes
¤ a right subtree of orange nodes 84
¤ In general, Binary Trees can be:
¤ Empty
65 96
¤ A root node with
¤ a left binary tree 24 50 53
¤ a right binary tree

48 37
5
Binary Trees: Implementation

¤ A common implementation of binary trees uses nodes


¤ Each node has a “left” node and a “right” node.

¤ How to represent these nodes and pointers? With a Class (like a


Struct…)

65 Level  0

24 50 Level  1

48 37 14 Level  2

6
Implement Using a List

¤ We could also use a list to implement binary trees. For example:


0    1    2    3    4    5    6    7
- 65 24 50 48 37 - 14

65 Level  0

24 50 Level  1

48 37 14 Level  2

7
Binary Search Tree (BST)

¤ A binary search tree (BST) is a binary tree with no


duplicate nodes that imposes an ordering on its nodes.

¤ BST ordering invariant: At any node n with value k,


¤ all values of nodes in the left subtree of n are strictly less than
k
¤ all values of nodes in the right subtree of n are strictly greater
than k

8
Example: Binary Search Tree
BST ordering invariant: At any node with value k,
all values of elements in the left subtree are strictly less than k and
all values of elements in the right subtree are strictly greater than k
(assuming that there are no duplicates in the tree)

4 8

1 6 9
Example: Is this a BST?

4 4

1 7 1 7
3
6 8
6 8

3 9 9

no yes

10
Insertion in a BST

¤ For each data value that you wish to insert into the binary
search tree:
¤ If you reach an empty tree (must test this first, why?), create
a new leaf node with your value at that location
¤ Recursively search the BST for your value until you either find
it or reach an empty tree
¤ If you find the value, throw an exception since duplicates
are not allowed

11
Insertion Example
¤ Insert: 84, 41, 96, 24, 37, 50, 13, 98

84

41 96

24 50 98

13 37

12
Binary Search Tree Complexity
1 Tree 2

Tree 1 6
4

6
4 8

1 5 9
8

O(log n) O(n)
9
13
Relationship Data

From this…

14
Relationship Data
To this…

C
B
F
D
A G
E

15
Graphs

¤ A graph is a data structure that contains of a set of vertices


and a set of edges which connect pairs of the vertices.
¤ A vertex (or node) can be connected to any number of other
vertices using edges.
¤ An edge may be bidirectional or directed (one-way).
¤ An edge may have a weight on it that indicates a cost for
traveling over that edge in the graph.

¤ Unlike trees, graphs can contain cycles


¤ In fact, a tree is an acyclic graph

¤ Applications: computer networks, transportation systems,


social networks

16
Example Graphs

Vertex A A

C C
B B

D D

Undirected Directed

17
Graph Implementation

¤ We usually represent graphs using a table (2d list) where


each column and row is associated with a specific
vertex. This is called an adjacency matrix.

¤ A separate list of vertices shows which vertex name (city,


person, etc.) is associated with each index

¤ The values of the 2d list are the weights of the edges


between the row vertices and column vertices
¤ If there is not an edge between the two vertices, we use
infinity, or None, to represent that
Graph Representation
A 7 weight A 7
6 6 2
C C
4 4
B B 9
3 3
5 5
D D

to to
from
A B C D from
A B C D
A 0 6 7 5 A 0 6 7 5
B 6 0 4 ∞ B ∞ 0 4 ∞
C 7 4 0 3 C 2 ∞ 0 3
D 5 ∞ 3 0 D ∞ ∞ 9 0

19
Graphs in Python
A 7
6
C
4
B
3
5
D

to
from
A B C D
vertices = ['A', 'B', 'C', 'D']
A 0 6 7 5 graph =
B 6 0 4 ∞ [ [ 0, 6, 7, 5 ],
[ 6, 0, 4, None],
C 7 4 0 3 [ 7, 4, 0, 3],
D 5 ∞ 3 0 [ 5, None, 3, 0] ]

20
Graph Algorithms

¤ Lots! Here are some examples.

¤ There are algorithms to search graphs efficiently for a value


¤ Breadth-first search and Depth-first search

¤ There are algorithms to compute the shortest path between


a start vertex and all the others
¤ Dijkstra’s algorithm

¤ There are algorithms for operations research, which can be


used to solve network flow problems
¤ For example, how to efficiently distribute water through a system
of pipes

21
Shortest Path (Dijkstra's algorithm)
¤ Assign every node an initial distance (0 to the source, ∞ for all
others); mark all nodes as unvisited

¤ While there are unvisited nodes:


¤ select unvisited node with smallest distance (current)
¤ consider all unvisited neighbors of current node:
¤ compute distance to each neighbor from current node
¤ if less than current distance, replace with new distance
¤ mark current node as visited (and never evaluate again)

22
Dijkstra example

A B C D E F G
2
D E 0 ∞ ∞ ∞ ∞ ∞ ∞

17 A->
6 7
19

B 4 F

12
2 5 6

A C G
3 4

23
Dijkstra example

A B C D E F G
2
0 ∞ ∞ ∞ ∞ ∞ ∞
D E
A-> 12 3 ∞ 17 ∞ ∞
6 17 7
C-> 5 22 17 8 7
19
B-> 11 17 8 7
B 4 F
G-> 11 17 8
12
2 5 6 F-> 11 15

A C G D-> 13
3 4 E 0 5 3 11 13 8 7

What does this assume?

24

You might also like