Presentation is loading. Please wait.

Presentation is loading. Please wait.

Monday, March 19, 2018 Announcements… For Today… For Next Time…

Similar presentations


Presentation on theme: "Monday, March 19, 2018 Announcements… For Today… For Next Time…"— Presentation transcript:

1 Monday, March 19, 2018 Announcements… For Today… For Next Time…
Trees Announcements… Lab 07 3D Maze Peer Reviews due Friday. Lab 08 BST due Friday. Midterm Exam II 03/29-04/03. Average of Peer Reviews Direct s to Submitting inaccurate Peer Reviews is DISHONEST! Sharing Attendance Quiz Codes is CHEATING! For Today… 8.3-4 For Next Time… 8.5

2 Attendance Quiz #28 Trees

3 Tip #25: Iterator Requirements
Trees Iterators open the door for expression, readability, maintenance and reusability. How do I create a custom iterator? First, you have to decide how to define the state of your iterator, and how to define begin and end conditions. Then, you have to decide what the contents of the iterator will be. In other words, you have to decide on the value type, when the iterator is dereferenced. How many functions must a custom iterator implement? iterators should have a constructor. iterators should be comparable. iterators should dereference to a data item. iterators should increment (and decrement if possible). iterators should be instantiable with begin() and end() functions.

4 Lab 08 - BST 8.2, pgs

5 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."

6 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

7 Height: Root Predecessor: Root Successor: Full  Complete 3 6 10 
4 6 10 8 4 12 2 6 10 14 8 4 12 2 6 10 14 11 15 Height: Root Predecessor: Root Successor: Full Complete 4 7 10 Height: Root Predecessor: Root Successor: Full Complete 4 7 9 8 4 12 2 6 10 14 1 3 5 7 8 4 12 2 6 10 14 5 7 9 11 13 15 Height: Root Predecessor: Root Successor: Full Complete 4 10 Height: Root Predecessor: Root Successor: Full Complete 4 6 10 8 4 12 2 10 14 1 15 8 4 12 2 6 10 14 1 3 5

8 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

9 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

10 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)

11 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

12 Delete node 8 Delete node 17 Delete node 40 Delete node 40 8 4 12 2 6
5 7 12 4 22 2 17 1 16 20 Delete node 40 Delete node 40 40 4 45 34 42 55 13 15 47 49 48 75 14 3 40 4 45 3 20 42 55 15 34 48 25 47 49

13 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.

14 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

15 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.

16 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

17 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

18 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; ?

19 8.2, pgs. 457-465 8.3 Implementing a Binary_Tree Class
The BTNode Class The Binary_Tree Class Copy Constructor, Assignment, and Destructor 8.2, pgs

20 The BTNode Class Trees #ifndef BTNODE_H #define BTNODE_H #include <sstream> using namespace std; /** A node for a Binary Tree. */ template<typename T> struct BTNode { T data; BTNode<T>* left; BTNode<T>* right; BTNode(const T& data, BTNode<T>* left = NULL, BTNode<t>* right = NULL) : this->data(data), this->left(left), this->right(right) {} virtual ~BTNode() {} virtual std::string toString() const ostringstream os; os << data; return os.str(); } friend ostream& operator<<(ostream& out, const BTNode<T>& node) return out << node.toString(); }; // end BTNode #endif // BTNODE_H Like a linked list, a node consists of a data part and links to successor nodes. So that we can store any kind of data in a tree node, we make the data part an object of type T. A binary tree node must have links (pointers) to both its left and right subtrees.

21 The BTNode Class Trees bT.root->left points to the left subtree of the root (the root node of tree x + y).

22 The BTNode Class Trees bT.root.right points to the right subtree of the root (the root node of tree a / b)

23 bT.root->right.data contains the char object '/'
The BTNode Class Trees bT.root->right.data contains the char object '/'

24


Download ppt "Monday, March 19, 2018 Announcements… For Today… For Next Time…"

Similar presentations


Ads by Google