Top Banner
DESIGN & ANALYSIS OF ALGORITHM 05 – N-ARY TREE & BINARY TREE Informatics Department Parahyangan Catholic University
26

D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

Jan 02, 2016

Download

Documents

Derick Dixon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

DESIGN & ANALYSIS OF ALGORITHM05 – N-ARY TREE & BINARY TREE

Informatics Department

Parahyangan Catholic University

Page 2: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

TREE REPRESENTATION

How do we store a tree in a computer software ?

Store as a graph ? Hard to tell the parent-child relationship between its

vertices

Store in an array of parents ?(just like a DFS/BFS traversal tree) Only able to tell which vertex is the parent of a

given vertex But we often need to know which is/are the

child/children of a given vertex

Page 3: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

TREE REPRESENTATION

[Recursive definition] A tree is either : An empty tree (has no vertex) A root with zero or more tree children

[Recursive tree representation]A node (vertex) of a tree can have: A parent Zero or more node(s) as its children

A Tree has either Null root, means it’s an empty tree (0 vertex) One root, mean it’s not an empty tree (>0

vertex)

Page 4: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

EXAMPLE IN JAVA

class Node{Info info;Node parent;LinkedList<Node> children;

}

class Tree{Node root;

}

Page 5: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

N-ARY & BINARY TREE A tree is called n-ary tree if every node may have no

more than n children. 2-ary tree is called binary tree

Why is n important ?

By limiting the number of children, the tree data structure is easier to implement Instead of a linked list of children, we can use a static

array Instead of traversing through a linked list, we can

directly access the k-th children by using the array’s index

etc.

Page 6: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

WHY ARE BINARY TREES SPECIAL ?

Binary representation of computer data

Every other trees can be represented as binary tree, which is more efficient if the average number of children is << n

Page 7: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

EXAMPLE IN JAVA

class Node{Info info;Node parent;Node left, right;

}

class Tree{Node root;

}

class Node{Info info;Node parent;Node children[];

}

class Tree{Node root;

}

Binary tree N-ary tree

Page 8: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

N-ARY TO BINARY TREE

1 2

4 5

3

6 7 8 9

1 2

4 5

3

6 7 8 9

class Node{Info info;Node parent;Node firstChild;Node nextSibling;

}

class Node{Info info;Node parent;Node children[];

}

The root has a null sibling

Page 9: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

TREE TRAVERSAL :: DFS

Visit first child and all its descendant first, then visit the second sibling, etc.

1 2

4 5

3

6 7 8 9

1 2

4 5

3

6 7 8 9

DFS(x)visit(x)for each v child of x

DFS(v)

DFS(x)if(x not NULL)

visit(x)DFS(x.firstChild)DFS(x.nextSibling)

Same as DFS on a binary tree

Page 10: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

TREE TRAVERSAL :: DFS There are basically 3 variants of DFS

Preordervisit the root, then the left subtree, then the right subtree

Inordervisit the left subtree, then the root, then the right subtree

Postordervisit the left subtree, then the right subtree, then the root

Preorder, inorder, and postorder on n-ary tree Left subtree = first sibling subtree Right subtree = next sibling subtree

DFS_PRE(x)if(x not NULL)

visit(x)DFS(x.left)DFS(x.right)

Preorder

DFS_IN(x)if(x not NULL)

DFS(x.left)visit(x) DFS(x.right)

Inorder

DFS_POST(x)if(x not NULL)

DFS(x.left)DFS(x.right)visit(x)

Postorder

Page 11: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

TREE TRAVERSAL :: BFS

Similar to BFS traversal on a graphBFS()

Q.enqueue(root)while not Q.isEmpty()

x = Q.dequeue()visit(x)if(x.left not NULL) Q.enqueue(x.left)if(x.left not NULL) Q.enqueue(x.right)

Page 12: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

SOME BASIC METHODS

Counting the number of nodes

Computing depth

COUNT(x)if (x == NULL)

return 0else

return 1 + COUNT(x.left) + COUNT(x.right)

DEPTH(x)if (x == NULL)

return 0else

return 1 + MAX(DEPTH(x.left),DEPTH(x.right))

Page 13: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

SOME BASIC METHODS

Searching info in a tree rooted at xSEARCH(x, info)

if (x == NULL)return NULL

else if (x.info == info)return x

elses = SEARCH(x.left, info)if(s not NULL)

return selse

return SEARCH(x.right, info)

Page 14: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

BINARY SEARCH TREE

BST is a binary tree which has the property that for any node x in the tree If y is a node in the left subtree of x then

y.info < x.info If y is a node in the right subtree of x then

y.info ≥ x.info

Basic Methods Querying

Searching Finding minimum / maximum Finding successor / predecessor

Insertion & Deletion Sorting

Page 15: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

SEARCHING

Similar to Binary Search on an array

SEARCH(x, info)if (x == NULL)

return NULLelse if (x.info == info)

return xelse

if(info < x.info)return SEARCH(x.left, info)

elsereturn SEARCH(x.right, info)

x

< x ≥ x

Page 16: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

MINIMUM / MAXIMUM

The smallest element in a BST must be stored on the left most node, and similarly, the largest element is stored on the right most node

MIN(x)if (x == NULL)

return xelse

while(x.left not NULL)x = x.left

return x

MAX(x)if (x == NULL)

return xelse

while(x.right not NULL)x = x.right

return x

Page 17: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

FINDING SUCCESSOR

Successor = the smallest element among elements that is greater than x

Case 1 : x has a right subtree Successor of x is the minimum of x’s right

subtree

x

Page 18: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

FINDING SUCCESSOR

Case 2 : x doesn’t have a right subtree

x

z

x < z

x

z

(y<x) < z

y

x

yn

y1

z

(y1<…<yn<x) < z

Page 19: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

FINDING SUCCESSOR

Case 3 : x doesn’t have a right subtree, and x is the right most element of the tree X doesn’t have a successor

x

SUCCESSOR(x)if (x.right not NULL)

return MIN(x.right)else

y = x.parentwhile(y not NULL AND x == y.right)

x = yy = y.parent

return y

Finding predecessor is very similar

Page 20: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

BST INSERTIONTREE_INSERT(T, info)

x = Node(info)if (T is an empty tree)

T.root = xelse

INSERT(T.root, x)

INSERT(curr, x)if(x.info < curr.info)

if (curr.left == NULL)x.parent = currcurr.left = x

elseINSERT(curr.left, x)

elseif(curr.right == NULL)

x.parent = currcurr.right = x

elseINSERT(curr.right, x)

Page 21: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

BST DELETION

It has three basic cases If the node to be deleted has no children, then

just remove it If the node to be deleted has one child, then

replace the node with its only child If the node to be deleted has two children, then

replace with its successor

Pseudocode is left as an exercise

Page 22: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

SORTING

Given a list of data L = {a1, a2, …, an} Successively insert the data into BST To view the sorted list just use DFS (inorder)

Page 23: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

TIME COMPLEXITY

For a tree with n nodes Traversal visits every node, so it takes O(n) time

Insertion can insert on the bottom most location of the tree, so it is proportional to the tree’s depth/height. Suppose the tree’s height is h, then Insertion takes O(h) time

Searching, finding Maximum / Minimum, finding Successor / Predecessor are also O(h)

Deletion might call successor, so it also O(h)

Page 24: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

SORTING

Given a list of data L = {a1, a2, …, an} Successively insert the data into BST To view the sorted list just use DFS (inorder)

What is the complexty of

Sorting ?

Inserting n elements is O(n.h)Traversal takes O(n)

So sorting takes O(n.h + n) = O(n.h)

Page 25: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

TREE’S HEIGHT / DEPTH

The tree’s height determine the efficiency of BST’s operations

8

4 12

2 6 10 14

1 3 5 7 9 11 13 15

Best case: h = lg(n)

1

2

3

14

15Worst case:h = n

Page 26: D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.

BALANCED TREE

It is clear that a more balanced tree gives a better performance than the unbalanced one

There are various attempts to build a balanced tree data structure, such as: Red-Black tree Self Balancing BST B-Tree Treap etc.