HERE ARE THE INSTRUCTIONS FOR THE ASSIGNMENT. I NEED THE CODE DONE IN C++ PLEASE
ID: 3787142 • Letter: H
Question
HERE ARE THE INSTRUCTIONS FOR THE ASSIGNMENT. I NEED THE CODE DONE IN C++ PLEASE. THANK YOU
Begin the implementation of a binary C++ persistent vector like that described here: http://hypirion.com/musings/understanding-persistent-vector-pt-1 and http://hypirion.com/musings/understanding-persistent-vector-pt-2.
That is, the tree should be a binary tree rather than the more "performant" 32-ary one described in part 2.
For this assignment, you must provide
1) a constructor that takes a constant reference to an std::vector and results in a new persistent vector with the same contents. You should copy the contents of the vector into the leaves of the trie.
2) The trie should be composed of std::shared_pointers so that when the persistent vector is destructed, the elements will be automatically destructed (so long as no other copies have been made)
3) A copy constructor that takes a constant reference to another persistent_vector and produces a copy that shares the other vector's trie.
4) An assignment operator that takes a constant reference to another persistent_vector and produces a copy that shares the other vector's trie.
5) a bracket operator that returns a constant reference to the indexed element.
Your submission should be a single C++ header file that provides the implementation of the persistent vector. Use the attached header file as a starting point.
When finished, running test-1.cpp should yield something like:
persistent_vector of 11 elements with height 3
node 0x7ff331d004a0 : 0x7ff331d00400 0x7ff331d00430
node 0x7ff331d00400 : 0x7ff331d002d0 0x7ff331d00330
node 0x7ff331d002d0 : 0x7ff331d00030 0x7ff331d00090
leaf 0x7ff331d00030 : 0 1
leaf 0x7ff331d00090 : 2 3
node 0x7ff331d00330 : 0x7ff331d00100 0x7ff331d000e0
leaf 0x7ff331d00100 : 4 5
leaf 0x7ff331d000e0 : 6 7
node 0x7ff331d00430 : 0x7ff331d00140 0x0
node 0x7ff331d00140 : 0x7ff331d001b0 0x7ff331d00280
leaf 0x7ff331d001b0 : 8 9
leaf 0x7ff331d00280 : 10
element at index 3 is 3
Where the hexadecimal numbers can be any unique identifier so long as it is possible to reconstruct the tree from the statements. For my implementation, I just printed the value of the pointer to each node as the unique identifier.
HERE IS THE CODE WE ARE SUPPOSE TO USE. THE HEADER FILE:
THE MAIN FILE:
Explanation / Answer
Below is the program of a binary tree with vector properties. nn return; } isBST(node->right); return; } int treeSize(struct Tree *node) { if(node == NULL) return 0; else return treeSize(node->left) + 1 + treeSize(node->right); } int maxDepth(struct Tree *node) { if(node == NULL || (node->left == NULL && node->right == NULL)) return 0; int leftDepth = maxDepth(node->left); int rightDepth = maxDepth(node->right); return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; } int minDepth(struct Tree *node) { if(node == NULL || (node->left == NULL && node->right == NULL)) return 0; int leftDepth = minDepth(node->left); int rightDepth = minDepth(node->right); return leftDepth left; return node; } /* Tree Maximum */ Tree* maxTree(struct Tree *node) { while(node->right) node = node -> right; return node; } /* In Order Successor - a node which has the next higher key */ Tree *succesorInOrder(struct Tree *node) { /* if the node has right child, seccessor is Tree-Minimum */ if(node->right != NULL) return minTree(node->right); Tree *y = node->parent; while(y != NULL && node == y->right) { node = y; y = y->parent; } return y; } /* In Order Predecessor - a node which has the next lower key */ Tree *predecessorInOrder(struct Tree *node) { /* if the node has left child, predecessor is Tree-Maximum */ if(node->left != NULL) return maxTree(node->left); Tree *y = node->parent; /* if it does not have a left child, predecessor is its first left ancestor */ while(y != NULL && node == y->left) { node = y; y = y->parent; } return y; } void reverseOrderPrint(struct Tree *node) { if(node == NULL) return; if(node->left == NULL && node->right == NULL) { cout left == q || node->right == p || node->right == q) return node; left = lowestCommonAncestor(node->left,p,q); right = lowestCommonAncestor(node->right, p,q); if(left && right) return node; else return (left) ? left : right; } void clear(struct Tree *node) { if(node != NULL) { clear(node->left); clear(node->right); delete node; } } /* print tree in order */ /* 1. Traverse the left subtree. 2. Visit the root. 3. Traverse the right subtree. */ void printTreeInOrder(struct Tree *node) { if(node == NULL) return; printTreeInOrder(node->left); cout left); printTreePostOrder(node->right); cout right == NULL) { cout right == NULL) { path[level] = node->data; for(int i = 0; i data != r2->data) return false; return (matchTree(r1->left, r2->left) && matchTree(r1->right, r2->right)); } bool subTree(Tree *r1, Tree *r2) { /*Big tree empty and subtree not found */ if(r1 == NULL) return false; if(r1->data == r2->data) if(matchTree(r1, r2)) return true; return (subTree(r1->left, r2) || subTree(r1->right, r2)); } bool isSubTree(Tree *r1, Tree *r2) { /* Empty tree is subtree */ if(r2 == NULL) return true; else return subTree(r1, r2); } /* change a tree so that the roles of the left and right hand pointers are swapped at every node */ void mirror(Tree *r) { if(r == NULL) return; Tree *tmp; mirror(r->left); mirror(r->right); /* swap pointers */ tmp = r->right; r->right = r->left; r->left = tmp; } /* create a new tree from a sorted array */ Tree *addToBST(char arr[], int start, int end) { if(end data = arr[mid]; r->left = addToBST(arr, start, mid-1); r->right = addToBST(arr, mid+1, end); return r; } Tree *createMinimalBST(char arr[], int size) { return addToBST(arr,0,size-1); } /* Breadth first traversal using queue */ void BreadthFirstTraversal(Tree *root) { if (root == NULL) return; deque queue; queue.push_back(root); while (!queue.empty()) { Tree *p = queue.front(); cout left); if (p->right != NULL) queue.push_back(p->right); } cout left, elm, level+1); else return getLevel(node->right, elm, level+1); } /* This code prints out all nodes at the same depth (level) */ void BreadthFirst_LevelElement_Print (struct Tree *root, vector &v;) { if(root == NULL) return; deque q; q.push_back(root); while(!q.empty()) { Tree *p = q.front(); int lev = getLevel(root, p->data, 0); v[lev].push_back(p->data); q.pop_front(); if(p->left) q.push_back(p->left); if(p->right)q.push_back(p->right); } return; } /* levelPrint() prints nodes at the same level This is simpler than the BreadthFirstTraversal(root) above It takes 2D vector with the same size of level (= MaxDepth+1) and fills elements as we traverse (preOrder) */ void levelPrint(struct Tree *node, vector&elm;, int level) { if(node == NULL) return; // leaf nodes if(node->left == NULL && node->right == NULL) { elm[level].push_back(node->data); return; } // other nodes elm[level++].push_back(node->data); levelPrint(node->left, elm, level); levelPrint(node->right, elm, level); } /* find n-th max node from a tree */ void NthMax(struct Tree* t) { static int n_th_max = 5; static int num = 0; if(t == NULL) return; NthMax(t->right); num++; if(num == n_th_max) cout data); else oddVec.push_back(p->data); que.pop_front(); if(p->left) que.push_back(p->left); if(p->right) que.push_back(p->right); } coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.