Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure...

51

Transcript of Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure...

Page 1: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Page 2: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Page 3: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Trees Linear access time of linked lists is

prohibitiveDoes there exist any simple data structure for

which the running time of most operations (search, insert, delete) is O(log N)?

TreesBasic conceptsTree traversalBinary treeBinary search tree and its operations

Page 4: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Trees A tree is a collection of nodes

The collection can be empty(recursive definition) If not empty, a tree

consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r

Page 5: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Some Terminologies

Child and Parent Every node except the root has one parent  A node can have an zero or more children

Leaves Leaves are nodes with no children

Sibling nodes with same parent

Page 6: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More Terminologies Path

A sequence of edges Length of a path

number of edges on the path Depth of a node

length of the unique path from the root to that node Height of a node

length of the longest path from that node to a leaf all leaves are at height 0

The height of a tree = the height of the root = the depth of the deepest leaf

Ancestor and descendant If there is a path from n1 to n2 n1 is an ancestor of n2, n2 is a descendant of n1 Proper ancestor and proper descendant

Page 7: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Example: UNIX Directory

Page 8: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Example: Expression Trees

Leaves are operands (constants or variables) The internal nodes contain operators Will not be a binary tree if some operators are not

binary

Page 9: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Tree Traversal Used to print out the data in a tree in a

certain order Pre-order traversal

Print the data at the rootRecursively print out all data in the left

subtreeRecursively print out all data in the right

subtree

Page 10: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Preorder, Postorder and Inorder Preorder traversal

node, left, rightprefix expression

○ ++a*bc*+*defg

Page 11: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Preorder, Postorder and Inorder Postorder traversal

left, right, nodepostfix expression

○ abc*+de*f+g*+

Inorder traversalleft, node, rightinfix expression

○ a+b*c+d*e+f*g

Page 12: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Example: Unix Directory TraversalPreOrder PostOrder

Page 13: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Preorder, Postorder and Inorder Pseudo Code

Page 14: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Binary Trees A tree in which no node can have more than two

children

The depth of an “average” binary tree is considerably smaller than N, even though in the worst case, the depth can be as large as N – 1.

Generic binary tree

Worst-casebinary tree

Page 15: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Node Struct of Binary Tree Possible operations on the Binary Tree

ADTParent, left_child, right_child, sibling, root,

etc

ImplementationBecause a binary tree has at most two

children, we can keep direct pointers to them

Page 16: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Convert a Generic Tree to a Binary Tree

Page 17: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Binary Search Trees (BST) A data structure for efficient searching,

insertion and deletion Binary search tree property

For every node XAll the keys in its left

subtree are smaller than the key value in X

All the keys in its right subtree are larger than the key value in X

Page 18: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Binary Search Trees

A binary search tree Not a binary search tree

Page 19: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Binary Search Trees

Average depth of a node is O(log N) Maximum depth of a node is O(N)

The same set of keys may have different BSTs

Page 20: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Searching BST If we are searching for 15, then we are

done. If we are searching for a key < 15, then we

should search in the left subtree. If we are searching for a key > 15, then we

should search in the right subtree.

Page 21: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Page 22: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Searching Find X: return a pointer to the node that has

key X, or NULL if there is no such node

Time complexity: O(height of the tree)

BinaryNode * BinarySearchTree::Find(const float &x, BinaryNode *t) const

{

if (t == NULL)

return NULL;

else if (x < t->element)

return Find(x, t->left);

else if (t->element < x)

return Find(x, t->right);

else

return t; // match

}

Page 23: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

In-order Traversal of BST Inorder traversal of BST prints out all the

keys in sorted order

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20

Page 24: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

findMin/ findMax Goal: return the node containing the smallest

(largest) key in the tree Algorithm: Start at the root and go left (right) as

long as there is a left (right) child. The stopping point is the smallest (largest) element

Time complexity = O(height of the tree)

BinaryNode * BinarySearchTree::FindMin(BinaryNode *t) const

{

if (t == NULL)

return NULL;

if (t->left == NULL)

return t;

return FindMin(t->left);

}

Page 25: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Insertion Proceed down the tree as you would with a find If X is found, do nothing (or update something) Otherwise, insert X at the last spot on the path

traversed

Time complexity = O(height of the tree)

Page 26: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Deletion When we delete a node, we need to

consider how we take care of the children of the deleted node.This has to be done such that the property

of the search tree is maintained.

Page 27: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Deletion under Different Cases

Case 1: the node is a leafDelete it immediately

Case 2: the node has one childAdjust a pointer from the parent to bypass that

node

Page 28: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Deletion Case 3 Case 3: the node has 2 children

Replace the key of that node with the minimum element at the right subtree

Delete that minimum element ○ Has either no child or only right child because if it has

a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2.

Time complexity = O(height of the tree)

Page 29: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

29

AVL Trees Keep the tree balanced Use node rotation

Balanced condition:

The left and the right subtrees of each node should differ by at most one level.

The method was proposed by two Russian scientists Adelson-Velskii and Landis in 1962

Page 30: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

30

Single Rotation5

37

6 8

9

The sub-tree containing the inserted node (rooted at 8) is at the same side as the ‘heavier’ sub-tree of node 5 (rooted at 7)

Links to be changed

New node

Page 31: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

31

After the Rotation7

58

963

The middle node 6 is switched to the other subtree

Page 32: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

32

Double Rotation5

6

78

5

7 89 6

9

8

8

7

7

The new node 6 is in the left subtree of node 8, while node 8 is the right subtree of 5 -> rotate 7 and 8 to obtain same-side trees.

New node

Page 33: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

33

Splay TreesMove to the top each accessed node with rotations, decreasing the depth of the tree.

Page 34: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

34

Multi-Way SearchNodes contain more than one key

nodes are used only to contain the keys, the actual records are stored at the terminal nodes at the bottom of the tree

Page 35: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

35

Complexity Issues AVL trees:

Search,Insertion, and Deletion: O(logN) Creating the tree – O(N)

Trees with multiple keys: O(LogmN)

m – branching factor

Page 36: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Page 37: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Linear searchLinear search In computer science, linear search or

sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.

Linear search is the simplest search algorithm.

Page 38: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Linear searchLinear search (continued)(continued)

The simplest type of searching process.

In the sequential search, each element of the array is compared to the key, in the order it appears in the array, until the first element matching the key is found. 

Page 39: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Example AlgorithmLINEAR vs. BINARY

Finding the oldest person in a room full of people1. Understanding the problem

initial condition – room full of people goal – identify the oldest person assumptions

a person will give their real birthday if two people are born on the same day,

they are the same age if there is more than one oldest person,

finding any one of them is okay

Page 40: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Finding the oldest person (algorithm 1)1. line up all the people along one wall2. ask the first person to state his or her name and birthday, then write this

information down on a piece of paper3. for each successive person in line:

i. ask the person for his or her name and birthdayii. if the stated birthday is earlier than the birthday on the paper, cross out old

information and write down the name and birthday of this person

Note: When you reach the end of the line, the name and birthday of the oldest person will be written on the paper

Page 41: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Finding the oldest person (algorithm 2)1. line up all the people along one wall2. as long as there is more than one person in the line, repeatedly

i. have the people pair up (1st with 2nd, 3rd with 4th, etc) – if there is an odd number of people, the last person will be without a partner

ii. ask each pair of people to compare their birthdaysiii. request that the younger of the two leave the line

when there is only one person left in line, that person is the oldest

Page 42: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

for algorithm 1:○ 100 people 5*100 = 500 seconds○ 200 people 5*200 = 1000 seconds○ 400 people 5*400 = 2000 seconds

. . .○ 1,000,000 people 5*1,000,000

= 5,000,000 seconds

for algorithm 2:

○ 100 people 5* log2 100 = 35 seconds

○ 200 people 5* log2 200 = 40 seconds

○ 400 people 5* log2 400 = 45 seconds

. . .○ 1,000,000 people 5* log2 1,000,000 = 100 seconds

Page 43: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Analysis For a list with n items, the best case is when the value is equal to the

first element of the list, in which case only one comparison is needed. The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed. If the value being sought occurs k times in the list, and all orderings of the list are equally likely, the expected number of comparisons is:

Asymptotically, therefore, the worst-case cost and the expected cost of linear search are both O(n).

Page 44: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

APPLICATION Linear search is usually very simple to

implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list.

When many values have to be searched in the same list, it often pays to pre-process the latter in order to use a faster method. For example, one may sort the list and use binary search, or build any efficient search data structure from it.

Page 45: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

ALGORITHM The following ALGORITHM describes a typical variant of

linear search, where the result of the search is supposed to be either the location of the list item where the desired value was found; or an invalid location Λ, to indicate that the desired element does not occur in the list.

Note: the last line is executed only after all list items have been examined with none matching.

Page 46: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

If the list is stored as an array data structure, the location may be the index of the item found (usually between 1 and n, or 0 and n−1). In that case the invalid location Λ can be any index before the first element (such as 0 or −1, respectively) or after the last one (n+1 or n, respectively).

Page 47: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Recursive version Linear search can also be described as

a recursive algorithm:

Page 48: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Searching in reverse order Linear search in an array that is usually

programmed by stepping up an index variable until it reaches the last index.

In many computers, one can reduce the work of the first comparison by scanning the items in reverse order.

Page 49: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

Example Algorithm Suppose an array A with elements indexed 1 to n is to be

searched for a value x. The following pseudocode performs a forward search, returning n + 1 if the value is not found:

Page 50: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

The following pseudocode searches the array in the reverse order, returning 0 when the element is not found:

Page 51: Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

End…Thank you