advanced algorithms analysis and design lecture 8 (continue lecture 7…..) elementry data...

29
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Upload: abner-dawson

Post on 27-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Advanced Algorithms Analysis and Design

Lecture 8

(Continue Lecture 7…..)

Elementry Data Structures

By

Engr Huma Ayub Vine

Page 2: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.2

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.

Figure 12.20 Tree representation

Page 3: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.3

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 eachtype of node.

Page 4: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.4

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.

Figure 12.21 Subtrees

Page 5: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

lambda

mu nu

lambda

nu mu

Figure: Two distinct rooted trees

Page 6: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

a

b

c d

a

b

cd

Figure. Two distinct binary trees

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 leftchild is missing

Page 7: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Trees• A tree is an acyclic, connected and undirectedgraph. Equivalently, a tree may be defined as anundirected graph in which there exists exactlyone path between any given pair of nodes.• Since a tree is a kind of graph, the samerepresentations used to implement graphs canbe used to implement trees.

Figure (a) Trees with 4 nodes

• Trees have a number of simple properties, of which thefollowing 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 resultinggraph is no longer connected.

Page 8: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

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.

Page 9: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

alphaNode Height Depth Level

beta

delta epsilon

gamma

zeta

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

Page 10: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.10

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.

Figure 12.22 A binary tree

Page 11: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.11

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.

Page 12: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.12

Figure 12.23 shows eight trees, the first of which is an empty binary tree (sometimes called a null binary tree).

Figure 12.23 Examples of binary trees

Page 13: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.13

Operations on binary treesThe 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.

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)

Binary Tree Traversal

Page 14: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.14Figure Depth-first traversal of a binary tree

ROOT LEFT RIGHT LEFT ROOT RIGHT LEFT RIGHT ROOT

Depth First Transversal

Page 15: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

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

Page 16: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Example

Figure shows how we visit each node in a tree using preorder traversal i.e A, B, C, D, E, F.

Page 17: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.17

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.

Page 18: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.18

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.

Page 19: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.19Figure 12.27 Expression tree

Page 20: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.20

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 andsmaller than the value of all nodes in each right subtree. Figure 12.28 shows the idea.

Figure 12.28 Binary search tree (BST)

Page 21: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.21

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.

Figure 12.29 Example 12.12

Page 22: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.22

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).

An inorder traversal of a BST creates a list that is sorted in ascending order.

i

LEFT ROOT RIGHT

Page 23: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.23

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.

Page 24: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

12.24

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.

Figure 12.31 A BST implementation

Page 25: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Trees On a computer any rooted tree may berepresented using nodes of the following type.

type treenode1 = record

value: information

eldest - child, next - sibling: ↑ treenode1

Page 26: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Trees• We shall often have occasion to use binarytrees. In such a tree, each node can have 0, 1, or

2 children.• In fact, we almost always assume that a nodehas two pointers, one to its left and one to itsright, either of which can be nil.

.

Page 27: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Trees• If each node of a rooted tree can have no more than kchildren, we say it is a k-ary tree.• One obvious representation uses nodes of the

type k-ary-node = recordvalue: informationchild: array [i. . k] of ↑k-ary-node

• In the case of a binary tree we can also definetype binary-node = record

value: informationleft-child, right-child : ↑binary-node

Page 28: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

function Binarysearch (x,r)

{The pointer r points to the root of a search tree. Thefunction searches for the value x in this tree andreturns a pointer to the node containing x. If x ismissing, the function returns nil.}

if r = nil then {x is not in the tree.}

return nilelse 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)

Page 29: Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

Trees• For a tree operations as searches or the addition and deletion of nodes take a O(log n) in the worstcase, where n is the number of nodes in the tree.