CS261 – Data Structures Graphs. Goals Introduction and Motivation Representations.
CS261 Data Structures Binary Search Trees II Bag Implementation.
-
Upload
luke-terry -
Category
Documents
-
view
226 -
download
0
Transcript of CS261 Data Structures Binary Search Trees II Bag Implementation.
![Page 1: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/1.jpg)
CS261 Data Structures
Binary Search Trees IIBag Implementation
![Page 2: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/2.jpg)
![Page 3: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/3.jpg)
Goals
• BST Representation• Bag Operations• Functional-style operations
![Page 4: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/4.jpg)
Binary Search Tree (review)
• Binary search trees are binary trees where every nodes object value is:– Greater than all its descendents in the left subtree– Less than or equal to all its descendents in the right
subtree
• In-order traversal returns elements in sorted order
• If tree is reasonably full (well balanced), searching for an element is O(log n)
![Page 5: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/5.jpg)
Binary Search Tree (review)
Alex
Abner Angela
Adela Alice Audrey
Adam Agnes Allen Arthur
Abigail
![Page 6: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/6.jpg)
BST Bag
struct BSTree { struct Node *root; int cnt;};
void initBSTree(struct BSTree *tree);void addBSTree(struct BSTree *tree, TYPE val);int containsBSTree(struct BSTree *tree, TYPE val);void removeBSTree(struct BSTree *tree, TYPE val);int sizeBSTree(struct BSTree *tree);
struct Node { TYPE val; struct Node *left struct Node *rght};
![Page 7: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/7.jpg)
Functional Approach
A useful trick (adapted from the functional programming world): Recursive helper routine that returns tree with the value inserted
Node addNode(Node current, TYPE value) if current is null then return new Node with value otherwise if value < Node.value left child = addNode(left child, value) else right child = addNode(right child, value) return current node
![Page 8: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/8.jpg)
Visual Example
![Page 9: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/9.jpg)
Process Stack
![Page 10: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/10.jpg)
BST Add: public facing add
void add(struct BSTree *tree, TYPE val) { tree->root = _addNode(tree->root, val); tree->cnt++;}
![Page 11: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/11.jpg)
Recursive Helper – functional flavor
struct Node *_addNode(struct Node *cur, TYPE val){ struct Node *newnode; if (cur == NULL){ /* base case goes here */
return newNode } if (val < cur->val) /* recursive call left */ else /*recursive call right */ return cur;}
![Page 12: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/12.jpg)
Iterative Flavor - Java
public void insert(int data) { if (m_root == null) { m_root = new TreeNode(data, null, null); return; } Node root = m_root; while (root != null) {
if (data == root.getData()) { return; } else if (data < root.getData()) {
if (root.getLeft() == null) { root.setLeft(new TreeNode(data, null,
null)); return; } else { root = root.getLeft(); } }
![Page 13: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/13.jpg)
Iterative (Java)
} else {if (root.getRight() == null) {
root.setRight(new TreeNode(data, null, null));
return; } else { root = root.getRight(); } } }}
![Page 14: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/14.jpg)
BST Remove
Alex
Abner Angela
Adela Alice Audrey
Adam Agnes Allen Arthur
Abigail
How would you remove Abigail? Audrey? Angela?
![Page 15: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/15.jpg)
Who fills the hole?
• Answer: the leftmost child of the right subtree (smallest element in right subtree)
• Useful to have a couple of private inner routines:
TYPE _leftmost(struct Node *cur) { ... /* Return value of leftmost child of current node. */}
struct Node *_removeLeftmost(struct Node *cur) { ... /* Return tree with leftmost child removed. */}
![Page 16: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/16.jpg)
Intuition…
50
80
70
6061
62
63
![Page 17: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/17.jpg)
BST Remove Example
Alex
Abner Angela
AdelaAbigail Alice Audrey
Adam Agnes Allen Arthur
Before call to remove
Element toremove
Replace with:leftmost(rght)
![Page 18: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/18.jpg)
BST Remove Example
Alex
Abner Arthur
AdelaAbigail Alice
Adam Agnes Allen
Audrey
After call to remove
![Page 19: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/19.jpg)
BST Remove Pseudocode
Node removeNode(Node current, TYPE value)if value = current.value
if right child is null return left child
else replace value with value in leftmost child of right
subtree set right child to result of removeLeftmost(right)
else if value < current.valueleft child = removeNode(left child, value)
else right child = removeNode(right child, value)return current node
![Page 20: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/20.jpg)
Comparison
• Average Case Execution Times
Operation DynArrBag LLBag OrderedArrBag
BST Bag
Add O(1+) O(1) O(n) O(logn)
Contains O(n) O(n) O(logn) O(logn)
Remove O(n) O(n) O(n) O(logn)
![Page 21: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/21.jpg)
Space Requirements
• Does the functional-style recursive version require more or less space than an iterative version? Think about the call stack?– rebuilding as move up from recursion requires
O(logn) space on average
![Page 22: CS261 Data Structures Binary Search Trees II Bag Implementation.](https://reader035.fdocuments.us/reader035/viewer/2022081503/56649f155503460f94c2a57b/html5/thumbnails/22.jpg)
Your Turn
• Complete the BST implementation in Worksheet #29