Week 5-6-7-8 DS
Week 5-6-7-8 DS
return -1;
}
Cont….
• Using a while (or a for) loop, the definition of the method seqSearch can also be
written without the break statement as:
• Suppose that the second element in the array list contains the variable key, then we
have performed two comparisons to find the key.
• Carry on the same analysis till the key is contained in the last element of the array list.
In this case, we have performed N comparisons (N is the size of the array list) to find
the key.
• Finally if the key is NOT in the array list, then we would have performed N
comparisons and the key is NOT found and we would return -1.
Binary Search Algorithm
• If search item > middle element of list, search second half of the
list
key = 89
key = 34
Cont….
key = 22
Performance of Binary Search Algorithm
• The sum of the indegree and outdegree branches is the degree of the node.
• If the tree is not empty, the first node is called the root.
Cont….
• The indegree of the root is, by definition, zero.
• With the exception of the root, all of the nodes in a tree must have an
indegree of exactly one; that is, they may have only one predecessor.
• All nodes in the tree can have zero, one, or more branches leaving them;
that is, they may have outdegree of zero, one, or more.
Cont….
• A leaf is any node with an outdegree of zero, that is, a node with no
successors.
• An ancestor is any node in the path from the root to the node.
• A descendant is any node in the path below the parent node; that is, all
nodes in the paths from a given node to a leaf are descendants of that
node.
Cont….
• A path is a sequence of nodes in which each node is adjacent to the next
node.
• The level of a node is its distance from the root. The root is at level 0, its
children are at level 1, etc. …
• The height of the tree is the level of the leaf in the longest path from the
root plus 1. By definition the height of any empty tree is -1.
• A subtree is any connected structure below the root. The first node in the
subtree is known is the root of the subtree.
Cont….
Cont….
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 subtrees, which are also trees.
Tree Representation
• Properties
• Binary Tree Traversals
• Expression Trees
• Huffman Code
Cont….
• A binary tree is a tree in which no node can have more than two subtrees;
the maximum outdegree for a node is two.
• These subtrees are designated as the left subtree and the right subtree.
Cont….
Cont….
Some Properties of Binary Trees
• The minimum height of a binary tree is determined as follows:
H min = [ log 2 N ] + 1
For instance, if there are three nodes to be stored in the binary tree (N=3) then
Hmin=2.
Cont….
• The formula for the maximum number of nodes is derived from the fact that
each node can have only two descendents. Given a height of the binary tree,
H, the maximum number of nodes in the tree is given as follows:
N max = 2 - 1 H
Cont….
• The children of any node in a tree can be accessed by following only one
branch path, the one that leads to the desired node.
• The nodes at level 1, which are children of the root, can be accessed by
following only one branch; the nodes of level 2 of a tree can be accessed
by following only two branches from the root, etc.
• The balance factor of a binary tree is the difference in height between its
left and right subtrees:
Balance of the Tree
Some Properties of Binary Trees
• In the balanced binary tree (definition of Russian mathematicians
Adelson-Velskii and Landis) the height of its subtrees differs by no more
than one (its balance factor is -1, 0, or 1), and its subtrees are also
balanced.
Complete & Nearly Complete Binary Tress
• A complete tree has the maximum number of entries for its height. The
maximum number is reached when the last level is full.
• A tree is considered nearly complete if it has the minimum height for its
nodes and all nodes in the last level are found on the left
Cont….
Binary Search Tree Property
• Binary search tree property:
• If y is in left subtree of x,
then key [y] ≤ key [x]
• If y is in right subtree of x,
then key [y] ≥ key [x]
3 7
2 5 9
Cont….
• Support many dynamic set operations
• SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT,
DELETE
3 7
2 5 9
Searching for a Key
Given a pointer to the root of a tree and a key k:
• Return a pointer to a node with key k if one exists
• Otherwise return NIL
Idea: Starting at the root, trace down a path by comparing k with the key of the current node.
ü if the keys are equal: we have found the key
ü if k < key[x] search in the left subtree of x
ü if k > key[x] search in the right subtree of x
3 7
2 4 9
Example: Tree–Search
15
• Search for key 13:
6 18 • 15 ® 6 ® 7 ® 13
3 7 17 20
2 4 13
9
Searching for a Key
Alg: TREE-SEARCH(x, k)
5
1. if x = NIL or k == key [x]
3 7
2. then return x
2 4 9
3. if k < key [x]
4. then return TREE-SEARCH(left [x], k )
5. else return TREE-SEARCH(right [x], k )
Alg: TREE-MINIMUM(x) 6 18
15 15
5 16 5 16
3 12 20 3 12 20
10 13 18 23 10 18 23
6 delete 6
7 7
Cont….
• Case 2: z has one child
• Delete z by making the parent of z point to z’s child, instead of to z
15 delete 15
5 16 5 20
3 12 20 3 12 18 23
10 13 18 23 10
6 6
7 7
Cont….
• Case 3: z has two children
• z’s successor (y) is the minimum node in z’s right subtree
• y has either no children or one right child (but no left child)
• Delete y from the tree (via Case 1 or 2)
• Replace z’s key and satellite data with y.
15 15
delete z
5 16 6 16
3 12 20 3 12 20
10 13 18 23 10 13 18 23
6 7
7
Cont….
Ø Tree is a non linear data structure to present data in
hierarchical form.
Ø It is also called acyclic data structure.
Ø The main component of a tree are node , branches, level,
family (subtree) and path.
Ø A node may be used as root, parent, left, brother,
predecessor , successor and leaf node.
Ø If every node of a tree have 0, 1 or 2 child it is called
binary tree.
Ø There ar different traversing method of tree such as
inorder, pre order and post order.
Cont….
ØBinary search tree is a special binary tree which is designed to
make the search of elements or keys in linear times.
ØFor each current parent node key, the values of all nodes of left
sub tree are less and of right sub tree are greater than from
current parent.
ØOperations on binary search trees:
• SEARCH O(h)
• MINIMUM O(h)
• MAXIMUM O(h)
• INSERT O(h)
• DELETE O(h)
• PREDECESSOR O(h)
• SUCCESOR O(h)
Graphs
Instructor: Sameer Malik
Introduction to Graphs
Graphs are important discrete structures because they are
a flexible mathematical model for many application
problems.
Hypertext Circuits
Introduction to Graphs
Ø A graph G = ( V , E ) consists of
o a finite set of vertices V (or nodes) and
o E, a binary relation on V called edges.
Adjacent Vertices
1&2
1&3
1&4
2&4
in: 1 in: 1
out: 3 out: 1
out: 2 out: 0
Σin-degree(v) + Σout-degree(v) = | E |
Where | E | means the cardinality of the set E, i.e.
the number of edges.
Σ degree(v) = 2| E |
Where | E | means the cardinality of the set E, i.e.
the number of edges.
ì
A( v , w ) = í
1 if (v, w) Î E
î0 otherwise