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

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
Binary Search Trees Comp 550.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Search Trees Chapter 7 Objectives
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Starting at Binary Trees
Binary Search Tree Qamar Abbas.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees … From
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary Search Trees Chapter 7 Objectives
CSE 373 Data Structures Lecture 7
Binary Search Trees What is a binary search tree?
Recursive Objects (Part 4)
Binary Search Tree (BST)
Design & Analysis of Algorithm n-ary Tree & Binary Tree
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Ch. 12: Binary Search Trees Ming-Te Chi
Find in a linked list? first last 7  4  3  8 NULL
Ch. 12: Binary Search Trees Ming-Te Chi
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Trees.
Binary Search Trees Comp 122, Spring 2004.
Non-Linear data structures
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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

T REE R EPRESENTATION 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

T REE R EPRESENTATION [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)

E XAMPLE IN J AVA class Node{ Info info; Node parent; LinkedList children; } class Tree{ Node root; } class Node{ Info info; Node parent; LinkedList children; } class Tree{ Node root; }

N-A RY & B INARY T REE 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.

W HY 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

E XAMPLE IN J AVA class Node{ Info info; Node parent; Node left, right; } class Tree{ Node root; } 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; } class Node{ Info info; Node parent; Node children[]; } class Tree{ Node root; } Binary treeN-ary tree

N- ARY TO B INARY T REE class Node{ Info info; Node parent; Node firstChild; Node nextSibling; } class Node{ Info info; Node parent; Node firstChild; Node nextSibling; } class Node{ Info info; Node parent; Node children[]; } class Node{ Info info; Node parent; Node children[]; } The root has a null sibling

T REE T RAVERSAL :: DFS Visit first child and all its descendant first, then visit the second sibling, etc DFS(x) visit(x) for each v child of x DFS(v) 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) DFS(x) if(x not NULL) visit(x) DFS(x.firstChild) DFS(x.nextSibling) Same as DFS on a binary tree

T REE T RAVERSAL :: DFS There are basically 3 variants of DFS Preorder visit the root, then the left subtree, then the right subtree Inorder visit the left subtree, then the root, then the right subtree Postorder visit 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) 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) 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) DFS_POST(x) if(x not NULL) DFS(x.left) DFS(x.right) visit(x) Postorder

T REE T RAVERSAL :: BFS Similar to BFS traversal on a graph BFS() 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) BFS() 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)

S OME B ASIC M ETHODS Counting the number of nodes Computing depth COUNT(x) if (x == NULL) return 0 else return 1 + COUNT(x.left) + COUNT(x.right) COUNT(x) if (x == NULL) return 0 else return 1 + COUNT(x.left) + COUNT(x.right) DEPTH(x) if (x == NULL) return 0 else return 1 + MAX(DEPTH(x.left),DEPTH(x.right)) DEPTH(x) if (x == NULL) return 0 else return 1 + MAX(DEPTH(x.left),DEPTH(x.right))

S OME B ASIC M ETHODS Searching info in a tree rooted at x SEARCH(x, info) if (x == NULL) return NULL else if (x.info == info) return x else s = SEARCH(x.left, info) if(s not NULL) return s else return SEARCH(x.right, info) SEARCH(x, info) if (x == NULL) return NULL else if (x.info == info) return x else s = SEARCH(x.left, info) if(s not NULL) return s else return SEARCH(x.right, info)

B INARY S EARCH T REE 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

S EARCHING Similar to Binary Search on an array SEARCH(x, info) if (x == NULL) return NULL else if (x.info == info) return x else if(info < x.info) return SEARCH(x.left, info) else return SEARCH(x.right, info) SEARCH(x, info) if (x == NULL) return NULL else if (x.info == info) return x else if(info < x.info) return SEARCH(x.left, info) else return SEARCH(x.right, info) x < x≥ x

M INIMUM / M AXIMUM 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 x else while(x.left not NULL) x = x.left return x MIN(x) if (x == NULL) return x else while(x.left not NULL) x = x.left return x MAX(x) if (x == NULL) return x else while(x.right not NULL) x = x.right return x MAX(x) if (x == NULL) return x else while(x.right not NULL) x = x.right return x

F INDING S UCCESSOR 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

F INDING S UCCESSOR Case 2 : x doesn’t have a right subtree x z x < z x z (y<x) < z y x ynyn y1y1 z (y 1 <…<y n <x) < z

F INDING S UCCESSOR 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.parent while(y not NULL AND x == y.right) x = y y = y.parent return y SUCCESSOR(x) if (x.right not NULL) return MIN(x.right) else y = x.parent while(y not NULL AND x == y.right) x = y y = y.parent return y Finding predecessor is very similar

BST I NSERTION TREE_INSERT(T, info) x = Node(info) if (T is an empty tree) T.root = x else INSERT(T.root, x) INSERT(curr, x) if(x.info < curr.info) if (curr.left == NULL) x.parent = curr curr.left = x else INSERT(curr.left, x) else if(curr.right == NULL) x.parent = curr curr.right = x else INSERT(curr.right, x) TREE_INSERT(T, info) x = Node(info) if (T is an empty tree) T.root = x else INSERT(T.root, x) INSERT(curr, x) if(x.info < curr.info) if (curr.left == NULL) x.parent = curr curr.left = x else INSERT(curr.left, x) else if(curr.right == NULL) x.parent = curr curr.right = x else INSERT(curr.right, x)

BST D ELETION 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

S ORTING Given a list of data L = {a 1, a 2, …, a n } Successively insert the data into BST To view the sorted list just use DFS (inorder)

T IME C OMPLEXITY 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)

S ORTING Given a list of data L = {a 1, a 2, …, a n } 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) Inserting n elements is O(n.h) Traversal takes O(n) So sorting takes O(n.h + n) = O(n.h)

T REE ’ S H EIGHT / D EPTH The tree’s height determine the efficiency of BST’s operations Best case: h = lg(n) Worst case: h = n

B ALANCED T REE 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.