Initial commit of code for dopelib, and ch 1, 2, and 12

This commit is contained in:
2024-10-29 16:04:54 -04:00
parent 11601352d3
commit afbf4c2f7e
25 changed files with 745 additions and 0 deletions

172
libs/dope/bintree.hpp Normal file
View File

@@ -0,0 +1,172 @@
#pragma once
#include <queue>
template <class T>
class DopeBinTreeNode{
DopeBinTreeNode<T>* left;
DopeBinTreeNode<T>* right;
T data;
public:
DopeBinTreeNode(T data);
~DopeBinTreeNode();
void SetLeft(DopeBinTreeNode<T>* newleft){left = newleft;}
void SetRight(DopeBinTreeNode<T>* newright){right = newright;}
DopeBinTreeNode<T>* GetLeft(){return left;}
DopeBinTreeNode<T>* GetRight(){return right;}
T GetData(){return data;}
};
template <class T>
class DopeBinTree{
DopeBinTreeNode<T>* root;
T* RecPreOrderTraversal(DopeBinTreeNode<T>* node, T* out);
T* RecInOrderTraversal(DopeBinTreeNode<T>* node, T* out);
T* RecPostOrderTraversal(DopeBinTreeNode<T>* node, T* out);
public:
DopeBinTree(T data);
DopeBinTree();
~DopeBinTree();
void insert(T data);
bool isEmpty();
DopeBinTreeNode<T>* getRoot(){return root;}
void RecPreOrderTraversalArray(T* array);
void RecInOrderTraversalArray(T* array);
void RecPostOrderTraversalArray(T* array);
};
// Implementations ___________________________________________________
template <class T>
DopeBinTreeNode<T>::DopeBinTreeNode(T node_data):
left(nullptr),
right(nullptr),
data(node_data)
{
}
template <class T>
DopeBinTreeNode<T>::~DopeBinTreeNode(){
delete left;
delete right;
}
template <class T>
DopeBinTree<T>::DopeBinTree(T data){
root = new DopeBinTreeNode<T>(data);
}
template <class T>
DopeBinTree<T>::DopeBinTree():
root(nullptr)
{
}
template <class T>
DopeBinTree<T>::~DopeBinTree(){
if (root != nullptr){
delete root;
}
}
template <class T>
// Insert Breadth first
void DopeBinTree<T>::insert(T data){
DopeBinTreeNode<T>* current_node = root;
if (root == nullptr){
root = new DopeBinTreeNode<T>(data);
} else {
auto new_node = new DopeBinTreeNode<T>(data);
// Breadth first traversal
std::queue<DopeBinTreeNode<T>*> q;
q.push(root);
while(!q.empty()){
current_node = q.front();
q.pop();
if(current_node->GetLeft()){
q.push(current_node->GetLeft());
} else {
current_node->SetLeft(new_node);
break;
}
if(current_node->GetRight()){
q.push(current_node->GetRight());
} else {
current_node->SetRight(new_node);
break;
}
}
}
}
template <class T>
bool DopeBinTree<T>::isEmpty(){
if(root == nullptr){
return true;
} else {
return false;
}
}
template <class T>
T* DopeBinTree<T>::RecPreOrderTraversal(DopeBinTreeNode<T>* node, T* out){
if (node == nullptr){
return out--;
}
*out = node->GetData();
out++;
out = RecPreOrderTraversal(node->GetLeft(), out);
out = RecPreOrderTraversal(node->GetRight(), out);
return out;
}
template <class T>
T* DopeBinTree<T>::RecInOrderTraversal(DopeBinTreeNode<T>* node, T* out){
if (node == nullptr){
return out--;
}
out = RecInOrderTraversal(node->GetLeft(), out);
*out = node->GetData();
out++;
out = RecInOrderTraversal(node->GetRight(), out);
return out;
}
template <class T>
T* DopeBinTree<T>::RecPostOrderTraversal(DopeBinTreeNode<T>* node, T* out){
if (node == nullptr){
return out--;
}
out = RecPostOrderTraversal(node->GetLeft(), out);
out = RecPostOrderTraversal(node->GetRight(), out);
*out = node->GetData();
out++;
return out;
}
template <class T>
void DopeBinTree<T>::RecPreOrderTraversalArray(T* array){
int index = 0;
DopeBinTreeNode<T>* node = root;
RecPreOrderTraversal(node, &array[index]);
}
template <class T>
void DopeBinTree<T>::RecInOrderTraversalArray(T* array){
int index = 0;
DopeBinTreeNode<T>* node = root;
RecInOrderTraversal(node, &array[index]);
}
template <class T>
void DopeBinTree<T>::RecPostOrderTraversalArray(T* array){
int index = 0;
DopeBinTreeNode<T>* node = root;
RecPostOrderTraversal(node, &array[index]);
}

56
libs/dope/linkedlist.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include "linkedlist.hpp"
DopeLinkedList::DopeLinkedList(){
for(int i = 0; i < MAX_DOPE_NODES; ++i){
nodes[i].Reset();
}
head = &nodes[0];
}
DopeNode::DopeNode(int init_data){
data = init_data;
next = NULL;
state = NodeState::UnInitialized;
}
DopeNode::DopeNode(){
Reset();
}
void DopeNode::Reset(){
this->data = 0;
this->next = NULL;
this->state = NodeState::UnInitialized;
}
void DopeLinkedList::AppendData(int init_data){
DopeNode* new_node = head;
DopeNode* node = head;
// Find next free slot and allocate it for the new node
for(int i=0; i < MAX_DOPE_NODES; ++i){
if(nodes[i].GetState() == NodeState::UnInitialized){
new_node = &nodes[i];
new_node->SetNext(NULL);
new_node->SetState(NodeState::Initialized);
new_node->SetData(init_data);
break;
}
}
if(head == new_node){
// Just set first node, nothing else to do
return;
}
// Find last node in linked list
if ((head->GetNext() == NULL) && (head->GetState() == NodeState::Initialized)){
// Special case, only one node so far
head->SetNext(new_node);
} else {
while(node->GetNext() != NULL){
node = node->GetNext();
}
node->SetNext(new_node);
}
}

37
libs/dope/linkedlist.hpp Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#include<stdlib.h>
#define MAX_DOPE_NODES 255
typedef enum NodeState_E{
UnInitialized,
Initialized
} NodeState;
class DopeNode {
int data;
DopeNode* next;
NodeState state;
public:
DopeNode(int data);
DopeNode();
DopeNode* GetNext(){return next;}
NodeState GetState(){return state;}
int GetData(){return data;}
void SetNext(DopeNode* node){next = node;}
void SetState(NodeState new_state){state = new_state;}
void SetData(int new_data){data = new_data;}
void Reset();
};
class DopeLinkedList {
DopeNode nodes[MAX_DOPE_NODES];
DopeNode* head;
public:
DopeLinkedList();
void AppendData(int init_data);
DopeNode* GetHead(){return head;}
void SetHead(DopeNode* new_head){head = new_head;}
};

3
libs/dope/meson.build Normal file
View File

@@ -0,0 +1,3 @@
dope_srcs = ['linkedlist.cpp']
dope_lib = static_library('dope', dope_srcs)

1
libs/dope/queues.cpp Normal file
View File

@@ -0,0 +1 @@
#include <dope/queues.hpp>

1
libs/dope/queues.hpp Normal file
View File

@@ -0,0 +1 @@
#pragma once

59
libs/dope/stacks.hpp Normal file
View File

@@ -0,0 +1,59 @@
#pragma once
#include <stdlib.h>
template <class T, size_t S>
class DopeStack{
static const size_t size = S;
T stack[S];
T* head;
size_t occupancy;
public:
DopeStack();
T pop();
void push(T item);
T peek();
size_t getOccupancy(){return occupancy;}
bool isEmpty();
};
// Implementation ____________________________________________________
template <class T, size_t S>
DopeStack<T, S>::DopeStack():
head (&stack[0]),
occupancy(0)
{
}
template <class T, size_t S>
T DopeStack<T, S>::pop(){
T ret = *head;
if(head != &stack[0]){
head--;
}
occupancy--;
return ret;
}
template <class T, size_t S>
void DopeStack<T, S>::push(T item){
if(occupancy != 0){
head++;
}
*head = item;
occupancy++;
}
template <class T, size_t S>
T DopeStack<T, S>::peek(){
return *head;
}
template <class T, size_t S>
bool DopeStack<T, S>::isEmpty(){
return (occupancy == 0);
}