Lab 08 - BST.

Slides:



Advertisements
Similar presentations
Computer Science C++ High School Level By Guillermo Moreno.
Advertisements

Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
CSCS-200 Data Structure and Algorithms Lecture
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Search Trees CS340. Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty If.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Lecture 9 Binary Trees Trees General Definition Terminology
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Chapter 12 – Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
BST Trees
Binary Search Tree (BST)
Problems with Linked List (as we’ve seen so far…)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Monday, March 19, 2018 Announcements… For Today… For Next Time…
Binary Search Trees.
Chapter 20: 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.
8.1 Tree Terminology and Applications
Lab 10 - Quicksort.
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Lab 04 – Linked List.
Wednesday, April 11, 2018 Announcements… For Today…
Lab 03 - Iterator.
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
CMSC 341 Binary Search Trees.
Podcast Ch18b Title: STree Class
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Pointers & Dynamic Data Structures
CMSC 341 Binary Search Trees.
Mark Redekopp David Kempe
Trees.
CMSC 341 Binary Search Trees 2/21/2006.
Binary Search Trees CS 580U Fall 17.
Data Structures Using C++ 2E
Lab 03 – Linked List.
16 – Sequential Containers
8.1 Tree Terminology and Applications
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Lab 08 - BST

L08 – Binary Search Tree Trees "A binary search tree (BST), which may sometimes be called an ordered or sorted binary tree, is a node- based data structure where each node references a value, a left child, and a right child. In a Binary Search Tree, the left subtree of a node contains only nodes with values less than the node's value, the right subtree of a node contains only nodes with values greater than the node's value, there are no duplicate nodes, and both left and right subtrees of a node must also be binary search trees."

Traversal Trees pre_order_traversal(node) if node is NULL return visit node pre_order_traversal(node.left) return pre_order_traversal(node.right) Full tree internal nodes w/2 children Complete tree unfilled nodes right on row h Predecessor left, right-most node Successor right, left-most node in_order_traversal(node) if node is NULL return in_order_traversal(node.left) visit node return in_order_traversal(node.right) 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 post_order_traversal(node) if node is NULL return post_order_traversal(node.left) post_order_traversal(node.right) visit node return

Search Find node 9 bool search(node, value) if node is NULL Trees bool search(node, value) if node is NULL return false if node.value == value return true if value < node.value return search(node.left, value) if value > node.value return search(node.right, value) 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Find node 9 Go right Go left

Insert Insert node 6 Insert node 13 Trees bool insert(Node*& node, const T& data) if node is NULL node = new Node(data) return true if data == node->data return false if data < node->data return insert(node->left, data); if data > node->data return insert(node->right, data); 8 4 12 2 6 10 14 1 3 11 13 15 8 4 12 2 10 14 1 3 11 15 8 4 12 2 6 10 14 1 3 15 Insert node 6 Go left Insert as 4's right child Insert node 13 Go right, right Insert as 14's leftchild

Delete Delete node 2 bool delete(Node*& node, const T& data) Trees bool delete(Node*& node, const T& data) if node is NULL, return false if data < node->data, return delete(node->left, data) if data > node->data, return delete(node->right, data) if node has no children, parent = NULL, return true if node has 1 child, parent = node->(left or right), return true exchange node->data with in_order_predecessor->data return delete(node->left, data) 8 3 9 2 4 12 11 1 5 8 3 9 2 4 12 11 5 1 Delete node 2 Find node (2) Set parent (3) to child (1)

Delete Delete node 2 Delete node 8 Trees bool delete(Node*& node, const T& data) if node is NULL, return false if data < node->data, return delete(node->left, data) if data > node->data, return delete(node->right, data) if node has no children, parent = NULL, return true if node has 1 child, parent = node->(left or right), return true exchange node->data with in_order_predecessor->data return delete(node->left, data) 8 3 9 2 4 12 11 5 1 8 3 9 2 4 12 11 1 5 Swap data 5 8 Delete node 2 Find node (2) Set parent (3) to child (1) Delete node 8 Find predecessor (5) Exchange data only with node Delete 8 from left tree

BST Commands Add <data> Recursion COMMAND DESCRIPTION OUTPUT Add <data> Add data node to BST. Return false if duplicate. True False Remove <data> Remove node from BST. Return false if not found. True False Clear Delete all nodes from the BST. True PrintBST Print BST (using insertion operator) in level-order. Level-order listing of BST. Find <data> (Bonus) Find and display node in BST. Return "Found" or "Not Found". Found Not Found! Tree (Bonus) Output the contents of the BST using begin() and end() iterators in in-order. In-order listing of BST.

Level-Order Traversal Trees /** Output nodes at a given level */ bool outLevel(Node<T>* root, int level, stringstream& out) const { if (root == NULL) return false; if (level == 0) out << " " << root->data; if ((root->left != NULL) || (root->right != NULL)) return true; return false; } if ((level == 1) && !root->left_ && root->right_) out << " _"; bool left = outLevel(root->left, level - 1, out); bool right = outLevel(root->right, level - 1, out); if ((level == 1) && root->left_ && !root->right_) out << " _"; return left || right; } // end outLevel() int level = -1; do out << endl << " " << ++level << ":"; } while (outLevel(root, level, out)); Add 2 True Add 3 True Add 4 True PrintBST 0: 2 1: _ 3 2: _ 4

Requirements Trees Points Requirement (35 Points) 5 argv[1] and argv[2] used for input / output streams respectively. No execution user interaction (i.e. system("pause"); or getchr();). A BST class is derived from the abstract BSTInterface interface class. No STL container is used anywhere in your BST class. The BST class implements a public toString and friend insertion member function. The "PrintBST" command uses the insertion operator function to send the contents of the BST container (in level-order) to the output stream. If the BST is empty, report "Empty". Any number of Nodes are correctly added to the BST using the "Add" command. Attempt to add a duplicate node outputs false. (lab08_in_01.txt) Nodes are correctly removed from the BST using the "Remove" command. Attempt to remove a non-existant node outputs false. (lab08_in_02.txt) The "Clear" command removes every node from the tree. (lab08_in_03.txt) BONUS: The "Find" uses a BST iterator and reports "Found" if the value is in the BST and "Not Found" if not found. (lab08_in_04.txt) BONUS: The "Tree" command displays the tree in in-order using BST iterators. (lab07_in_04.txt) VS_MEM_CHECK macro is included in main to detect memory leaks. No Memory leaks are reported.

Steps 1 - 3 BST Class Software Design #include "BSTInterface.h" template<typename T> class BST : public BSTInterface<T> { private: struct Node T data_; Node* left_; Node* right_; Node(T d) : data_(d), left_(NULL), right_(NULL) { } }; Node* root_; public: BST() { this->root_ = NULL; } ~BST() { clearTree(); } /** Return true if node added to BST, else false */ virtual bool addNode(const T& data) { return insert(this->root_, data); } /** Return true if node removed from BST, else false */ virtual bool removeNode(const T& data) { return remove(this->root_, data); } /** Return true if BST cleared of all nodes, else false */ virtual bool clearTree() { return deleteTree(root_); } /** Return a level order traversal of a BST as a string */ virtual string toString() const { ... } /** Override insertion operator to insert BST string */ friend std::ostream& operator<< (ostream& os, const BST<T>& bst) { ... } Class BST inherits from the interface template BSTInterface class. structs are lightweight objects and often used in place of a class if the object's main responsibility is a type of data storage. A node represents a single value, is similar to a primitive type, is mutable, and relatively small, and hence a good candidate for a struct

Step 3 – Add an Iterator (Bonus) Software Design #include "BSTInterface.h" template<typename T> class BST : public BSTInterface<T> { private: struct Node { ... }; Node* root_; public: BST() { this->root_ = NULL; } ~BST() { clearTree(); } /** BST iterator */ class Iterator mutable int index_; Iterator(int index, Node* root) : index_(index), root_(root) {} ~Iterator() {} virtual bool operator!=(const Iterator& rhs) const { ... } virtual Iterator operator++(T) { ... } virtual T operator*() const { ... } }; virtual Iterator find(T& value) { ... } virtual Iterator begin() { ... } virtual Iterator end() { ... } if (item1 == "Find") { out << endl << "Find " << data; BST<int>::Iterator iter = bst.find(data); BST<int>::Iterator end_iter = bst.end(); if (!(iter != end_iter)) out << " Not Found"; else out << " Found " << *iter; } else if (item1 == "Tree") out << endl << "Tree "; BST<int>::Iterator iter = bst.begin(); if (iter == end_iter) out << " Empty"; for (; iter != end_iter; iter++) out << " " << *iter; BST Iterator Nested classes can access all members of the parent via a reference/pointer

Bonus: Iterator BST<int> 8 3 9 2 4 12 11 5 1 Iterator begin() Trees BST<int> Node* root_; 8 3 9 2 4 12 11 5 1 Iterator int index_ = 0; Node* root_; begin() Iterator int index_ = 7; Node* root_; end() BST<int>::Iterator iter = bst.begin(); BST<int>::Iterator end_iter = bst.end(); if (iter == end_iter) out << " Empty"; for (; iter != end_iter; iter++) out << " " << *iter; ?