Transcript Slide 1

Advanced Algorithms Analysis
and Design
Lecture 8
(Continue Lecture 7…..)
Elementry Data Structures
By
Engr Huma Ayub Vine
12-5 TREES
A tree consists of a finite set of elements, called nodes
(or vertices) and a finite set of directed lines, called arcs,
that connect pairs of the nodes.
12.2
Figure 12.20 Tree representation
As We can divided the vertices in a tree into three categories:
the root, leaves and the internal nodes. Table 12.1 shows the
number of outgoing and incoming arcs allowed for each
type of node.
12.3
Each node in a tree may have a subtree. The subtree of each
node includes one of its children and all descendents of that
child. Figure 12.21 shows all subtrees for the tree in Figure
12.20.
12.4
Figure 12.21 Subtrees
lambda
lambda
mu
nu
nu
Figure: Two distinct rooted trees
mu
The two binary trees as shown in figure are not the same: in the first case b is the left child of
a and the right child is missing, whereas in the second case b is the right child of a and the left
child is missing
a
a
b
c
b
d
c
Figure. Two distinct binary trees
d
Trees
• A tree is an acyclic, connected and undirected
graph. Equivalently, a tree may be defined as an
undirected graph in which there exists exactly
one path between any given pair of nodes.
• Since a tree is a kind of graph, the same
representations used to implement graphs can
be used to implement trees.
• Trees have a number of simple properties, of which the
following are perhaps the most important:- A tree with n nodes
has exactly n - 1 edges.
If a single edge is removed from a tree, then the resulting
graph is no longer connected.
Figure (a) Trees with 4 nodes
Height, Depth and Level
• Height and level, for instance, are similar
concepts, but not the same.
- The height of a node is the number of edges in the
longest path from the node in question to a leaf.
- The depth of a node is the number of edges in the
path from the root to the node in question.
- The level of a node is equal to the height of the root
of the tree minus the depth of the node concerned.
alpha
gamma
beta
delta
epsilon
zeta
Node
Height
Depth
Level
alpha
2
0
2
beta
1
1
1
gamma
0
1
1
delta
0
2
0
epsilon
0
2
0
zeta
0
2
0
Figure: Height, depth and level
Figure: A rooted tree
Figure: Height, depth and level
12-6 BINARY TREES
A binary tree is a tree in which no node can have more
than two subtrees. In other words, a node can have zero,
one or two subtrees.
12.10
Figure 12.22 A binary tree
Recursive definition of binary trees
We can also define a structure or an ADT recursively. The
following gives the recursive definition of a binary tree. Note
that, based on this definition, a binary tree can have a root,
but each subtree can also have a root.
12.11
Figure 12.23 shows eight trees, the first of which is an empty
binary tree (sometimes called a null binary tree).
12.12
Figure 12.23 Examples of binary trees
Operations on binary trees
The six most common operations defined for a binary tree are tree (creates an empty
tree), insert, delete, retrieve, empty and traversal. We discuss binary tree traversal in
this section.
Binary Tree Traversal
A binary tree traversal requires that each node of the tree be processed once and only
once in a predetermined sequence.
The two general approaches to the traversal sequence are
1) Depth-first (deeper in the tree before exploring siblings)
2) Breadth-first traversal (Trees can also be traversed in level-order)
12.13
Depth First Transversal
ROOT LEFT RIGHT
12.14
LEFT ROOT RIGHT
Figure Depth-first traversal of a binary tree
LEFT RIGHT ROOT
Breadth-first Traversal
Trees can also be traversed in level-order, where we visit every node on a level before going
to a lower level. This is also called Breadth-first traversal.
Example
Preorder
Example
Figure shows how we visit each node in a tree using preorder
traversal i.e A, B, C, D, E, F.
Binary tree applications
Binary trees have many applications in computer science. In
this section we mention only two of them: Huffman coding
and expression trees.
Huffman coding
Huffman coding is a compression technique that uses binary
trees to generate a variable length binary code from a string
of symbols. We discuss Huffman coding in detail in next
lectures.
12.17
Expression trees
An arithmetic expression can be represented in three
different formats: infix, postfix and prefix. In an infix
notation, the operator comes between the two operands. In
postfix notation, the operator comes after its two operands,
and in prefix notation it comes before the two operands.
These formats are shown below for addition of two operands
A and B.
12.18
12.19
Figure 12.27 Expression tree
12-7 BINARY SEARCH TREES
A binary search tree (BST) is a binary tree with one
extra property: the key value of each node is greater
than the key values of all nodes in each left subtree and
smaller than the value of all nodes in each right subtree.
Figure 12.28 shows the idea.
12.20
Figure 12.28 Binary search tree (BST)
Example 12.12
Figure 12.29 shows some binary trees that are BSTs and some
that are not. Note that a tree is a BST if all its subtrees are BSTs
and the whole tree is also a BST.
12.21
Figure 12.29 Example 12.12
A very interesting property of a BST is that if we apply the
inorder traversal of a binary tree, the elements that are visited
are sorted in ascending order. For example, the three BSTs in
Figure 12.29, when traversed in order, give the lists
(3, 6, 17), (17, 19) and (3, 6, 14, 17, 19).
LEFT ROOT RIGHT
i
An inorder traversal of a BST creates a list that is sorted
in ascending order.
12.22
Binary search tree ADTs
The ADT for a binary search tree is similar to the one we
defined for a general linear list with the same operation. As a
matter of fact, we see more BST lists than general linear lists
today. The reason is that searching a BST is more efficient
than searching a linear list: a general linear list uses
sequential searching, but BSTs use a version of binary
search.
12.23
BST implementation
BSTs can be implemented using either arrays or linked lists.
However, linked list structures are more common and more
efficient. The implementation uses nodes with two pointers,
left and right.
12.24
Figure 12.31 A BST implementation
Trees
On a computer any rooted tree may be
represented using nodes of the following type.
type treenode1 = record
value: information
eldest - child, next - sibling: ↑ treenode1
Trees
• We shall often have occasion to use binary
trees. In such a tree, each node can have 0, 1, or
2 children.
• In fact, we almost always assume that a node
has two pointers, one to its left and one to its
right, either of which can be nil.
.
Trees
• If each node of a rooted tree can have no more than k
children, we say it is a k-ary tree.
• One obvious representation uses nodes of the
type k-ary-node = record
value: information
child: array [i. . k] of ↑k-ary-node
• In the case of a binary tree we can also define
type binary-node = record
value: information
left-child, right-child : ↑binary-node
function Binarysearch (x,r)
{The pointer r points to the root of a search tree. The
function searches for the value x in this tree and
returns a pointer to the node containing x. If x is
missing, the function returns nil.}
if r = nil then {x is not in the tree.}
return nil
else if x = r↑. Value then return r
else if x < r ↑. Value then return search (x,r ↑.left-child)
else return search(x,r ↑.right-child)
Trees
• For a tree operations as searches or the addition
and deletion of nodes take a O(log n) in the worst
case, where n is the number of nodes in the tree.