2. Linear Lists and Trees
• Linear lists (arrays, linked list)are useful for serially ordered
data
– (e1,e2,e3,…,en)
– Days of week
– Months in a year
– Students in a class
• Trees are useful for hierarchically ordered data
– directory structure
– Parenthetical Listing
– family trees:
• all descendants of a particular person
• all ancestors born after year 1800 of a particular person
4. Tree
A tree is a collection of Nodes
and a finite set of directed lines called branches, that connect the nodes.
Each node can have 0 or more children
A node can have at most one parent
Tree defined recursively
A tree consists of a distinguished node r, called the root, and zero or more
non-empty (sub) trees T1, T2, …, Tk each of whose roots are connected by
a directed edge from r.
A tree is a collection of N nodes, one of which is the root and N-1 edges.
The number of branches associated with a node is the degree of the
node.
Binary tree
Tree with 0–2 children per node
5. Terminology
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
The root of each sub tree is said to be a child of r and r is
said to be the parent of each sub tree root.
Siblings nodes with the same parent
6. • Depth (of node): the length of the unique path from the root to a
node.
• Depth (of tree): The depth of a tree is equal to the depth of its
deepest leaf.
• Height (of node): the length of the longest path from a node to a
leaf.
– All leaves have a height of 0
– The height of the root is equal to the depth of the tree
• When the branch is directed toward the node, it is indegree branch.
• When the branch is directed away from the node, it is an outdegree
branch.
• The sum of the indegree and outdegree branches is the degree of the
node.
7.
8.
9. Recursive definition of a tree
• A tree is a set of nodes that either:
- is empty or
- has a designated node, called the root, from
which hierarchically descend zero or more sub
trees, which are also trees.
9
10. Binary trees
• A binary tree is a tree in which no node can
have more than two subtrees; the maximum
outdegree for a node is two.
• In other words, a node can have zero, one, or
two subtrees.
• These subtrees are designated as the left
subtree and the right subtree.
11.
12.
13.
14. Picture of a binary tree
a
b c
d e
g h i
l
f
j k
16. Preorder traversal
• In preorder, the root is visited first
• Here’s a preorder traversal to print out all
the elements in the binary tree:
A
B C
D E F G
A B D E C F G
34. Binary Search Trees
• Binary Search Tree Property:
– the value stored at the root of a sub tree is greater than any value in its
left sub tree and less than any value in its right sub tree!
• Where is the smallest element?
– leftmost element
• Where is the largest element?
– rightmost element
• How to build & maintain binary trees?
– Insertion
– Deletion
• Maintain key property (invariant)
– Smaller values in left sub tree
– Larger values in right sub tree
35. Binary Search Trees
• Key property
– Value at node
• Smaller values in left subtree
• Larger values in right subtree
– Example
• X > Y
• X < Z
Y
X
Z
36. Binary Search Trees
• Examples
Binary search
trees
Not a binary
search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
37. Binary Search Tree – Insertion
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left sub tree for Y
4. If X > Y, insert new leaf X as new right sub tree for Y
• Observations
– O( log(n) ) operation for balanced tree
39. Binary Search Tree – Deletion
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left sub tree
OR smallest value Z on right sub tree
b) Delete replacement value (Y or Z) from sub tree
Observation
- O( log(n) ) operation for balanced tree
41. Example Deletion (Internal Node)
• Delete ( 10 )
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10
with largest
value in left sub
tree
Replacing 5 with
largest value in
left sub tree
Deleting leaf
42. Example Deletion (Internal Node)
• Delete ( 10 )
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10
with smallest
value in right
sub tree
Deleting leaf Resulting tree
43. Iterative Search of Binary Tree
Node *Find( Node *n, int key) {
while (n != NULL) {
if (n->data == key) // Found it
return n;
if (n->data > key) // In left subtree
n = n->left;
else // In right subtree
n = n->right;
}
return null;
}
Node * n = Find( root, 5);
44. Recursive Search of Binary Tree
Node *Find( Node *n, int key) {
if (n == NULL) // Not found
return( n );
else if (n->data == key) // Found it
return( n );
else if (n->data > key) // In left subtree
return Find( n->left, key );
else // In right subtree
return Find( n->right, key );
}
Node * n = Find( root, 5);
45. Example Binary Searches
• Find ( root, 2 )
5
10
30
2 25 45
5
10
30
2
25
45
10 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
root
46. Example Binary Searches
• Find (root, 25 )
5
10
30
2 25 45
5
10
30
2
25
45
10 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
47. Types of Binary Trees
• Degenerate – only one child
• Balanced – “mostly” two children
• Complete – always two children
Degenerate
binary tree
Balanced
binary tree
Complete
binary tree
48. Introduction to Graph
• What is a 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
• A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
49. Directed vs. undirected graphs
• When the edges in a graph have no direction,
the graph is called undirected
50. Directed vs. undirected graphs (cont.)
• When the edges in a graph have a direction,
the graph is called directed (or digraph)
Warning: if the graph is directed, the order of the vertices in each edge is
important !!
52. Graph terminology
• Adjacent nodes: two nodes are adjacent if they are connected
by an edge
– 5 is adjacent to 7
– 7 is adjacent from 5
• Path: a sequence of vertices that connect two nodes in a
graph
• Complete graph: a graph in which every
vertex is directly connected to
every other vertex
53. Graph terminology
• What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
54. Graph terminology
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
56. Graph implementation
• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to represent
the edges