Rossella Lau Lecture 8, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 8: Trees  General model of a tree  Binary Tree.

Slides:



Advertisements
Similar presentations
Chapter 7. 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
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Rossella Lau Lecture 9, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 9: More on BST  Removal of a BST  Some advanced.
Transforming Infix to Postfix
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
Binary Search Trees Chapter 6.
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.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees Chapter 6.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Algorithms and data structures Protected by
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
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.
Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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 ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Starting at Binary Trees
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
CS 367 Introduction to Data Structures Lecture 8.
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.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Trees Chapter 15.
Binary Search Tree (BST)
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Binary Trees, Binary Search Trees
Presentation transcript:

Rossella Lau Lecture 8, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 8: Trees  General model of a tree  Binary Tree  Tree representations  Heap and Heap sort  Binary Search Tree: construction and search -- By Rossella Lau

Rossella Lau Lecture 8, DCO20105, Semester A, A reason for Tree  Basic sequential containers do not support efficient processes on all of {insert, delete, search}  vectors: can support efficient search but not insert/delete  list: can support efficient insert/delete but not search Any other structures support efficient processes on all the above operations?  Tree

Rossella Lau Lecture 8, DCO20105, Semester A, Tree  In the most general sense, is a set of vertices, or nodes, and a set of edges, where each edge connects a pair of distinct nodes, such that there is one and only one connecting path on these edges between any pair of nodes.  A tree in the above sense is called a free tree  By picking up a distinguished node, denoting it as a root, as the entrance of the tree, a tree can be represented as an oriented tree  A free tree may have numerous oriented trees corresponding to a given free tree

Rossella Lau Lecture 8, DCO20105, Semester A, Different orientations of a tree A B D C E F A CD EF B A CD EF B A CBD EF

Rossella Lau Lecture 8, DCO20105, Semester A, Binary Tree A binary tree can be empty or partitioned into three disjointed subsets: 1. A single element called the root of the tree 2. A left sub-tree, which is a binary tree, of itself 3. A right sub-tree, which is a binary tree, of itself Tree or binary tree’s definition is a recursive definition and operations on trees are usually in a recursive manner

Rossella Lau Lecture 8, DCO20105, Semester A, Notations of a binary tree A C F HI B DE G  A is the root of the tree  A is the parent of B and C (B is the parent of D and E, …)  B is a left child of A  C is a right child of A  A or B is an ancestor of E  E or B is a descendant of A  B and C are siblings  D, G, H, I are leaves of the tree  The level of A is 0, the level of B is 1, …, the level of G is 3  Depth = max{ level of leaves} = 3

Rossella Lau Lecture 8, DCO20105, Semester A, Structures that are not binary trees A C F I B DE GH  All of these trees contain a tree which is not a sub-tree of itself  There is more than one path connecting two of the nodes A C F B DE G A C F B DE G HI

Rossella Lau Lecture 8, DCO20105, Semester A, Traversing a binary tree  To pass through a binary tree and enumerate each of its nodes once  To enumerate, e.g., to print the contents of each node, to update the contents of each node  When a node is enumerated, it is visited There are, usually, three ways to traverse a binary tree  Preorder (depth-first order)  Inorder (symmetric order)  Postorder

Rossella Lau Lecture 8, DCO20105, Semester A, The algorithms for traversing a binary tree Preorder: 1.Visit the root 2.Traverse the left sub-tree in preorder sequence 3.Traverse the right sub-tree in preorder sequence Inorder: 1.Traverse the left sub-tree in inorder sequence 2.Visit the root 3.Traverse the right sub-tree in inorder sequence Postorder: 1.Traverse the left sub-tree in postorder sequence 2.Traverse the right sub-tree in postorder sequence 3.Visit the root

Rossella Lau Lecture 8, DCO20105, Semester A, Examples of traversing a binary tree For the binary tree on page 6  Preorder:  Inorder:  Postorder:

Rossella Lau Lecture 8, DCO20105, Semester A, Representations of Binary Tree  Static: Vector representation  Dynamic: Pointer (Node) representation

Rossella Lau Lecture 8, DCO20105, Semester A, Complete binary trees  A Complete binary tree of depth d:  all of whose leaves are at level d  all of non-leaf (internal) nodes have exactly two children  A binary tree of depth d is an almost complete binary tree if: 1. Any node n at level from 0 to d-2 has two children 2. For each node n in the tree with a right descendant at level d  n must have a left child and  every left descendant of n is either a leaf at level d or has two children

Rossella Lau Lecture 8, DCO20105, Semester A, A complete binary tree of depth 3 B DE A HIJK C FG LMNO

Rossella Lau Lecture 8, DCO20105, Semester A, Examples of almost complete binary trees B DE A HI C FG B DE A HI C FG J  Is this an almost complete binary tree? B DE A HI C FG J

Rossella Lau Lecture 8, DCO20105, Semester A, Density of a tree  A complete binary tree has the highest density: number of nodes: 2 d  A tree with nodes which all have a single child has the lowest density: number of nodes: d+1  A tree with not many nodes is called sparse  Give number of nodes n, a tree can be with a depth of n-1 to log2 (n+1) - 1

Rossella Lau Lecture 8, DCO20105, Semester A, Implicit array representation of a binary tree  For each almost complete binary tree, we can label each node from 0 to n, where n < (2 d+1 - 1) B DE A HI C FG J  The label is the subscript of an array  The content of a numbered node can be stored in the corresponding position of an array ABCDIEHFGJ

Rossella Lau Lecture 8, DCO20105, Semester A, Extensions to almost complete BT B A C DE FG I H J KL M B A C DE FG I H J KL M ABCDE FG HIJKLM For trees that are not a complete binary tree, we may add null nodes to make the trees become almost complete

Rossella Lau Lecture 8, DCO20105, Semester A, Some operations on vector representation  Some basic binary tree operations can be easily implemented:  vector bt;  left_child(node): 2 * node + 1  right_child(node): 2 * (node + 1)  parent(node): (node – 1) / 2 when node > 0  data(node): bt[node]  For efficient calculation of parent and children, representation may start the root from subscript at 1 instead of 0  left_child(node): 2 * node (equivalent to node<<1 )  right_child((node): left_child + 1  parent(node): node / 2 (equivalent to node>>1 )  the calculation can be simplified to bit-shift operations

Rossella Lau Lecture 8, DCO20105, Semester A, Exercises on implicit representation  Ford’s written exercises: 14:11a, 12c

Rossella Lau Lecture 8, DCO20105, Semester A, An application of array representation: Heap  A heap is an almost complete binary tree in which each node is less than or equal to its parent  Since it is an almost complete binary tree, its implementation uses implicit array representation  The common use of a heap is as a priority queue  A sample of a heap

Rossella Lau Lecture 8, DCO20105, Semester A, Heap insert  To insert the item as the last leaf in the tree then shift it up whenever it is larger than its parent  E.g., Adding 92 to the previous heap

Rossella Lau Lecture 8, DCO20105, Semester A, Heap delete  To remove the maximum from the heap: 1. Swap the root (maximum) with the last element in the array (the last node in the tree)  the heap is reduced by one element 2. Shift the new root down whenever it is less than its larger child within the reduced heap

Rossella Lau Lecture 8, DCO20105, Semester A, Exercise on heap  Ford’s written exercises: 14:19b

Rossella Lau Lecture 8, DCO20105, Semester A, Heap sort  A binary tree can be represented by a vector; a list of data in a vector can also be treated as an almost complete binary tree!  Heap sort makes use of this feature to construct data on a vector into a heap then sort data in order  It is a kind of selection sort  Each time it finds the largest from a list (the heap) then places it to the last position of the list  The process continues the “selection” on the sub-lists starting from the first elements which are not in the proper positions yet.

Rossella Lau Lecture 8, DCO20105, Semester A, Heap sort method 1  1 st phase: Construction of a heap  it inserts elements to the heap one by one ()  2 nd phase: Selection sort 1. remove the maximum from the heap and replace it to the last (the heap is reduced in the first n-1 nodes) 2. process continues until reduced heap becomes a single node

Rossella Lau Lecture 8, DCO20105, Semester A, An example of heap sort (method 1)  Input stream:  Then insert data one by one into the heap:

Rossella Lau Lecture 8, DCO20105, Semester A, Phase II of heap sort ……

Rossella Lau Lecture 8, DCO20105, Semester A, Heapsort (method 2)  Instead of inserting data one by one, it converts the tree to a heap in the first phase makeHeap() in Ford: Iteratively applying the heap condition to each internal node (sub-trees) starting at the last and working up to the root  Then it applies the second phase of method 1

Rossella Lau Lecture 8, DCO20105, Semester A, Phase I of method

Rossella Lau Lecture 8, DCO20105, Semester A, Performance of heap sort  For each insertion, it takes O(logn) because the process is on an almost complete binary tree  For n elements, it takes O(nlogn) even for worst case  Experiments show that heapsort doubles the time of quicksort but out performs quicksort in the worst case since the process keeps working on an almost complete binary tree (level at most log(n+1)).

Rossella Lau Lecture 8, DCO20105, Semester A, Dynamic pointer representation  Reference program: BST.h: use two classes: BNode and BTree template class BNode { T item; BNode *left; BNode *right; //end of data member ……} template class BTree { BNode *root; size_t countNodes; //end of data member …… }

Rossella Lau Lecture 8, DCO20105, Semester A, Implementation of inorder traversal template void BTree ::inOrder(BNode const *bnode) const { if (bnode->left) inOrder(bnode->left); cout item << “ “; if (bnode->right) inOrder(bnode->right); } template void BTree ::inOrder() const { if ( root ) inOrder(root); else cout << “Empty tree\n”; }

Rossella Lau Lecture 8, DCO20105, Semester A, Pretty tree  In order to make a tree visible, we may imagine the tree with a 90 degree left rotation, then we have a special printing method: a reversed inorder traversal with nodes printed according to their levels void prettyTree (BNode const *bnode, size_t const level) const { if (bnode->right) prettyTree(bnode->right, level + 1); // make space for different levels for (size_t i=0; i<level; i++) cout << “ “; cout item << endl; if (bnode->left) prettyTree(bnode->left, level + 1); } void prettyTree () { if (root) pretty_tree(root, 0); else …… }

Rossella Lau Lecture 8, DCO20105, Semester A, Binary Search Tree (BST)  A BST is a binary tree in which all the key values stored in the left descendents of a node are less than the key value of the node, and all the key values stored in the right descendants of a node are greater than the key value of the node. E.g.,

Rossella Lau Lecture 8, DCO20105, Semester A, Dynamic representation for a BST  Same as a Binary Tree; sample program: BST.h template class BSTNode { T item; BSTNode *left; BSTNode *right; //end of data member ……} Template class BSTree { BSTNode *root; size_t countNodes; //end of data member …… }

Rossella Lau Lecture 8, DCO20105, Semester A, The algorithm for searching on a BST  The searching can use a recursive approach. BSTNode * BSTree ::search (BSTNode const *node, T const& target) const { if ( target == node->item ) return node; if ( target item ) return node->left ? search(node->left, target) : 0; else return node->right? search(node->right, target): 0; } BSTNode * BSTree ::search (T const& target) const { return root ? search(root, target) : 0; }

Rossella Lau Lecture 8, DCO20105, Semester A, The iterative version for searching on a BST  However, it is also quite easy to convert the recursive algorithm to a non-recursive(iterative) one since it only involves "going down" the tree. BSTNode * BSTree ::search(T const& target) const { BSTNode *cur = root; while (cur) { if (target == cur->item) return cur; if (target item;) cur = cur->left; else cur = crr->right; } return 0; }

Rossella Lau Lecture 8, DCO20105, Semester A, A better way to return the result: find()  Searching usually follows the operations of insert or delete but the traditional search returns  a null pointer when a new item is required to insert; i.e., the insert has to find the proper position to insert the item, again!  the node for deletion which requires checking if the node is on the right hand side or the left hand side of its parent, again!  With the reference supported in C++, we can write a find() which is similar the one in List.h in Lecture 4 for efficient insert() and remove() with one single search operation even if these operations require a search to make sure the node does not exist or does exist  Node *& means a reference of pointer that can be interpreted as the reference of the location where the pointer stores. From another view, if the name is on the right hand side of an expression, it refers to the value of the pointer, i.e., the node pointed to by the pointer; if the name is on the left hand side, it refers to the location storing the pointer; or the "parent" of the node! Assigning new values to the name means to change its "child"!

Rossella Lau Lecture 8, DCO20105, Semester A, The implementation of find() BSTNode *& find (T const & target) { if ( !root || target == root->item ) return root; BSTNode * par = root; // parent of current node while( 1 ) { if ( target item ) if (!par->left || target == par->left->item) return par->left; else par = par->left; else if (!par->right || target == par->right->item) return par->right; else par = par->right; }}

Rossella Lau Lecture 8, DCO20105, Semester A, Insert an item with find()  To insert an item involves searching for the correct place and usually, a BST assumes no duplication, then attach the new node to the target found by find()  Add an additional function attach() to BSTree bool attach( BSTNode *& nodeRef, T const & x ) { nodeRef = new BSTNode ( x ); return nodeRef;} bool insert(T const & target) { BSTNode *& curRef ( find ( target ) ); if ( !curRef ) return attach(curRef, target); else return false; // duplication }

Rossella Lau Lecture 8, DCO20105, Semester A, Construction of the BST using insert() Input sequence: 50, 28, 40, 75, 90, 22, 35, 95, 87  An online animation is also available at: l

Rossella Lau Lecture 8, DCO20105, Semester A, More exercises on BST  Ford’s exercises: 10:20, 22

Rossella Lau Lecture 8, DCO20105, Semester A, Complexity considerations  If the binary tree is constructed in a random order, the levels of the left sub-tree and right sub-tree of the resulting tree may be similar and each later search process is similar to a binary search in an array  Therefore, the optimal complexity for searching on a BST is about O(log 2 n)  However, if the input sequence for the BST is in sequential order, it may result in the tree on the next page. The complexity of find() becomes O(n)  Therefore, the complexity of the search on a BST is from O(log 2 n) to O(n).

Rossella Lau Lecture 8, DCO20105, Semester A, The worst case of searching on a BST  Input sequence: 22, 28, 35, 40, 50, 75, 87, 90,

Rossella Lau Lecture 8, DCO20105, Semester A, Complexity for insert()  As the logic of insert() is find() + attach()  If there is a fast memory allocation method, the running time of attach() is O(1)  insert() is similar to find(), insert() has the same complexity as find()

Rossella Lau Lecture 8, DCO20105, Semester A, Summary  A binary tree is a typical recursive structure and has three parts: root, left and right sub-trees  A binary tree is used to being stored in node representation  Sometimes, it is also efficient to store a binary tree in implicit array (vector) representation and its typical applications are heap and heap sort which is quite an efficient sorting algorithm for all cases  There are three usual ways to traverse a binary tree: preorder, inorder, and postorder  The binary search tree (BST) keeps smaller values on the left side of a node and larger values on the right  The optimal complexity for insert/search of a BST is O(log 2 n)

Rossella Lau Lecture 8, DCO20105, Semester A, Reference  Ford: ,  Data Structures and Algorithms in C++ by Michael T. Goodrich, Roberto Tamassia, David M. Mount : Chapter 6,8  Example programs: BST.h, testBST.cpp -- END --