Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified labs/lab05/code.zip
Binary file not shown.
34 changes: 34 additions & 0 deletions labs/lab05/code/postlab/AVLNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "AVLNode.h"
#include <string>

using namespace std;

AVLNode::AVLNode()
{
value = "";
left = NULL;
right = NULL;
height = 0;
}

AVLNode::~AVLNode()
{
delete left;
delete right;

// to avoid crash on double deletes
left = NULL;
right = NULL;
}

AVLNode& AVLNode::operator=(const AVLNode& other)
{
if (this != &other)
{
this->value = other.value;
this->left = other.left;
this->right = other.right;
this->height = other.height;
}
return *this;
}
19 changes: 19 additions & 0 deletions labs/lab05/code/postlab/AVLNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef AVL_NODE
#define AVL_NODE

#include <string>

class AVLNode {
AVLNode();
~AVLNode();
AVLNode& operator=(const AVLNode& other);

std::string value;
AVLNode* left;
AVLNode* right;
int height;

friend class AVLTree;
};

#endif //AVL_NODE
160 changes: 107 additions & 53 deletions labs/lab05/code/postlab/AVLTree.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,94 @@
// Feel free to edit this file and add functions as necessary
#include "AVLTree.h"
#include "AVLNode.h"
#include <string>
#include <iostream>
#include <iomanip> //for setw in postorder

using namespace std;

// Implement the following
AVLNode::AVLNode() {}
AVLTree::AVLTree() {}
AVLTree::~AVLTree() {}
void AVLTree::insert(const string& x) {}
string pathTo(const string& x) const {}
bool find(const string& x) const {}
int numNodes() const {}
void balance(AVLNode*& n) {}
AVLNode* rotateLeft(AVLNode*& n) {}
AVLNode* rotateRight(AVLNode*& n) {}
// Implement at least the remaining unimplemented methods in AVLTree.h (see below method skeletons)
AVLTree::AVLTree() { }

// The following are implemented for you
// remove finds x's position in the tree and removes it, rebalancing as
// necessary.
void AVLTree::remove(const string& x) { root = remove(root, x); }
AVLTree::~AVLTree() { }

// private helper for remove to allow recursion over different nodes. returns
// an AVLNode* that is assigned to the original node.
AVLNode* AVLTree::remove(AVLNode*& n, const string& x) {
if (n == NULL) {
return NULL;
void AVLTree::insert(const string& x) { }
string AVLTree::pathTo(const string& x) const { return ""; }
bool AVLTree::find(const string& x) const { return false; }
int AVLTree::numNodes() const { return -1; }
// balance should balance only the single node it is given
void AVLTree::balance(AVLNode*& n) { }
AVLNode* AVLTree::rotateLeft(AVLNode*& n) { return NULL; }
AVLNode* AVLTree::rotateRight(AVLNode*& n) { return NULL; }

/** The following are implemented for you: remove, max, min, and height;
remove finds x's position in the tree and removes it, rebalancing as necessary. **/
void AVLTree::remove(const string& x) { remove(root, x); }

/** private helper for remove to allow recursion over different nodes;
returns an AVLNode* that is assigned to the original node. **/
bool AVLTree::remove(AVLNode*& current, const string& x) {
bool returnVal = false;
// base-case for x not present in tree
if (current == NULL)
{
returnVal = false;
}
// recursively approach base case to left
else if (x < current->value)
{
returnVal = remove(current->left, x);
}
// recursively approach base case to right
else if (x > current->value)
{
returnVal = remove(current->right, x);
}
// first look for x
if (x == n->value) {
// found
// base-case for x present in tree
else
{
// no children
if (n->left == NULL && n->right == NULL) {
delete n;
return NULL;
if(current->left == NULL && current->right == NULL)
{
delete current;
current = NULL;
returnVal = (current == NULL); // should be true
}
// single child
if (n->left == NULL) {
return n->right;
// two children
else if (current-> left != NULL && current->right != NULL)
{
AVLNode *rLMost = current->right;
// find leftmost node in right subtree iteratively
// this value is the lowest of the values higher than our current value
// i.e. the middle-most value of this (sub)tree
while(rLMost->left != NULL)
{
rLMost = rLMost->left;
}
// replace current with rLMost (value only)
current->value = rLMost->value;
// this should immediately reach either base-case present
// with (above) no children or (below) one child, the right
// just log more recurse; runtime preserved.
returnVal = remove(current->right, rLMost->value);
}
if (n->right == NULL) {
return n->left;
// one child
else
{
// if right is not null, then child is right; otherwise child is left
AVLNode* child = (current->right != NULL) ? current->right : current->left;
// replace current with child but keep height
int heightTemp = current->height;
current = child;
current->height = heightTemp;
delete child;
child = NULL;
returnVal = (child == NULL); //should be true
}
// two children -- tree may become unbalanced after deleting n
string sr = min(n->right);
n->value = sr;
n->right = remove(n->right, sr);
} else if (x < n->value) {
n->left = remove(n->left, x);
} else {
n->right = remove(n->right, x);
}
n->height = 1 + max(height(n->left), height(n->right));
balance(n);
return n;
}

// max returns the greater of two integers.
int max(int a, int b) {
if (a > b) {
return a;
}
return b;
balance(current);
return returnVal;
// recall that if this returns true, balance(root) will be called, fixing any inbalance
}

// min finds the string with the smallest value in a subtree.
Expand All @@ -72,11 +100,37 @@ string AVLTree::min(AVLNode* node) const {
return min(node->left);
}

// height returns the value of the height field in a node. If the node is
// null, it returns -1.
/** height returns the value of the height field in a node.
If the node is null, it returns 0. **/
int AVLTree::height(AVLNode* node) const {
if (node == NULL) {
return -1;
return 0;
}
return node->height;
}

void AVLTree::print()
{
postorder(root, 0);
}

void AVLTree::postorder(AVLNode*& current, int indent)
{
if(current != NULL) {
if(current->left != NULL) postorder(current->left, indent+4);
if(current->right != NULL) postorder(current->right, indent+4);
if (indent) {
std::cout << std::setw(indent) << ' ';
}
std::cout << current->value << std::endl;
}
}

// non-member(s)
// max returns the greater of two integers.
int max(int a, int b) {
if (a > b) {
return a;
}
return b;
}
31 changes: 15 additions & 16 deletions labs/lab05/code/postlab/AVLTree.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
// Feel free to edit this file and add functions as necessary

#ifndef AVL_H
#define AVL_H

#include "AVLNode.h"
#include <string>

using namespace std;

class AVLNode {
AVLNode();

string value;
AVLNode* left;
AVLNode* right;
int height;

friend class AVLTree;
};

class AVLTree {
public:
AVLTree();
Expand All @@ -36,30 +28,37 @@ class AVLTree {
// numNodes returns the total number of nodes in the tree.
int numNodes() const;

void print();

private:
// Declare a root node
AVLNode* root;

// balance makes sure that the subtree with root n maintains the AVL tree
// property, namely that the balance factor of n is either -1, 0, or 1.
// balance should balance only the single node it is given
// that the balance factor of n is either -1, 0, or 1.
void balance(AVLNode*& n);
// rotateLeft performs a single rotation on node n with its left child.
AVLNode* rotateLeft(AVLNode*& n);
// rotateRight performs a single rotation on node n with its right child.
AVLNode* rotateRight(AVLNode*& n);

// private helper for remove to allow recursion over different nodes. returns
// an AVLNode* that is assigned to the original node.
AVLNode* remove(AVLNode*& n, const string& x);
// an true if the node is removed; false otherwise
bool remove(AVLNode*& n, const string& x);
// min finds the string with the smallest value in a subtree.
string min(AVLNode* node) const;
// height returns the value of the height field in a node. If the node is
// null, it returns -1.
// null, it returns 0.
int height(AVLNode* node) const;

// prints a subtree
// call on root for postorder of full tree
void postorder(AVLNode* &current, int indent);

// Any other methods you need...
};

// non-member(s)
// max returns the greater of two integers.
int max(int a, int b);

Expand Down
32 changes: 32 additions & 0 deletions labs/lab05/code/postlab/BinaryNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "BinaryNode.h"
#include <string>

using namespace std;

BinaryNode::BinaryNode()
{
value = "";
left = NULL;
right = NULL;
}

BinaryNode::~BinaryNode()
{
delete left;
delete right;

// to avoid crash on double deletes
left = NULL;
right = NULL;
}

BinaryNode& BinaryNode::operator=(const BinaryNode& other)
{
if (this != &other)
{
this->value = other.value;
this->left = other.left;
this->right = other.right;
}
return *this;
}
18 changes: 18 additions & 0 deletions labs/lab05/code/postlab/BinaryNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef BINARY_NODE
#define BINARY_NODE

#include <string>

class BinaryNode {
BinaryNode();
~BinaryNode();
BinaryNode& operator=(const BinaryNode& other);

std::string value;
BinaryNode* left;
BinaryNode* right;

friend class BinarySearchTree;
};

#endif //BINARY_NODE
Loading