Skip to content

Commit 77c2b8f

Browse files
committed
init commit
0 parents  commit 77c2b8f

14 files changed

+858
-0
lines changed

CMakeLists.txt

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(Table)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
set(SOURCE_DIR src)
7+
8+
set(SOURCES
9+
${SOURCE_DIR}/main.cpp
10+
${SOURCE_DIR}/TSortedTable.cpp
11+
${SOURCE_DIR}/TUnsortedTable.cpp
12+
${SOURCE_DIR}/TBalanceTree.cpp
13+
${SOURCE_DIR}/THashTable.cpp
14+
${SOURCE_DIR}/TTable.cpp
15+
16+
)
17+
18+
set(HEADERS
19+
headers/TSortedTable.h
20+
headers/TUnsortedTable.h
21+
headers/TBalanceTree.h
22+
headers/THashTable.h
23+
headers/TTable.h
24+
submodules/TList/TList.h
25+
)
26+
27+
include_directories(
28+
${SOURCE_DIR}/../headers
29+
${SOURCE_DIR}/../submodules/TList
30+
)
31+
32+
add_executable(Table ${SOURCES} ${HEADERS})
33+
34+
set(TABLE_EXAMPLES ${SOURCE_DIR}/../tableExamples/Table.txt)
35+
36+
configure_file(${TABLE_EXAMPLES} ${CMAKE_CURRENT_BINARY_DIR}/tableExamples/Table.txt COPYONLY)

headers/TBalanceTree.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
#include<iostream>
3+
#include<string>
4+
5+
class TBalanceTree {
6+
struct obj {
7+
std::string key;
8+
int data;
9+
obj(std::string _key = "", int _data = 0) : key(_key), data(_data) {};
10+
};
11+
class TNode {
12+
public:
13+
obj data;
14+
TNode* pLeft;
15+
TNode* pRight;
16+
short balanceRatio;
17+
TNode(std::string _key, int _data, short _balanceRatio = 0) : pLeft(nullptr), pRight(nullptr), balanceRatio(_balanceRatio) { data.key = _key; data.data = _data; };
18+
};
19+
TNode* pRoot;
20+
int count_op;
21+
public:
22+
TBalanceTree() : pRoot(nullptr), count_op(0) {};
23+
~TBalanceTree() {};
24+
25+
bool IsEmpty();
26+
bool IsFull();
27+
28+
TNode* Search(std::string, bool&);
29+
void Push(std::string, int);
30+
void Del(std::string);
31+
32+
int GetCountOp() {
33+
return count_op;
34+
}
35+
void Print();
36+
private:
37+
void PrintLTR(const TNode*);
38+
TNode* GetPrevNode(const TNode*);
39+
};

headers/THashTable.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
#include"../submodules/TList/TList.h"
3+
#include<string>
4+
5+
class THashTable {
6+
class pair {
7+
std::string key;
8+
int data;
9+
public:
10+
pair(std::string _key = "", int _data = 0) :key(_key), data(_data) {};
11+
pair(const pair& _pair) {
12+
key = _pair.key;
13+
data = _pair.data;
14+
}
15+
pair& operator =(const pair& _pair) {
16+
key = _pair.key;
17+
data = _pair.data;
18+
return *this;
19+
}
20+
bool operator ==(const pair& _pair) {
21+
return _pair.key == key;
22+
}
23+
pair& operator +(const pair& _pair) {
24+
data += _pair.data;
25+
return *this;
26+
}
27+
friend std::ostream& operator <<(std::ostream& os, const pair& _pair);
28+
};
29+
TList<pair>* listsArray;
30+
size_t opCount, size;
31+
public:
32+
THashTable(size_t Max = 100);
33+
~THashTable();
34+
35+
void Print();
36+
size_t GetCountOp();
37+
38+
size_t Search(std::string _key, bool& isFound);
39+
void Push(std::string _key, int _data);
40+
void Del(std::string _key);
41+
private:
42+
bool IsEmpty();
43+
bool IsFull();
44+
45+
size_t hash(std::string _key);
46+
friend std::ostream& operator <<(std::ostream& os, const pair& _pair);
47+
};

headers/TSortedTable.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include"./TTable.h"
3+
#include<cstring>
4+
5+
class TSortedTable : public TTable {
6+
public:
7+
TSortedTable(const int _Max) : TTable(_Max) {};
8+
9+
virtual int Search(std::string, bool* flag)override;
10+
virtual void Push(std::string, int)override;
11+
virtual void Del(std::string)override;
12+
};

headers/TTable.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
#include<iostream>
3+
#include<cstring>
4+
5+
class TTable {
6+
public:
7+
struct obj{
8+
std::string key;
9+
int data;
10+
obj& operator =(const obj& _obj) {
11+
key = _obj.key;
12+
data = _obj.data;
13+
return *this;
14+
}
15+
};
16+
obj* arr;
17+
const int MAX;
18+
int cur_size, count_op;
19+
public:
20+
TTable(const int _MAX) : MAX(_MAX), cur_size(0), count_op(0) {
21+
arr = new obj[MAX];
22+
}
23+
~TTable() {
24+
delete[] arr;
25+
}
26+
27+
bool IsEmpty();
28+
bool IsFull();
29+
int GetCountOp();
30+
31+
friend std::ostream& operator <<(std::ostream&, const TTable&);
32+
protected:
33+
virtual int Search(std::string, bool*) = 0;
34+
virtual void Push(std::string, int) = 0;
35+
virtual void Del(std::string) = 0;
36+
};

headers/TUnsortedTable.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include"./TTable.h"
3+
#include<string>
4+
5+
class TUnsortedTable : public TTable {
6+
public:
7+
TUnsortedTable(const int _Max) : TTable(_Max) {};
8+
9+
virtual int Search(std::string, bool* = NULL)override;
10+
virtual void Push(std::string, int)override;
11+
virtual void Del(std::string)override;
12+
};

src/TBalanceTree.cpp

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include"../headers/TBalanceTree.h"
2+
3+
bool TBalanceTree::IsFull()
4+
{
5+
try {
6+
TNode* tmp = new TNode("", 0);
7+
delete tmp;
8+
}
9+
catch (...) {
10+
return true;
11+
}
12+
return false;
13+
}
14+
15+
bool TBalanceTree::IsEmpty()
16+
{
17+
return pRoot == nullptr;
18+
}
19+
20+
TBalanceTree::TNode* TBalanceTree:: Search(std::string _key, bool& isFound) {
21+
count_op = 1;
22+
if (IsEmpty()) return nullptr;
23+
if (pRoot->data.key == _key) { isFound = true; return pRoot; }
24+
else if ((pRoot->pLeft == nullptr) && (pRoot->data.key > _key) || (pRoot->pRight == nullptr) && (pRoot->data.key < _key)) { isFound = false; return pRoot; }
25+
TNode* tmp = pRoot;
26+
while (true) {
27+
if (tmp->data.key == _key) { isFound = true; return tmp; }
28+
else if ((tmp->pLeft == nullptr) && (tmp->data.key > _key) || (tmp->pRight == nullptr) && (tmp->data.key < _key)) { isFound = false; return tmp; }
29+
else if (tmp->data.key > _key) tmp = tmp->pLeft;
30+
else if (tmp->data.key < _key) tmp = tmp->pRight;
31+
count_op++;
32+
}
33+
}
34+
35+
void TBalanceTree::Push(std::string _key, int _data) {
36+
bool isFound;
37+
TNode* tmp = Search(_key, isFound);
38+
TNode* node = new TNode(_key, _data);
39+
if (tmp == nullptr) pRoot = node;
40+
else if (isFound) {
41+
tmp->data.data += _data;
42+
}
43+
else {
44+
if (tmp->data.key > _key) tmp->pLeft = node;
45+
else if (tmp->data.key < _key) tmp->pRight = node;
46+
}
47+
}
48+
49+
void TBalanceTree::Del(std::string _key) {
50+
bool isFound;
51+
TNode* tmp = Search(_key, isFound);
52+
if (!isFound) return;
53+
else {
54+
if ((tmp->pLeft == nullptr) && (tmp->pRight == nullptr)) {
55+
TNode* tmpPrev = GetPrevNode(tmp);
56+
if (tmpPrev == nullptr) pRoot = nullptr;
57+
else if (tmpPrev->data.key > tmp->data.key) tmpPrev->pLeft = nullptr;
58+
else if (tmpPrev->data.key < tmp->data.key) tmpPrev->pRight = nullptr;
59+
delete tmp;
60+
}
61+
else if (tmp->pLeft == nullptr) {
62+
TNode* tmpPrev = GetPrevNode(tmp);
63+
if (tmpPrev == nullptr) pRoot = tmp->pRight;
64+
else if (tmpPrev->data.key > tmp->data.key) tmpPrev->pLeft = tmp->pRight;
65+
else if (tmpPrev->data.key < tmp->data.key) tmpPrev->pRight = tmp->pRight;
66+
delete tmp;
67+
}
68+
else if (tmp->pRight == nullptr) {
69+
TNode* tmpPrev = GetPrevNode(tmp);
70+
if (tmpPrev == nullptr) pRoot = tmp->pLeft;
71+
else if (tmpPrev->data.key > tmp->data.key) tmpPrev->pLeft = tmp->pLeft;
72+
else if (tmpPrev->data.key < tmp->data.key) tmpPrev->pRight = tmp->pLeft;
73+
delete tmp;
74+
}
75+
else {
76+
TNode* tmpSheet = tmp;
77+
tmpSheet = tmpSheet->pLeft;
78+
while (tmpSheet->pRight != nullptr) tmpSheet = tmpSheet->pRight;
79+
TNode* tmpPrevSheet = GetPrevNode(tmpSheet);
80+
if (tmpPrevSheet == tmp) {
81+
TNode* tmpPrev = GetPrevNode(tmp);
82+
if (tmpPrev == nullptr) pRoot = tmpSheet;
83+
else if (tmpPrev->data.key > tmp->data.key) tmpPrev->pLeft = tmpSheet;
84+
else if (tmpPrev->data.key < tmp->data.key) tmpPrev->pRight = tmpSheet;
85+
tmpSheet->pRight = tmp->pRight;
86+
delete tmp;
87+
return;
88+
}
89+
tmpPrevSheet->pRight = tmpSheet->pLeft;
90+
TNode* tmpPrev = GetPrevNode(tmp);
91+
if (tmpPrev == nullptr) pRoot = tmpSheet;
92+
else if (tmpPrev->data.key > tmp->data.key) tmpPrev->pLeft = tmpSheet;
93+
else if (tmpPrev->data.key < tmp->data.key) tmpPrev->pRight = tmpSheet;
94+
if (tmp->pLeft != tmpSheet) tmpSheet->pLeft = tmp->pLeft;
95+
tmpSheet->pRight = tmp->pRight;
96+
delete tmp;
97+
}
98+
}
99+
}
100+
101+
void TBalanceTree::PrintLTR(const TBalanceTree::TNode* _node) {
102+
if (_node == nullptr) return;
103+
PrintLTR(_node->pLeft);
104+
std::cout << _node->data.key << " (" << _node->data.data << ")" << std::endl;
105+
PrintLTR(_node->pRight);
106+
}
107+
108+
void TBalanceTree::Print() {
109+
PrintLTR(pRoot);
110+
}
111+
112+
TBalanceTree::TNode* TBalanceTree::GetPrevNode(const TBalanceTree::TNode* _node) {
113+
if (_node == pRoot) return nullptr;
114+
TNode* res = pRoot;
115+
while (true) {
116+
if ((res->pLeft == nullptr) && (res->data.key > _node->data.key) || (res->pRight == nullptr) && (res->data.key < _node->data.key)) return nullptr;
117+
if ((res->pLeft == _node) || (res->pRight == _node)) return res;
118+
else if (res->data.key > _node->data.key) res = res->pLeft;
119+
else if (res->data.key < _node->data.key) res = res->pRight;
120+
}
121+
}

0 commit comments

Comments
 (0)