Skip to content

Instantly share code, notes, and snippets.

@farmdawgnation
Last active January 1, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farmdawgnation/8172377 to your computer and use it in GitHub Desktop.
Save farmdawgnation/8172377 to your computer and use it in GitHub Desktop.
Hilariously primitive implementations of Binary Search Tree and Linked List I found on an old CD-ROM from High School. Written in Visual-C++. Circa 2005.
#include "StdAfx.h"
#include ".\binarysearch.h"
#include <stdlib.h>
#include <stdio.h>
BSNode::BSNode() {
this->lessThan = NULL;
this->greaterThan = NULL;
}
BinarySearch::BinarySearch() {
treeTop = NULL;
}
void BinarySearch::add(char *value) {
if(treeTop == NULL) {
treeTop = new BSNode();
treeTop->value = (char *)malloc(strlen(value));
strcpy(treeTop->value,value);
} else {
BSNode *transversal;
transversal = treeTop;
while(0==0) {
if(strcmp(transversal->value, value) > 0) {
if(transversal->lessThan == NULL) {
transversal->lessThan = new BSNode();
transversal->lessThan->value = (char *)malloc(strlen(value));
strcpy(transversal->lessThan->value, value);
break;
} else {
transversal = transversal->lessThan;
}
} else if(strcmp(transversal->value, value) < 0) {
if(transversal->greaterThan == NULL) {
transversal->greaterThan = new BSNode();
transversal->greaterThan->value = (char *)malloc(strlen(value));
strcpy(transversal->greaterThan->value, value);
break;
} else {
transversal = transversal->greaterThan;
}
} else {
break;
}
}
}
}
BSNode *BinarySearch::find(char *value) {
transversal = treeTop;
while(0==0) {
if(strcmp(transversal->value, value) > 0) {
printf("Less than %s\n", transversal->value);
if(transversal->lessThan != NULL) {
printf("Moving into %s's left node.\n", transversal->value);
transversal = transversal->lessThan;
} else {
printf("No left node for %s! Not in tree.\n", transversal->lessThan);
return NULL;
}
} else if(strcmp(transversal->value, value) < 0) {
printf("Greater than %s\n", transversal->value);
if(transversal->greaterThan != NULL) {
printf("Moving into %s's right node.\n", transversal->value);
transversal = transversal->greaterThan;
} else {
printf("No left node for %s! Not in tree.\n", transversal->lessThan);
return NULL;
}
} else if(strcmp(transversal->value, value) == 0) {
printf("Yippie yay! I found %s!\n", transversal->value);
return transversal;
}
}
}
#pragma once
class BSNode
{
public:
char *value;
BSNode *lessThan;
BSNode *greaterThan;
BSNode();
};
class BinarySearch
{
private:
BSNode *treeTop;
BSNode *transversal;
public:
BinarySearch();
void add(char *value);
BSNode *find(char *value);
};
#include "StdAfx.h"
#include <time.h>
#include ".\linkedlist.h"
#include <stdio.h>
LinkedList::LinkedList() {
count = 0;
}
void LinkedList::add(char* value) {
switch(count) {
case 0:
first = new ListItem();
first->value = (char *)malloc(strlen(value));
strcpy(first->value, value);
last = first;
count = 1;
break;
default:
last->next = new ListItem();
last->next->value = (char *)malloc(strlen(value));
strcpy(last->next->value, value);
last = last->next;
count++;
}
return;
}
void LinkedList::rem(int id) {
switch(id) {
case 0:
ListItem *secondItem;
secondItem = first->next;
first = secondItem;
break;
default:
ListItem *previousItem;
ListItem *selectedItem;
ListItem *nextItem;
for(int i = 0;i < id+1;i++) {
if(i == 0) {
selectedItem = first;
} else {
previousItem = selectedItem;
selectedItem = selectedItem->next;
}
}
nextItem = selectedItem->next;
previousItem->next = nextItem;
selectedItem->value = NULL;
selectedItem = NULL;
}
count--;
}
char* LinkedList::get(int id) {
if(id > count - 1) {
return NULL;
}
ListItem *lstitm;
if(id == 0) {
lstitm = this->first;
} else {
lstitm = first;
for(int i = 0;i < id;i++) {
lstitm = lstitm->next;
}
}
return lstitm->value;
}
int LinkedList::find(char *value) {
ListItem *lstitm;
int i;
lstitm = first;
for(i=0;i < count;i++) {
if(strcmp(lstitm->value, value) == 0) {
return i;
} else {
if(lstitm->next != NULL) {
lstitm = lstitm->next;
} else {
return -1;
}
}
}
return -1;
}
void LinkedList::set(int id, char *value) {
ListItem *lstitm;
if(id == 0) {
lstitm = this->first;
} else {
lstitm = first;
for(int i = 0;i < id;i++) {
lstitm = lstitm->next;
}
}
lstitm->value = value;
}
char *LinkedList::getRandom() {
int id;
id = rand() % count;
return get(id);
}
int LinkedList::getCount() {
return count;
}
/* randomizer for the linked list */
void LinkedList::randomize() {
LinkedList finallist;
int i = 0;
while(i < count) {
int z = rand() % count;
if(finallist.find(this->get(z)) == -1) {
finallist.add(get(z));
i++;
}
}
clear();
count = 0;
for(i=0;i < finallist.count;i++) {
add(finallist.get(i));
}
}
void LinkedList::clear() {
int storedcount;
int i=0;
storedcount = count;
for(i=storedcount-1;i > 0;i--) {
rem(i);
}
}
void LinkedList::printAll() {
for(int i = 0;i <= count-1; i ++) {
printf("%s\n", get(i));
}
}
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
class ListItem
{
public:
char* value;
ListItem *next;
};
class LinkedList
{
private:
ListItem *first;
ListItem *last;
int count;
public:
LinkedList();
void add(char* value);
void rem(int id);
char* get(int id);
int find(char* value);
void set(int id, char* value);
char* getRandom();
int getCount();
void randomize();
void clear();
void printAll();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment