tree
TRANSCRIPT
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
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
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
• 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.
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
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.
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
Inorder traversal• In inorder, the root is visited in the middle• Here’s an inorder traversal to print out all
the elements in the binary tree:
Postorder traversal
• In Postorder, the root is visited last• Here’s a Postorder traversal to print out all
the elements in the binary tree:
Arithmetic Expression Tree• Binary tree for an arithmetic expression
– internal nodes: operators– leaves: operands
• Example: arithmetic expression tree for the expression ((2 (5 - 1)) + (3 2))
+
-2
5 1
3 2
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
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
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
Binary Search Tree – Insertion
• Algorithm1. 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
Binary Search Tree – Deletion
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal nodea) Replace with largest value Y on left sub tree OR smallest value Z on right sub treeb) Delete replacement value (Y or Z) from sub tree
Observation
- O( log(n) ) operation for balanced tree
Example Deletion (Leaf)
• Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25, delete
5
10
30
2 45
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
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
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);
Recursive Search of Binary TreeNode *Find( Node *n, int key) {
if (n == NULL) // Not foundreturn( n );
else if (n->data == key) // Found itreturn( n );
else if (n->data > key) // In left subtreereturn Find( n->left, key );
else // In right subtreereturn Find( n->right, key );
}
Node * n = Find( root, 5);
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
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
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
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 verticesE(G): a set of edges (pairs of vertices)
Directed vs. undirected graphs
• When the edges in a graph have no direction, the graph is called undirected
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 !!
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
Graph terminology
• What is the number of edges in a complete directed graph with N vertices?
N * (N-1)
Graph terminology
• What is the number of edges in a complete undirected graph with N vertices?
N * (N-1) / 2
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