CS302 - Data Structures using C++

Similar documents
CS302 - Data Structures using C++

CS302 - Data Structures using C++

CS302 - Data Structures using C++

CS302 - Data Structures using C++

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Trees, Binary Trees, and Binary Search Trees

Chapter 20: Binary Trees

Programming II (CS300)

Cpt S 122 Data Structures. Data Structures Trees

Lecture Topics. Object Structures. Hierarchical Organization. Tree Concepts. Hierarchical Organization. Hierarchical Organization

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Binary Trees, Binary Search Trees

CS302 - Data Structures using C++

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Binary Trees and Binary Search Trees

A set of nodes (or vertices) with a single starting point

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

void insert( Type const & ) void push_front( Type const & )

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

CMSC 341. Binary Search Trees CMSC 341 BST

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CMSC 341 Lecture 10 Binary Search Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS302 - Data Structures using C++

A Binary Search Tree Implementation

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

CmpSci 187: Programming with Data Structures Spring 2015

Binary Trees. Height 1

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Topic Binary Trees (Non-Linear Data Structures)

INF2220: algorithms and data structures Series 1

Binary Trees. Examples:

Data Structures and Algorithms

Lecture 2. Binary Trees & Implementations. Yusuf Pisan

Data Structures in Java

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Trees. (Trees) Data Structures and Programming Spring / 28

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

Generic BST Interface

TREES. Trees - Introduction

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

Introduction to Computers and Programming. Concept Question

CS302 - Data Structures using C++

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Principles of Computer Science

Traversing Trees with Iterators

Traversing Trees with Iterators

CS200: Balanced Search Trees

BBM 201 Data structures

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CS24 Week 8 Lecture 1

Tree Travsersals and BST Iterators

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Chapter 8. Binary Search Trees. Fall 2013 Yanjun Li CISC Trees. Owner Jake. Waitress Waiter Cook Helper Joyce Chris Max Len

If you took your exam home last time, I will still regrade it if you want.

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Tree Data Structures CSC 221

ITI Introduction to Computing II

Binary Trees

CSE 214 Computer Science II Heaps and Priority Queues

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Name CPTR246 Spring '17 (100 total points) Exam 3

Spring 2018 Mentoring 8: March 14, Binary Trees

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

ECE 244 Programming Fundamentals Fall Lab Assignment #5: Binary Search Trees

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

CS 206 Introduction to Computer Science II

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

ITI Introduction to Computing II

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

INF2220: algorithms and data structures Series 1

CS200 Midterm 2 Fall 2007

Data Structures Lab II. Binary Search Tree implementation

Tables, Priority Queues, Heaps

Data Structure - Binary Tree 1 -

B-Trees. CS321 Spring 2014 Steve Cutchin

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

8. Binary Search Tree

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Title Description Participants Textbook

COMP : Trees. COMP20012 Trees 219

Transcription:

CS302 - Data Structures using C++ Topic: Implementation Kostas Alexis

s Definition: A (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node s left subtree and smaller than the keys in all nodes in that node s right subtree.

s Binary Tree property 8 Each node has <= 2 children Result 5 11 Storage is small Operations are simple Average depth is small Search Tree property 2 6 10 12 All keys in left subtree smaller than root s key All keys in right subtree larger than root s key 4 7 9 14 Result Easy to find any given key Insert/delete by changing links 13

s Example and Counter-Example 5 8 4 8 5 11 1 7 11 2 7 6 10 18 3 BINARY SEARCH TREE 4 15 NOT A BINARY SEARCH TREE 20 21

s (BST) Binary tree that has the following properties for each node n: n s value is > all values in n s left subtree T L n s value is < all values in n s right subtree T R Both T L and T R are binary search trees

s (BST) Binary tree that has the following properties for each node n: n s value is > all values in n s left subtree T L n s value is < all values in n s right subtree T R Both T L and T R are binary search trees A binary tree whose nodes contain objects and Data in a node is greater than the data in the node s left child Data in a node is less than the data in the node s right child

s Jared Brittany Megan Brett Doug Brett Doug

s Jared Brittany Megan Brett Doug Brett Doug An In-Order Traversal is In Order for a

Uses same node objects as for binary-tree implementation

Uses same node objects as for binary-tree implementation Class BinaryNode will be used

Uses same node objects as for binary-tree implementation Class BinaryNode will be used Recursive search algorithm is the basis for operations

Adding Kody to a

Adding Kody to a Addition is like search we first find where Kody should be if it was there.

Adding Kody to a Addition is like search we first find where Kody should be if it was there. Terminates at Mia

Adding Kody to a Addition is like search we first find where Kody should be if it was there. Terminates at Mia As Mia has no left child, the addition is simple, requiring only that Mia s left child pointer points to a new node that contains Kody.

Notes for the class BinarySearchTree In this lecture-approach: based on BinaryNodeTree Tree Root Data Field rootptr Disable any methods that could change a value BinaryNode Tree void setrootdata(const ItemType& newdata)

Method add template<class ItemType> bool BinarySearchTree<ItemType>::add(const ItemType& newdata) { auto newnodeptr = std::make_shared<binarynode<itemtype>>(newdata); rootptr = placenode(rootptr, newnodeptr); return true; } // end add

Method add Must maintain binary search tree structure Every addition to a binary search tree adds a new leaf to the tree

Method add Must maintain binary search tree structure Every addition to a binary search tree adds a new leaf to the tree add (Chad) Jared Brittany Megan Brett Doug Brett Doug

Method add Must maintain binary search tree structure Every addition to a binary search tree adds a new leaf to the tree add (Chad) Chad Jared Brittany Megan Brett Doug Brett Doug

Method add Must maintain binary search tree structure Every addition to a binary search tree adds a new leaf to the tree add (Chad) Jared Chad Brittany Megan Brett Doug Brett Doug

Method add Must maintain binary search tree structure Every addition to a binary search tree adds a new leaf to the tree add (Chad) Jared Brittany Megan Brett Doug Brett Doug Chad

Method add Must maintain binary search tree structure Every addition to a binary search tree adds a new leaf to the tree add (Chad) Jared Brittany Megan Brett Doug Brett Doug Chad

Refinement of addition algorithm // Recursively places a given new node at its proper position in a binary search tree placenode(subtreeptr: BinaryNodePointer, newnodeptr: BinaryNodePointer): BindayNodePointer { if (subtreeptr == nullptr) return newnodeptr else if (subtreeptr->getitem() > newnodeptr->getitem()) { tempptr = placenode(subtreeptr->getleftchildptr(), newnodeptr) subtreeptr->setleftchildptr(tempptr) } else { tempptr = placenode(subtreeptr->getrightchildptr(), newnodeptr) subtreeptr->setrightchildptr(tempptr) } return subtreeptr }

Adding new data to a binary search tree

Removing an entry. More involved than the adding process First use the search algorithm to locate the specified item and then, if it is found, you must remove it from the tree While doing so, maintain a structure.

First draft of the removal algorithm // Removes the given target from a binary search tree // Returns true if the removal is successful or false otherwise removevalue(target: ItemType): boolean { Locate the target by using the search algorithm if (target is found) { Remove target from the tree return true } else return false }

Must maintain binary search tree structure

Cases for node N containing item to be removed 1. Case 1: N is a leaf 2. Case 2: N has only one child 3. Case 3: N has two children

Cases for node N containing item to be removed Case 1: N is a leaf Remove leaf containing target Set pointer in parent nullptr

Cases for node N containing item to be removed Case 2: N has only left (or right) child cases are symmetrical After N removed, all data items rooted at L (or R) are adopted by root of N All items adopted are in correct order, binary search tree property preserved

Cases for node N containing item to be removed Case 3: N has two children Harder case. One cannot simply remove the parent node. Find a node easier to remove and replace the node elements.

Cases for node N containing item to be removed Case 3: N has two children Locate another node M easier to remove from tree than N Copy item that is in M to N Remove M from tree

leftchild data rightchild K / K H P H P / / / / / / C M R C M R

leftchild data rightchild remove(r) K / K H P H P / / / / / / C M R C M R

leftchild data rightchild remove(r) K / K / H P H P / / / / C M R C M

leftchild data rightchild remove(p) K / K / H P H P / / / / C M R C M

leftchild data rightchild remove(p) K / K H P H / / / / / P C M R C M

leftchild data rightchild remove(p) K / K / / H P H M / / C M R C

leftchild data rightchild remove(k) K / K H P H P / / / / / / C M R C M R

leftchild data rightchild remove(k) K / K H P H P / / / / / / C M R C M R

leftchild data rightchild remove(k) K / K H P H P / / / / / / C M R C M R

leftchild data rightchild remove(k) K / M H P H P / / / / / / C M R C K R

leftchild data rightchild remove(k) K / M H P H P / / / / C M R C R

Case 2 for removevalue: The data item to remove is in a node N that has only a left child and whose parent is node P

Final draft of the removal algorithm (method removenode uses findsuccesornode) // Removes the given target from the binary search tree to which subtreeptr points. // Removes a pointer to the node at this tree location after the value is removed. // Sets issuccessful to true if the removal is successful, or false otherwise removevalue(subtreeptr: BinaryNodePointer, target: ItemType, issuccessful: boolean&): BinaryNodePointer { if (subtreeptr == nullptr) { issuccessful = false } else if (subtreeptr->getitem() == target) { // Item is in the root of some subtree subtreeptr = removenode(subtreeptr) // Remove the item issuccessful = true } else if (subtreeptr->getitem() > target) { // Search the left subtree tempptr = removevalue(subtreeptr->getleftchildptr(), target, issuccessful) subtreeptr->setleftchildptr(tempptr) } else

Final draft of the removal algorithm (method removenode uses findsuccesornode) } else { // Search the right subtree tempptr = removevalue(subtreeptr->getrightchildptr(), target, issuccessful) subtreeptr->setrightchildptr(tempptr) } return subtreeptr // Removes the data item in the node N to which nodeptr points. // Returns a pointer to the node at this tree location after the removal removenode(nodeptr: BinaryNodePointer): BinaryNodePointer { if (N is a leaf) { // Remove leaf from the tree Delete the node to which nodeptr points (done for us if nodeptr is a smart pointer) return nodeptr } else if (N has only one child C)

Final draft of the removal algorithm (method removenode uses findsuccesornode) else if (N has only one child C) { // C replaces N as the child of N s parent if (C is a left child) nodetoconnectptr = nodeptr->getleftchildptr() else nodetoconnectptr = nodeptr->getrightchildptr() Delete the node to which nodeptr points (done for us if nodeptr is a smart pointer) return nodetoconnectptr } else // N has two children

Final draft of the removal algorithm (method removenode uses findsuccesornode) } else // N has two children { // Find the inorder successor of the entry in N: it is in the left subtree rooted at N s right child tempptr = removeleftmostnode(nodeptr->getrightchildptr(), newnodevalue) nodeptr->setrightchildptr(tempptr) nodeptr->setitem(newnodevalue) // Put replacement value in noden return nodeptr } // Removes the leftmost node in the left subtree of the node pointed to by nodeptr // Sets inorder Successor to the value in this node // Returns a pointer to the revised subtree removeleftmostnode(nodeptr: BinaryNodePointer, inordersuccessor: ItemType&): BinaryNodePointer

Final draft of the removal algorithm (method removenode uses findsuccesornode) removeleftmostnode(nodeptr: BinaryNodePointer, inordersuccessor: ItemType&): BinaryNodePointer { if (nodeptr->getleftchildptr() == nullptr) { // This is the node you want; it has no left child, but it might have a right subtree inordersuccessor = nodeptr->getitem() return removenode(nodeptr) } else { tempptr = removeleftmostnode(nodeptr->getleftchildptr(), inordersuccessor) nodeptr->setleftchildptr(tempptr) return nodeptr } }

Final draft of the removal algorithm (method removenode uses findsuccesornode) Public method remove // Removes the given data from this binary search tree remove(target: ItemType): boolean { issuccessful = false rootptr = removevalue(rootptr, target, issuccessful) return issuccessful }

Recursive removal of node N

Recursive removal of node N (cont)

Recursive removal of node N (cont)

Algorithm findnode // Locates the node in the binary search tree to which subtreeptr points and that contains the value target. // Returns either a pointer to the located node or nullptr if such a node is not found. findnode(subtreeptr: BinaryNodePointer, target: ItemType): BinaryNodePointer { if (subtreeptr == nullptr) return nullptr // Not found } else if (subtreeptr->getitem() == target) return subtreeptr; // Found else if (subtreeptr->getitem() > target) // Search left subtree return findnode(subtreeptr->getleftchildptr(), target) else // Search right subtree return findnode(subtreeptr->getrightchildptr(), target)

Algorithm findnode The method findnode is designed to be utilized by a method getentry. The operation getentry must return the data item with the desired value if it exists; otherwise it must throw an exception NotFoundException. The method, therefore, calls findnode and checks its return value. If the desired target is found, getentry returns it. If findnode returns nullptr, getentry throws an exception.

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree // Link-based implementation of the ADT binary search tree. #ifndef BINARY_SEARCH_TREE_ #define BINARY_SEARCH_TREE_ #include BinaryTreeInterface.h #include BinaryNode.h #inlucde BinaryNodeTree.h #include NotFoundException.h #include PrecondViolatedExcept.h #include <memory> template<class ItemType> class BinarySearchTree: public BinaryNodeTree<ItemType> { private: std::shared_ptr<binarynode<itemtype>> rootptr;

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree protected: // PROTECTED UTILITY METHODS SECTION: // RECURSIVE HELPER METHODS FOR THE PUBLIC METHODS // Places a given new node at its proper position in this binary search tree auto placenode(std::shared_ptr<binarynode<itemtype>> subtreeptr, std::shared_ptr<binarynode<itemtype>> newnode); // Removes the given target value from the tree while maintaining a binary search tree auto removevalue(std::shared_ptr<binarynode<itemtype>> subtreeptr, const ItemType target, bool& issuccessful) override; // Removes a given node from a tree while maintaining a binary search tree auto removenode(std::shared_ptr<binarynode<itemtype>> nodeptr); Why protected? Why not private?

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree // Removes the leftmost node in the left subtree of the node pointed to by nodeptr // Sets inordersuccessor to the value in this node // Returns a pointer to the revised subtree auto removeleftmostnode(std::shared_ptr<binarynode<itemtype>>subtreeptr, ItemType& inordersuccessor); // Returns a pointer to the node containing the given value, or nullptr if not found auto findnode(std::shared_ptr<binarynode<itemtype>> treeptr, const ItemType& target) const; public: // CONSTRUCTOR AND DESTRUCTOR SECTION BinarySearchTree(); BinarySearchTree(const ItemType& rootitem); BinarySearchTree(const BinarySearchTree<ItemType>& tree); virtual ~BinarySearchTree();

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree // PUBLIC METHODS SECTION bool isempty() const; int getheight() const; int getnumberofnodes() const; ItemType getrootdata() const throw(precondviolatedexcept); void setrootdata(const ItemType& newdata); bool add(const ItemType& newentry); bool remove(const ItemType& target); void clear(); ItemType getentry(const ItemType& anentry) const throw(notfoundexception) bool contains(const ItemType& anentry) const;

The Class BinarySearchTree A header file for the link-based implementation of the class BinarySearchTree // PUBLIC TRAVERSALS SECTION void preordertraverse(void visit(itemtype&)) const; void inordertraverse(void visit(itemtype&)) const; void postordertraverse(void visit(itemtype&)) const; // OVERLOADED OPERATION SECTION BinarySearchTree<ItemType>& operator = (const BinarySearchTree<ItemType>& righthandside); }; // end BinarySearchTree #include BinarySearchTree.cpp #endif

Saving a in a File Saving the binary search tree involves saving its values and restoring it correctly. Saving a binary search tree and then restoring it to its original shape: First algorithm restores a binary search tree to exactly the same shape it had before it was saved

Saving a in a File An initially empty binary search tree after the addition of 60, 20, 10, 40, 30, 50, and 70

Saving a in a File An initially empty binary search tree after the addition of 60, 20, 10, 40, 30, 50, and 70 If you now use the add method to add these values to a BST you recover the original tree.

Saving a in a File Saving a binary search tree and then restoring it to a balanced shape: Do we necessarily want to recover its original shape? We can use the same data to crate set of BSTs The shape of the BST affects efficiency of operations We decide to recover a balanced tree.

Saving a in a File Saving a binary search tree and then restoring it to a balanced shape: Do we necessarily want to recover its original shape? We can use the same data to crate set of BSTs The shape of the BST affects efficiency of operations We decide to recover a balanced tree. Remember:

Saving a in a File Use preorder traversal to save binary search tree in a file Restore to original shape by using method add

Saving a in a File Use preorder traversal to save binary search tree in a file Restore to original shape by using method add Balanced binary search tree increases efficiency of ADT operations

Saving a in a File Gaining Insight: A full tree saved in a file by using inorder traversal

Saving a in a File Gaining Insight: A full tree saved in a file by using inorder traversal But trees are not always full What we care most is to have a tree of minimum height

Saving a in a File Gaining Insight: A tree of minimum height that is not complete

The Class BinarySearchTree Building a minimum-height binary search tree // Builds a minimum-height binary search tree from n sorted values in a file. // Returns a pointer to the tree s root. readtree(treeptr: BinaryNodePointer, n: integer): BinaryNodePointer { if (n > 0) { treeptr = pointer to new node with nullptr as its child pointers // Construct the left subtree leftptr = readtree(treeptr->getleftchildptr(), n / 2) treeptr->setleftchildptr(leftptr) // Get the data item for this node rootitem = next data item from file treeptr->setitem(rootitem) // Construct the right subtree rightptr = readtree(treeptr->getrightchildptr(), (n-1) / 2) treeptr->getrightchildptr(rightptr) } } return treeptr return nullptr

The Class BinarySearchTree Building a minimum-height binary search tree In conclusion: Easy to build a balanced tree if the data in the file is sorted. You need n so that you can determine the middle and, in turn, the number of nodes in the left and right subtrees of the tree s root. Knowing these numbers is a simple matter of counting nodes as you traverse the tree and then saving the number in a file that the restore operation can read.

Tree Sort Tree sort uses a binary search tree // Sorts the integers in an array into ascending order treesort(anarray: array, n: integer) { Add anarray s entries to a binary search tree bst Traverse bst in inorder. As you visit bst s nodes, copy their data items into successive locations of anarray }

General Trees A general tree or an n-ary tree with n=3

General Trees An implementation of a general tree and its equivalent binary tree

General Trees An implementation of the n-ary tree

Thank you