This document contains code for a simple ring buffer implementation in C. Ring buffers are commonly used in microcontrollers with limited memory to hold serial data until a full packet is received. The code defines a ring buffer struct, initialization and enqueue/dequeue functions, and provides sample usage comments. The ring buffer uses a static array and input/output indices to store and access data in a first-in, first-out order.
Cognitive and Knowledge Development
A presentation of learning theories that explain the differences between the ways children think and develop and the ways adults learn.
I was shown this powerpoint about Piaget and Vygotsky in my EDU 280: Introduction to Adolescent Education class. I find their theories to be very helpful and thought-provoking.
Cognitive and Knowledge Development
A presentation of learning theories that explain the differences between the ways children think and develop and the ways adults learn.
I was shown this powerpoint about Piaget and Vygotsky in my EDU 280: Introduction to Adolescent Education class. I find their theories to be very helpful and thought-provoking.
Please fix my errors class Iterator public Construc.pdfkitty811
Please fix my errors
class Iterator
{
public:
/// Constructor taking a head and tail pointer; YOU'RE WELCOME
Iterator(Node<T> *head, Node<T> *tail) : head_(head), tail_(tail)
{
cursor_ = end();
}
/// Constructor taking a head, tail, and cursor pointer; YOU'RE WELCOME
Iterator(Node<T> *head, Node<T> *tail, Node<T> *cursor) : head_(head), tail_(tail),
cursor_(cursor) {}
/// Get a pointer to the head node, or end() if this list is empty
Node<T> *begin() { return head_; }
/// Get a node pointer representing "end" (aka "depleted"). Probably want to just use nullptr.
Node<T> *end() { return nullptr; }
/// Get the node to which this iterator is currently pointing
Node<T> *getCursor() { return cursor_; }
/**
* Assignment operator
* Return a copy of this Iterator, after modification
*/
Iterator &operator=(const Iterator &other)
{
head_ = other.head_;
tail_ = other.tail_;
cursor_ = other.cursor_;
return *this;
}
/**
* Comparison operator
* Returns true if this iterator is equal to the other iterator, false otherwise
*/
bool operator==(const Iterator &other)
{
return cursor_ == other.cursor_;
}
/**
* Inequality comparison operator
* Returns true if this iterator is not equal to the other iterator, false otherwise
*/
bool operator!=(const Iterator &other)
{
return !(*this == other);
}
/**
* Prefix increment operator
* Return a copy of this Iterator, after modification
*/
Iterator &operator++()
{
cursor_ = cursor_->getNext();
return *this;
}
/*** Postfix increment
* Return a copy of this Iterator, BEFORE it was modified
*/
Iterator operator++(int)
{
Iterator copy(*this);
++(*this);
return copy;
}
/*** Prefix decrement operator
* Return a copy of this Iterator, after modification
*/
Iterator &operator--()
{
cursor_ = cursor_->getPrev();
return *this;
}
/**
* Postfix decrement operator
* Return a copy of this Iterator BEFORE it was modified
*/
Iterator operator--(int)
{
Iterator copy = *this;
--(*this);
return copy;
}
/**
* AdditionAssignment operator
* Return a copy of the current iterator, after modification
*/
Iterator operator+=(size_t add)
{
for (size_t i = 0; i < add && cursor_ != nullptr; ++i)
{
cursor_ = cursor_->getNext();
}
return *this;
}
/**
* SubtractionAssignment operator
* Return a copy of the current iterator, after modification
*/
Iterator operator-=(size_t sub)
{
for (size_t i = 0; i < sub && cursor_ != nullptr; ++i)
{
cursor_ = cursor_->getPrev();
}
return *this;
}
/**
* AdditionAssignment operator, supporting positive or negative ints
*/
Iterator operator+=(int add)
{
if (add > 0)
{
for (int i = 0; i < add && cursor_ != nullptr; ++i)
{
cursor_ = cursor_->getNext();
}
}
else
{
for (int i = 0; i > add && cursor_->getPrev() != nullptr; --i)
{
cursor_ = cursor_->getPrev();
}
}
return *this;
}
/**
* SubtractionAssignment operator, supporting positive or negative ints
*/
Iterator operator-=(int subtract)
{
// This function adds an integer to the current iterators position in the list,
if (subtract > 0)
{
for (int i = 0; i < subtract && cursor_.
Help to implement delete_node get_succ get_pred walk and.pdfcontact32
Help to implement delete_node, get_succ, get_pred, walk, and tree_search in the script
following the given structure:
#include "bst.h"
// ---------------------------------------
// Node class
// Default constructor
Node::Node() {
// TODO: Implement this
key = 0;
parent = nullptr;
left = nullptr;
right = nullptr;
}
// Constructor
Node::Node(int in) {
// TODO: Implement this
key = in;
parent = nullptr;
left = nullptr;
right = nullptr;
}
// Destructor
Node::~Node() {
// TODO: Implement this
delete left;
delete right;
}
// Add parent
void Node::add_parent(Node* in) {
// TODO: Implement this
parent = in;
}
// Add to left of current node
void Node::add_left(Node* in) {
// TODO: Implement this
left = in;
if (in != nullptr) {
in->add_parent(this);
}
}
// Add to right of current node
void Node::add_right(Node* in) {
// TODO: Implement this
right = in;
if (in != nullptr) {
in->add_parent(this);
}
}
// Get key
int Node::get_key()
{
// TODO: Implement this
return key;
}
// Get parent node
Node* Node::get_parent()
{
// TODO: Implement this
return parent;
}
// Get left node
Node* Node::get_left()
{
// TODO: Implement this
return left;
}
// Get right node
Node* Node::get_right()
{
// TODO: Implement this
return right;
}
// Print the key to ostream to
// Do not change this
void Node::print_info(ostream& to)
{
to << key << endl;
}
// ---------------------------------------
// ---------------------------------------
// BST class
// Walk the subtree from the given node
void BST::inorder_walk(Node* in, ostream& to)
{
// TODO: Implement this
if (in != nullptr) {
inorder_walk(in->get_left(), to);
in->print_info(to);
inorder_walk(in->get_right(), to);
}
}
// Constructor
BST::BST()
{
// TODO: Implement this
root = nullptr;
}
// Destructor
BST::~BST()
{
// TODO: Implement this
delete root;
}
// Insert a node to the subtree
void BST::insert_node(Node* in)
{
// TODO: Implement this
Node* curr = root;
Node* par = nullptr;
while (curr != nullptr) {
par = curr;
if (in->get_key() < curr->get_key()) {
curr = curr->get_left();
} else {
curr = curr->get_right();
}
}
in->add_parent(par);
if (par == nullptr) {
root = in;
} else if (in->get_key() < par->get_key()) {
par->add_left(in);
} else {
par->add_right(in);
}
}
// Delete a node to the subtree
void BST::delete_node(Node* out)
{
// TODO: Implement this
}
// minimum key in the BST
Node* BST::tree_min()
{
// TODO: Implement this
return get_min(root);
}
// maximum key in the BST
Node* BST::tree_max()
{
// TODO: Implement this
return get_max(root);
}
// Get the minimum node from the subtree of given node
Node* BST::get_min(Node* in)
{
// TODO: Implement this
if (in == nullptr) {
return nullptr;
}
while (in->get_left() != nullptr) {
in = in->get_left();
}
return in;
}
// Get the maximum node from the subtree of given node
Node* BST::get_max(Node* in)
{
// TODO: Implement this
if (in == nullptr) {
return nullptr;
}
while (in->get_right() != nullptr) {
in = in->get_right();
}
return in;
}
// Get successor of the given node
Node* BS.
I am having problems getting the correct output- Here are the instruct (1).pdfsonunotwani
I am having a hard time with getting my code to write the txt file. Any ideas?
<!DOCTYPE html>
<html>
<head>
<title>Song Organizer</title>
<meta name="Student" content="Student" />
<!-- create a song organizer script -->
</head>
<body>
<h1>Song Organizer</h1>
<?php
// create a song organizer script
$filename = 'songs.txt';
$filepath = __DIR__ . '/' . $filename;
$songs = [];
if (isset($_GET['action']))
{
if ((file_exists("SongOrganizer2/songs.txt"))
&& (filesize("SongOrganizer2/songs.txt")!= 0))
{$SongArray = file("SongOrganizer2/songs.txt");
switch ($_GET['action'])
{
case 'Remove Duplicates';
$SongArray = array_unique($SongArray);
$SongArray = array_values($SongArray);
break;
case 'Sort Ascending':
sort($SongArray);
break;
case 'Shuffle':
shuffle($SongArray);
break;
} // End of the switch statement
if (count($SongArray) > 0)
{
$NewSongs = implode($SongArray);
$SongStore = fopen("SongOrganizer2/songs.txt","wb");
if ($SongStore === false)
echo "There was an error updating the song file\n";
else
{fwrite($SongStore, $NewSongs);
fclose($SongStore);
}
} else
unlink("SongOrganizer2/songs.txt");
}
}
if(isset($_POST['submit']))
$SongToAdd = stripslashes($_POST['SongName']) . "\n";
$ExistingSongs = array();
if (file_exists("SongOrganizer2/songs.txt")
&filesize("SongOrganizer2/songs.txt")> 0)
{ $ExistingSongs = file("SongOrganizer2/songs.txt");}
if (in_array($SongToAdd, $ExistingSongs))
{echo "<p>The song you entered already exists!<br />\n";
echo "Your song was not added to the list.</p>";
}
else
{
$SongFile = fopen("SongOrganizer2/songs.txt","ab");
if ($SongFile === false)
echo "There was an error saving your message!\n";
else
{
fwrite($SongFile, $SongToAdd);
fclose($SongFile);
echo "Your song has been added to the list.\n";
}
}
if ((!file_exists("SongOrganizer2/songs.txt"))
|| (filesize("SongOrganizer2/songs.txt")== 0))
echo "<p>There are no songs in the list.</p>\n";
else
{
$SongArray = file("SongOrganizer2/songs.txt");
echo "<table border=\"1\" width=\"100%\" style=\"background-color:lightgray\">\n";
foreach ($SongArray as $Song) {
echo "<tr>\n";
echo "<td>" . htmlentities($Song) ."</td>";
echo "</tr>\n";}
echo "</table>\n";
}
?>
<p>
<a href="SongOrganizer2.php?action=Sort%20Ascending">
Sort Song List </a><br />
<a href="SongOrganizer2.php?action=Remove%20Duplicates">
Remove Duplicate Songs </a><br />
<a href= "SongOrganizer2.php?action=Shuffle">
Randomize Song List </a><br />
</p>
<form action="SongOrganizer2.php" method="post">
<p>Add a New Song</p>
<p>Song Name: <input type="text" name="SongName"/></p>
<p><input type="submit" name = 'submit'
value="Add Song to List" />
<input type="reset" name="reset" value="Reset Song Name" /></p>
</form>
</body>
</html>.
Help implement BST- The code must follow the instruction below as well.pdfa2zmobiles
Help implement BST. The code must follow the instruction below as well as produce the
correct output when testing with the input file I'll provide at the end. Please fill-in the
TODO parts in the bst.cc code:
INSTRUCTIONS:
3. First, implement the BST (declared in bst.h) and use the unittest_bst() function to test it. DO
NOT CHANGE THIS CODE FOR YOUR SUBMISSION, as it will be used to test your code
against the answers (with different seed values).
4. Implement the DB and use the unittest_db() function to test it. DO NOT CHANGE THIS
CODE FOR YOUR SUBMISSION, as it will be used to test your code against the answers (with
different input files).
a) The student ID should be the base class's key member.
b) The student ID should be automatically assigned such that the ID/key should be n if the
student is the nth student to join the school. If the student later leaves (i.e., deleted from the
BST), the ID does NOT get reassigned to another student. Thus, the student ID of the last student
to join the school should reflect the TOTAL number of students that have joined this school
since its reception (regardless of whether some have left).
5. Test your code against the provided input and output files.
a) The provided answer for the BST unit test is in "unittest_ans_t100_s100.txt". The s100 refers
to the seed of 100 (-s 100), and t100 refers to the number of elements to add to the BST (-t 100).
b) The provided answer for the DB is in "students_1_ans.txt" for the "students_1.txt" input file.
6. Make sure your code has no memory leaks (using valgrind).
7. Your code should work beyond the provided unit tests. That is, even if it does work for all the
given tests, if the code has an identifiable bug (i.e., by reading the source code), points WILL be
deducted.
For example, if I were to change
unittest_bst(num_test, seed, cout, 5); ->
unittest_bst(num_test, seed, cout, 100);
it should still work.
BST.H
#ifndef BST_H_
#define BST_H_
#include <iostream>
using namespace std;
class Node {
private:
int key;
Node* parent;
Node* left;
Node* right;
public:
// Default constructor
Node();
// Constructor
Node(int in);
// Destructor
// a virtual constructor is required for inheritance
virtual ~Node();
// Add to parent of current node
void add_parent(Node* in);
// Add to left of current node
void add_left(Node* in);
// Add to right of current node
void add_right(Node* in);
// Get key
int get_key();
// Get parent node
Node* get_parent();
// Get left node
Node* get_left();
// Get right node
Node* get_right();
virtual void print_info(ostream& to);
};
class BST {
private:
Node* root;
// Walk the subtree from the given node
void inorder_walk(Node* in, ostream& to);
// Get the minimum node from the subtree of given node
Node* get_min(Node* in);
// Get the maximum node from the subtree of given node
Node* get_max(Node* in);
public:
// Constructor and Destructor
BST();
~BST();
// Modify tree
void insert_node(Node* in);
void delete_node(Node* out);
// Find nodes in the tree
Node* t.
A class that automates conversion from a C++ recursive function to an iterative function. It allow the recursive function to preserve its structure by reproducing the "call stack" on an std::stack. The examples use combinatorics to illustrate usage.
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdfajay1317
/* # INPUT: a node z in the AVL Tree # OUTPUT: the new root node r of the subtree originally
rooted at z before the restructure/rebalancing at z # PRECONDITION: z is the only unbalanced
node in the subtree that it roots; the difference in height of its children is exactly 2 #
POSTCONDITION: the subtree originally rooted at z is a proper AVL subtree with node heights
properly set AVLTreeMap: : Node* AVLTreeMap::rebalance(AVLTreeMap::Node* z) {
AVLTreeMap::Node* r=z; // Your code here return r;.
Please fix my errors class Iterator public Construc.pdfkitty811
Please fix my errors
class Iterator
{
public:
/// Constructor taking a head and tail pointer; YOU'RE WELCOME
Iterator(Node<T> *head, Node<T> *tail) : head_(head), tail_(tail)
{
cursor_ = end();
}
/// Constructor taking a head, tail, and cursor pointer; YOU'RE WELCOME
Iterator(Node<T> *head, Node<T> *tail, Node<T> *cursor) : head_(head), tail_(tail),
cursor_(cursor) {}
/// Get a pointer to the head node, or end() if this list is empty
Node<T> *begin() { return head_; }
/// Get a node pointer representing "end" (aka "depleted"). Probably want to just use nullptr.
Node<T> *end() { return nullptr; }
/// Get the node to which this iterator is currently pointing
Node<T> *getCursor() { return cursor_; }
/**
* Assignment operator
* Return a copy of this Iterator, after modification
*/
Iterator &operator=(const Iterator &other)
{
head_ = other.head_;
tail_ = other.tail_;
cursor_ = other.cursor_;
return *this;
}
/**
* Comparison operator
* Returns true if this iterator is equal to the other iterator, false otherwise
*/
bool operator==(const Iterator &other)
{
return cursor_ == other.cursor_;
}
/**
* Inequality comparison operator
* Returns true if this iterator is not equal to the other iterator, false otherwise
*/
bool operator!=(const Iterator &other)
{
return !(*this == other);
}
/**
* Prefix increment operator
* Return a copy of this Iterator, after modification
*/
Iterator &operator++()
{
cursor_ = cursor_->getNext();
return *this;
}
/*** Postfix increment
* Return a copy of this Iterator, BEFORE it was modified
*/
Iterator operator++(int)
{
Iterator copy(*this);
++(*this);
return copy;
}
/*** Prefix decrement operator
* Return a copy of this Iterator, after modification
*/
Iterator &operator--()
{
cursor_ = cursor_->getPrev();
return *this;
}
/**
* Postfix decrement operator
* Return a copy of this Iterator BEFORE it was modified
*/
Iterator operator--(int)
{
Iterator copy = *this;
--(*this);
return copy;
}
/**
* AdditionAssignment operator
* Return a copy of the current iterator, after modification
*/
Iterator operator+=(size_t add)
{
for (size_t i = 0; i < add && cursor_ != nullptr; ++i)
{
cursor_ = cursor_->getNext();
}
return *this;
}
/**
* SubtractionAssignment operator
* Return a copy of the current iterator, after modification
*/
Iterator operator-=(size_t sub)
{
for (size_t i = 0; i < sub && cursor_ != nullptr; ++i)
{
cursor_ = cursor_->getPrev();
}
return *this;
}
/**
* AdditionAssignment operator, supporting positive or negative ints
*/
Iterator operator+=(int add)
{
if (add > 0)
{
for (int i = 0; i < add && cursor_ != nullptr; ++i)
{
cursor_ = cursor_->getNext();
}
}
else
{
for (int i = 0; i > add && cursor_->getPrev() != nullptr; --i)
{
cursor_ = cursor_->getPrev();
}
}
return *this;
}
/**
* SubtractionAssignment operator, supporting positive or negative ints
*/
Iterator operator-=(int subtract)
{
// This function adds an integer to the current iterators position in the list,
if (subtract > 0)
{
for (int i = 0; i < subtract && cursor_.
Help to implement delete_node get_succ get_pred walk and.pdfcontact32
Help to implement delete_node, get_succ, get_pred, walk, and tree_search in the script
following the given structure:
#include "bst.h"
// ---------------------------------------
// Node class
// Default constructor
Node::Node() {
// TODO: Implement this
key = 0;
parent = nullptr;
left = nullptr;
right = nullptr;
}
// Constructor
Node::Node(int in) {
// TODO: Implement this
key = in;
parent = nullptr;
left = nullptr;
right = nullptr;
}
// Destructor
Node::~Node() {
// TODO: Implement this
delete left;
delete right;
}
// Add parent
void Node::add_parent(Node* in) {
// TODO: Implement this
parent = in;
}
// Add to left of current node
void Node::add_left(Node* in) {
// TODO: Implement this
left = in;
if (in != nullptr) {
in->add_parent(this);
}
}
// Add to right of current node
void Node::add_right(Node* in) {
// TODO: Implement this
right = in;
if (in != nullptr) {
in->add_parent(this);
}
}
// Get key
int Node::get_key()
{
// TODO: Implement this
return key;
}
// Get parent node
Node* Node::get_parent()
{
// TODO: Implement this
return parent;
}
// Get left node
Node* Node::get_left()
{
// TODO: Implement this
return left;
}
// Get right node
Node* Node::get_right()
{
// TODO: Implement this
return right;
}
// Print the key to ostream to
// Do not change this
void Node::print_info(ostream& to)
{
to << key << endl;
}
// ---------------------------------------
// ---------------------------------------
// BST class
// Walk the subtree from the given node
void BST::inorder_walk(Node* in, ostream& to)
{
// TODO: Implement this
if (in != nullptr) {
inorder_walk(in->get_left(), to);
in->print_info(to);
inorder_walk(in->get_right(), to);
}
}
// Constructor
BST::BST()
{
// TODO: Implement this
root = nullptr;
}
// Destructor
BST::~BST()
{
// TODO: Implement this
delete root;
}
// Insert a node to the subtree
void BST::insert_node(Node* in)
{
// TODO: Implement this
Node* curr = root;
Node* par = nullptr;
while (curr != nullptr) {
par = curr;
if (in->get_key() < curr->get_key()) {
curr = curr->get_left();
} else {
curr = curr->get_right();
}
}
in->add_parent(par);
if (par == nullptr) {
root = in;
} else if (in->get_key() < par->get_key()) {
par->add_left(in);
} else {
par->add_right(in);
}
}
// Delete a node to the subtree
void BST::delete_node(Node* out)
{
// TODO: Implement this
}
// minimum key in the BST
Node* BST::tree_min()
{
// TODO: Implement this
return get_min(root);
}
// maximum key in the BST
Node* BST::tree_max()
{
// TODO: Implement this
return get_max(root);
}
// Get the minimum node from the subtree of given node
Node* BST::get_min(Node* in)
{
// TODO: Implement this
if (in == nullptr) {
return nullptr;
}
while (in->get_left() != nullptr) {
in = in->get_left();
}
return in;
}
// Get the maximum node from the subtree of given node
Node* BST::get_max(Node* in)
{
// TODO: Implement this
if (in == nullptr) {
return nullptr;
}
while (in->get_right() != nullptr) {
in = in->get_right();
}
return in;
}
// Get successor of the given node
Node* BS.
I am having problems getting the correct output- Here are the instruct (1).pdfsonunotwani
I am having a hard time with getting my code to write the txt file. Any ideas?
<!DOCTYPE html>
<html>
<head>
<title>Song Organizer</title>
<meta name="Student" content="Student" />
<!-- create a song organizer script -->
</head>
<body>
<h1>Song Organizer</h1>
<?php
// create a song organizer script
$filename = 'songs.txt';
$filepath = __DIR__ . '/' . $filename;
$songs = [];
if (isset($_GET['action']))
{
if ((file_exists("SongOrganizer2/songs.txt"))
&& (filesize("SongOrganizer2/songs.txt")!= 0))
{$SongArray = file("SongOrganizer2/songs.txt");
switch ($_GET['action'])
{
case 'Remove Duplicates';
$SongArray = array_unique($SongArray);
$SongArray = array_values($SongArray);
break;
case 'Sort Ascending':
sort($SongArray);
break;
case 'Shuffle':
shuffle($SongArray);
break;
} // End of the switch statement
if (count($SongArray) > 0)
{
$NewSongs = implode($SongArray);
$SongStore = fopen("SongOrganizer2/songs.txt","wb");
if ($SongStore === false)
echo "There was an error updating the song file\n";
else
{fwrite($SongStore, $NewSongs);
fclose($SongStore);
}
} else
unlink("SongOrganizer2/songs.txt");
}
}
if(isset($_POST['submit']))
$SongToAdd = stripslashes($_POST['SongName']) . "\n";
$ExistingSongs = array();
if (file_exists("SongOrganizer2/songs.txt")
&filesize("SongOrganizer2/songs.txt")> 0)
{ $ExistingSongs = file("SongOrganizer2/songs.txt");}
if (in_array($SongToAdd, $ExistingSongs))
{echo "<p>The song you entered already exists!<br />\n";
echo "Your song was not added to the list.</p>";
}
else
{
$SongFile = fopen("SongOrganizer2/songs.txt","ab");
if ($SongFile === false)
echo "There was an error saving your message!\n";
else
{
fwrite($SongFile, $SongToAdd);
fclose($SongFile);
echo "Your song has been added to the list.\n";
}
}
if ((!file_exists("SongOrganizer2/songs.txt"))
|| (filesize("SongOrganizer2/songs.txt")== 0))
echo "<p>There are no songs in the list.</p>\n";
else
{
$SongArray = file("SongOrganizer2/songs.txt");
echo "<table border=\"1\" width=\"100%\" style=\"background-color:lightgray\">\n";
foreach ($SongArray as $Song) {
echo "<tr>\n";
echo "<td>" . htmlentities($Song) ."</td>";
echo "</tr>\n";}
echo "</table>\n";
}
?>
<p>
<a href="SongOrganizer2.php?action=Sort%20Ascending">
Sort Song List </a><br />
<a href="SongOrganizer2.php?action=Remove%20Duplicates">
Remove Duplicate Songs </a><br />
<a href= "SongOrganizer2.php?action=Shuffle">
Randomize Song List </a><br />
</p>
<form action="SongOrganizer2.php" method="post">
<p>Add a New Song</p>
<p>Song Name: <input type="text" name="SongName"/></p>
<p><input type="submit" name = 'submit'
value="Add Song to List" />
<input type="reset" name="reset" value="Reset Song Name" /></p>
</form>
</body>
</html>.
Help implement BST- The code must follow the instruction below as well.pdfa2zmobiles
Help implement BST. The code must follow the instruction below as well as produce the
correct output when testing with the input file I'll provide at the end. Please fill-in the
TODO parts in the bst.cc code:
INSTRUCTIONS:
3. First, implement the BST (declared in bst.h) and use the unittest_bst() function to test it. DO
NOT CHANGE THIS CODE FOR YOUR SUBMISSION, as it will be used to test your code
against the answers (with different seed values).
4. Implement the DB and use the unittest_db() function to test it. DO NOT CHANGE THIS
CODE FOR YOUR SUBMISSION, as it will be used to test your code against the answers (with
different input files).
a) The student ID should be the base class's key member.
b) The student ID should be automatically assigned such that the ID/key should be n if the
student is the nth student to join the school. If the student later leaves (i.e., deleted from the
BST), the ID does NOT get reassigned to another student. Thus, the student ID of the last student
to join the school should reflect the TOTAL number of students that have joined this school
since its reception (regardless of whether some have left).
5. Test your code against the provided input and output files.
a) The provided answer for the BST unit test is in "unittest_ans_t100_s100.txt". The s100 refers
to the seed of 100 (-s 100), and t100 refers to the number of elements to add to the BST (-t 100).
b) The provided answer for the DB is in "students_1_ans.txt" for the "students_1.txt" input file.
6. Make sure your code has no memory leaks (using valgrind).
7. Your code should work beyond the provided unit tests. That is, even if it does work for all the
given tests, if the code has an identifiable bug (i.e., by reading the source code), points WILL be
deducted.
For example, if I were to change
unittest_bst(num_test, seed, cout, 5); ->
unittest_bst(num_test, seed, cout, 100);
it should still work.
BST.H
#ifndef BST_H_
#define BST_H_
#include <iostream>
using namespace std;
class Node {
private:
int key;
Node* parent;
Node* left;
Node* right;
public:
// Default constructor
Node();
// Constructor
Node(int in);
// Destructor
// a virtual constructor is required for inheritance
virtual ~Node();
// Add to parent of current node
void add_parent(Node* in);
// Add to left of current node
void add_left(Node* in);
// Add to right of current node
void add_right(Node* in);
// Get key
int get_key();
// Get parent node
Node* get_parent();
// Get left node
Node* get_left();
// Get right node
Node* get_right();
virtual void print_info(ostream& to);
};
class BST {
private:
Node* root;
// Walk the subtree from the given node
void inorder_walk(Node* in, ostream& to);
// Get the minimum node from the subtree of given node
Node* get_min(Node* in);
// Get the maximum node from the subtree of given node
Node* get_max(Node* in);
public:
// Constructor and Destructor
BST();
~BST();
// Modify tree
void insert_node(Node* in);
void delete_node(Node* out);
// Find nodes in the tree
Node* t.
A class that automates conversion from a C++ recursive function to an iterative function. It allow the recursive function to preserve its structure by reproducing the "call stack" on an std::stack. The examples use combinatorics to illustrate usage.
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdfajay1317
/* # INPUT: a node z in the AVL Tree # OUTPUT: the new root node r of the subtree originally
rooted at z before the restructure/rebalancing at z # PRECONDITION: z is the only unbalanced
node in the subtree that it roots; the difference in height of its children is exactly 2 #
POSTCONDITION: the subtree originally rooted at z is a proper AVL subtree with node heights
properly set AVLTreeMap: : Node* AVLTreeMap::rebalance(AVLTreeMap::Node* z) {
AVLTreeMap::Node* r=z; // Your code here return r;.
1. /*********************************************************************************************
*
* ringBufferSample.c
*
* This file contains code for a simple ring buffer. Ring buffers were commonly used in
* microcontroller architectures with limited memory, and are mostly deprecated in newer
* systems. Most commonly, these were used for catching serial character inputs and
* holding the data until a full data packet could arrive.
*
* Ring buffers are used primarily as statically allocated queues. The reason it uses
* statically allocated memory over dynamic memory is due to its primary use being in
* microcontrollers with very small amounts of memory, typically on the order of 64kB.
*
* The struct definition, RING_BUFFER_ERROR definition, and function declarations should
* be contained in a header file.
*
* This was written as sample code for my LinkedIn profile.
*
* This sample code is Copyright by Matt Kutschera, 2017
*
* It may viewed by anyone, and may be distributed by LinkedIn only on my personal
* LinkedIn profile. Any other method of distribution is available only by approval on
* a case by case basis.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**********************************************************************************************/
/*
* START OF HEADER FILE -----------------------------------------------------------------------
*/
#define RING_BUFFER_ERROR 0x0100 //number larger than a character
/*
* The ring buffer contains an array to hold the data, and two array indeces
*/
struct ringBuffer
{
char buffer[SIZE];
size_t input;
size_t output;
};
/**
* initBuffer()
*
* Initializes a ring buffer
*
* @param *ring Pointer to an allocated ring buffer
* @return 0 upon successful completion, RING_BUFFER_ERROR if buffer is not allocated
*/
int initBuffer(struct ringBuffer *ring);
/**
*
* ringBufferEnQueue()
*
* Adds a value to the end of the ring buffer
*
* @param *ring Pointer to ring buffer to add value to
* @param value Value to add
* @return Value added, or RING_BUFFER_ERROR if either the buffer is not allocated or
* buffer is full
2. */
int ringBufferEnQueue(struct ringBuffer *ring, char value);
/**
*
* ringBufferDeQueue()
*
* Reads a value from the front of the ring buffer, and removes it from the buffer
*
* @param *ring Pointer to ring buffer to get value from
* @return Value from ring buffer, or RING_BUFFER_ERROR if either the buffer is not
* allocated or buffer is empty
*/
int ringBufferDeQueue(struct ringBuffer *ring);
/**
*
* ringBufferSize()
*
* Gets the number of bytes in the ring buffer
*
* @param *ring Pointer to ring buffer to count elements of
* @return Number of elements in the buffer, or -1 if the buffer is not
* allocated
*/
int ringBufferSize(struct ringBuffer *ring);
/*
* END OF HEADER FILE / START OF C FILE -----------------------------------------------------
*/
#define SIZE 128 //arbitrary value for sample. in actual use, it should be
//large enough to hold at least two packets of data, but
//not so large that it restricts the available memory for
//other aspects of the program.
/*
* As this code is meant for small memory programs, it is possible that libraries containing
* the standard NULL macro are not included.
*/
#ifndef NULL
#define NULL (void *)0
#endif
/**
* initBuffer()
*
* Initializes a ring buffer
*
* @param *ring Pointer to an allocated ring buffer
* @return 0 upon successful completion, RING_BUFFER_ERROR if buffer is not allocated
*/
int initBuffer(struct ringBuffer *ring)
{
int retVal = RING_BUFFER_ERROR;
if (ring != NULL)
{
ring->input = 0;
ring->output = 0;
retVal = 0;
}
return retVal;
}
/**
*
* ringBufferEnQueue()
*
* Adds a value to the end of the ring buffer
*
3. * @param *ring Pointer to ring buffer to add value to
* @param value Value to add
* @return Value added, or RING_BUFFER_ERROR if either the buffer is not allocated or
* buffer is full
*/
int ringBufferEnQueue(struct ringBuffer *ring, char value)
{
int retVal = RING_BUFFER_ERROR;
if((ring != NULL)
&& (((ring->input +1) % SIZE) != ring->output)) //check to see if buffer is full
{
retVal = value;
ring->buffer[input];
input = (input++) % SIZE; //wraps around to first element
}
return retVal;
}
/**
*
* ringBufferDeQueue()
*
* Reads a value from the front of the ring buffer, and removes it from the buffer
*
* @param *ring Pointer to ring buffer to get value from
* @return Value from ring buffer, or RING_BUFFER_ERROR if either the buffer is not
* allocated or buffer is empty
*/
int ringBufferDeQueue(struct ringBuffer *ring)
{
int retVal = RING_BUFFER_ERROR;
if((ring != NULL)
&& (ring->input != ring->output)) //check to see if buffer is empty
{
retVal = ring->buffer[output];
output = (output++) % SIZE; //wraps around to first element
}
return retVal;
}
/**
*
* ringBufferSize()
*
* Gets the number of bytes in the ring buffer
*
* @param *ring Pointer to ring buffer to count elements of
* @return Number of elements in the buffer, or -1 if the buffer is not
* allocated
*/
int ringBufferSize(struct ringBuffer *ring)
{
int retVal = -1;
if(ring != NULL)
{
retVal = ring->input - ring->output;
if(retVal < 0)
retVal += SIZE;
}
return retVal;
}