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,

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees, Binary Trees, and Binary Search Trees. 2 Trees * Linear access time of linked lists is prohibitive n Does there exist any simple data structure.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Chapter 4: Trees AVL Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
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,
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
CSE 373 Data Structures Lecture 7
CC 215 Data Structures Trees
BCA-II Data Structure Using C
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Introduction to Trees IT12112 Lecture 05.
Trees, Binary Trees, and Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Trees CSE 373 Data Structures.
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

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, insert, delete) is O(log N)?  Trees Basic concepts Tree traversal Binary tree Binary search tree and its operations

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 T 1, T 2,...., T k, each of whose roots are connected by a directed edge from r

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

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

Example: UNIX Directory

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

Tree Traversal  Used to print out the data in a tree in a certain order  Pre-order traversal Print the data at the root Recursively print out all data in the left subtree Recursively print out all data in the right subtree

Preorder, Postorder and Inorder  Preorder traversal node, left, right prefix expression ○ ++a*bc*+*defg

Preorder, Postorder and Inorder  Postorder traversal left, right, node postfix expression ○ abc*+de*f+g*+  Inorder traversal left, node, right infix expression ○ a+b*c+d*e+f*g

Example: Unix Directory Traversal PreOrder PostOrder

Preorder, Postorder and Inorder Pseudo Code

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-case binary tree

Node Struct of Binary Tree  Possible operations on the Binary Tree ADT Parent, left_child, right_child, sibling, root, etc  Implementation Because a binary tree has at most two children, we can keep direct pointers to them

Convert a Generic Tree to a Binary Tree

Binary Search Trees (BST)  A data structure for efficient searching, insertion and deletion  Binary search tree property For every node X All 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

Binary Search Trees A binary search tree Not a binary search tree

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

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.

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 element) return Find(x, t->left); else if (t->element < x) return Find(x, t->right); else return t; // match }

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

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); }

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)

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.

Deletion under Different Cases  Case 1: the node is a leaf Delete it immediately  Case 2: the node has one child Adjust a pointer from the parent to bypass that node

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)

l 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

l 30 Single Rotation l5l5 l3l3 l7l7 l6l6 l8l8 l9l9 l 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) l Links to be changed l New node

l 31 After the Rotation l7l7 l5l5 l8l8 l9l9 l6l6 l3l3 l The middle node 6 is switched to the other subtree

l 32 Double Rotation l5l5 l6l6 l7l7 l8l8 l5l5 l7l7 l8l8 l9l9 l6l6 l9l9 l8l8 l8l8 l7l7 l7l7 l 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. l New node

l 33 Splay Trees l Move to the top each accessed node with rotations, decreasing the depth of the tree.

l 34 Multi-Way Search l Nodes contain more than one key l nodes are used only to contain the keys, the actual records are stored at the terminal nodes at the bottom of the tree

l 35 Complexity Issues  AVL trees: Search,Insertion, and Deletion: O(logN) Creating the tree – O(N)  Trees with multiple keys: O(Log m N) m – branching factor

Linear 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.computer sciencelist  Linear search is the simplest search algorithm.search algorithm

Linear search (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.

Example Algorithm LINEAR vs. BINARY Finding the oldest person in a room full of people 1. 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

 Finding the oldest person (algorithm 1) 1. line up all the people along one wall 2. ask the first person to state his or her name and birthday, then write this information down on a piece of paper 3. for each successive person in line: i. ask the person for his or her name and birthday ii. 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

l Finding the oldest person (algorithm 2) 1. line up all the people along one wall 2. as long as there is more than one person in the line, repeatedly i. have the people pair up (1 st with 2 nd, 3 rd with 4 th, 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 birthdays iii. request that the younger of the two leave the line when there is only one person left in line, that person is the oldest

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*  log  = 35 seconds ○ 200 people  5*  log  = 40 seconds ○ 400 people  5*  log  = 45 seconds... ○ 1,000,000 people  5*  log 2 1,000,000  = 100 seconds

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

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.sortsearch data structure

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.

 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).array data structureindex

Recursive version  Linear search can also be described as a recursive algorithm:recursive algorithm

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.

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:

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

End… Thank you