This commit is contained in:
iridiumR 2021-12-03 15:26:29 +08:00
parent 7f83a91eba
commit a887c5e047
9 changed files with 721 additions and 0 deletions

228
ex10/BST.hpp Normal file
View file

@ -0,0 +1,228 @@
#include "bintree.hpp"
#include "stack.hpp"
#include <iostream>
#include <string>
#include <time.h>
#include <cmath>
class BST : binTree<int>
{
private:
binNodeArray(int) p;
public:
~BST()
{
}
BST(int num)
{
srand(time(NULL));
int temp = rand() % 1440;
_root->data = temp;
_root->pos = "root";
std::cout << _root->pos;
printf("-%02d:%02d \n", temp / 60, temp % 60);
for (int i = 0; i < num - 1; i++)
{
temp = rand() % 1440;
p = root();
std::string position;
while (temp != -1)
{
if (temp < (p->data))
{
if (hasLC(p))
{
p = p->lc;
position.append("0");
}
else
{
position.append("0");
addLC(p, temp);
p->lc->pos = position;
std::cout << position;
printf("-%02d:%02d \n", temp / 60, temp % 60);
temp = -1;
}
}
else if (temp > (p->data))
{
if (hasRC(p))
{
p = p->rc;
position.append("1");
}
else
{
position.append("1");
addRC(p, temp);
p->rc->pos = position;
std::cout << position;
printf("-%02d:%02d \n", temp / 60, temp % 60);
temp = -1;
}
}
else
{
i--;
temp = -1;
}
}
}
}
bool searchPath(binNodeArray(int) root, binNodeArray(int) target, Stack<binNodeArray(int)> *s)
{
if (root == NULL)
return false;
s->push(root);
if (root->data == target->data)
return true;
bool flag = false;
//先去左子树找
if (root->lc != NULL)
flag = searchPath(root->lc, target, s);
//左子树找不到并且右子树不为空的情况下才去找
if (!flag && root->rc != NULL)
flag = searchPath(root->rc, target, s);
//左右都找不到,弹出栈顶元素
if (!flag)
s->pop();
return flag;
}
//层次遍历
void trav_level()
{
Queue<binNodeArray(int)> q;
int depth = 0;
int old_depth = 0;
q.enqueue(root());
while (!q.empty())
{
binNodeArray(int) node = q.dequeue();
old_depth = depth;
depth = node->pos.size();
if (old_depth != depth)
printf("\n");
// std::cout << node->pos;
printf("%02d:%02d ", node->data / 60, node->data % 60);
if (hasLC(node))
q.enqueue(node->lc);
if (hasRC(node))
q.enqueue(node->rc);
}
}
void search_best(int target)
{
Queue<binNodeArray(int)> q;
int minus = 1440;
binNodeArray(int) target_p;
q.enqueue(root());
while (!q.empty())
{
binNodeArray(int) node = q.dequeue();
if(minus>abs(target-node->data))
{
minus = abs(target - node->data);
target_p = node;
}
if (hasLC(node))
q.enqueue(node->lc);
if (hasRC(node))
q.enqueue(node->rc);
}
std::cout << target_p->pos;
printf("-%02d:%02d \n", target_p->data / 60, target_p->data % 60);
}
//先序遍历
void trav_pre()
{
pre(root());
}
//后序遍历
void trav_post()
{
post(root());
}
//中序遍历
void trav_in()
{
in(root());
}
protected:
void pre(binNodeArray(int) node)
{
if (!node)
return;
std::cout << node->pos;
printf("-%02d:%02d \n", node->data / 60, node->data % 60);
pre(node->lc);
pre(node->rc);
}
void post(binNodeArray(int) node)
{
if (!node)
return;
pre(node->lc);
pre(node->rc);
std::cout << node->pos;
printf("-%02d:%02d \n", node->data / 60, node->data % 60);
}
void in(binNodeArray(int) node)
{
if (!node)
return;
pre(node->lc);
std::cout << node->pos;
printf("-%02d:%02d \n", node->data / 60, node->data % 60);
pre(node->rc);
}
public:
int height()
{
Queue<binNodeArray(int)> q;
q.enqueue(root());
int depth;
while (!q.empty())
{
binNodeArray(int) node = q.dequeue();
depth = node->pos.size();
if (hasLC(node))
q.enqueue(node->lc);
if (hasRC(node))
q.enqueue(node->rc);
}
return depth;
}
};

49
ex10/binnode.hpp Normal file
View file

@ -0,0 +1,49 @@
#ifndef _binnode_hpp_
#define _binnode_hpp_
#include <iostream>
#include <string>
#define binNodeArray(T) binNode<T> *
template <class T>
class binNode
{
public:
T data;
std::string pos;
binNode *parent;
binNode *lc;
binNode *rc;
~binNode()
{
delete lc;
delete rc;
}
binNode()
{
parent = NULL;
lc = NULL;
rc = NULL;
}
binNode(binNode *p, binNode *l, binNode *r)
{
parent = p;
lc = l;
rc = r;
}
bool operator>(binNode const &bn)
{
return data > bn.data;
}
bool operator<(binNode const &bn)
{
return data < bn.data;
}
bool operator==(binNode const &bn)
{
return data == bn.data;
}
};
#endif

9
ex10/bintree.h Normal file
View file

@ -0,0 +1,9 @@
//bintree宏定义
#include "binnode.hpp"
#define isRoot(x) (!(x->parent))
#define isLC(x) (!isRoot(x)&&((x)==(x->parent->lc))
#define isRC(x) (!isRoot(x)&&((x)==(x->parent->rc))
#define hasLC(x) (x->lc)
#define hasRC(x) (x->rc)

49
ex10/bintree.hpp Normal file
View file

@ -0,0 +1,49 @@
#ifndef _bintree_hpp_
#define _bintree_hpp_
#include "binnode.hpp"
#include "list.hpp"
#include "queue.hpp"
template <class T>
class binTree
{
protected:
binNodeArray(T) _root;
public:
binNodeArray(T) root()
{
return _root;
}
binTree()
{
_root = new binNode<T>;
}
binNodeArray(T) addLC(binNodeArray(T) node,T data)
{ if(node->lc)
return NULL;
binNodeArray(T) temp = new binNode<T>;
temp->data = data;
node->lc = temp;
return temp;
}
binNodeArray(T) addRC(binNodeArray(T) node,T data)
{ if(node->rc)
return NULL;
binNodeArray(T) temp = new binNode<T>;
temp->data = data;
node->rc = temp;
return temp;
}
void trav_level()
{
}
};
#endif
#include "bintree.h"

35
ex10/ex10.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "BST.hpp"
#include <iostream>
int main()
{
printf("初始化\n");
BST *b = new BST(31);
printf("层次");
b->trav_level();
// printf("\n高为%d\n", b->height());
restart:
printf("是否重新生成0/1\n");
int op = 2;
scanf("%d", &op);
if (op == 1)
{
delete b;
b = new BST(31);
printf("层次");
b->trav_level();
goto restart;
}
while (1)
{
printf("输入需要寻找的时间:");
int h = 0, m = 0;
scanf("%d:%d", &h,&m);
b->search_best(60 * h + m);
}
return 0;
}

123
ex10/list.hpp Normal file
View file

@ -0,0 +1,123 @@
#ifndef _LIST_HPP_
#define _LIST_HPP_
#include <iostream>
#include <string>
#define listNode ln *
template <class T>
class List
{
typedef struct ln
{
T data;
listNode pred;
listNode succ;
} ln;
private:
listNode header = new ln;
listNode trailer = new ln;
int _size;
protected:
listNode firstInsertN(listNode New);
listNode lastInsertN(listNode New);
listNode succInsertN(listNode old, listNode ne);
listNode predInsertN(listNode old, listNode ne);
void removeN(listNode i);
listNode searchN(int value, int n, listNode p);
public:
int size(){return _size;}
List();
void clear();
listNode first() { return header->succ; }
listNode last() { return trailer->pred; }
T firstInsert(T New);
T lastInsert(T New);
T removeLast();
listNode succInsert(listNode Old, T New);
listNode predInsert(listNode Old, T New);
int get(int i);
listNode find(int i);
T operator[](int i)
{
listNode p = first();
while (0 < i--)
p = p->succ;
return p->data;
}
protected:
T remove(listNode p);
};
template <class T>
List<T>::List()
{
_size = 0;
header->data = 0;
header->succ = trailer;
header->pred = NULL;
trailer->data = 0;
trailer->succ = NULL;
trailer->pred = header;
}
template <class T>
void List<T>::clear()
{
while (0 < _size)
remove(0);
}
template <class T>
T List<T>::firstInsert(T New)
{
listNode temp = new ln;
temp->data = New;
temp->pred = header;
temp->succ = header->succ;
header->succ = temp;
temp->succ->pred = temp;
_size++;
return New;
}
template <class T>
T List<T>::lastInsert(T New)
{
listNode temp = new ln;
temp->data = New;
temp->succ = trailer;
temp->pred = trailer->pred;
trailer->pred = temp;
temp->pred->succ = temp;
_size++;
return New;
}
template <class T>
T List<T>::removeLast()
{
return remove(trailer->pred);
}
template <class T>
T List<T>::remove(listNode p)
{
if (!_size)
return NULL;
T temp = p->data;
(p->pred)->succ = p->succ;
(p->succ)->pred = p->pred;
delete[] p;
_size--;
return temp;
}
#endif

45
ex10/queue.hpp Normal file
View file

@ -0,0 +1,45 @@
#ifndef _QUEUE_HPP_
#define _QUEUE_HPP_
#include <iostream>
#include "list.hpp"
template <class T>
class Queue
{
private:
List<T> q;
public:
T operator[](int i)
{
return q[i];
}
int size()
{
return q.size();
}
bool empty()
{
return size() ? false : true;
}
T enqueue(T e)
{
return q.firstInsert(e);
}
T dequeue()
{
return q.removeLast();
}
T front()
{
return q.last()->data;
}
T back()
{
return q.first()->data;
}
};
#endif

64
ex10/stack.hpp Normal file
View file

@ -0,0 +1,64 @@
#ifndef _STACK_HPP_
#define _STACK_HPP_
#include <iostream>
template <class T>
class Stack
{
private:
T *_v;
int _len = 10;
int _used = 0;
public:
Stack()
{
_v = new T[_len];
}
void push(T const &elem)
{
_v[_used++] = elem;
expand();
}
T pop() { return !_used ? NULL : _v[--_used]; }
T top() const { return !_used ? NULL : _v[_used - 1]; }
bool empty() { return _used ? false : true; }
int depth() { return _used; };
protected:
void expand()
{
if (((double)_used / (double)_len) <= 0.75)
return;
_len = _len * 2;
T *p = new T[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
void shrink()
{
if (((double)_used / (double)_len) >= 0.25)
return;
_len = _len >> 1;
T *p = new T[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
};
#endif

119
ex10/vector.hpp Normal file
View file

@ -0,0 +1,119 @@
#ifndef _VECTOR_HPP_
#define _VECTOR_HPP_
#include <iostream>
template <class T>
class Vector
{
private:
T *_v;
int _len;
int _used;
bool _sorted = false;
void expand();
void shrink();
void adjust();
public:
Vector(int len)
{
_used = 0;
_len = len;
_v = new int[_len];
}
Vector()
{
_used = 0;
_len = 10;
_v = new int[_len];
}
T get(int a){return _v[a];}
T operator[](int i){return _v[i];}
//排序搜索,返回秩
int search(T value);
void put(int a, int value);
void swap(int a, int b);
int insert(int locate, int value);
int remove(int locate, int value);
int remove_sorted(int value);
int find(int value);
void bubbleSort();
void printall();
int getlen();
int getused();
int count(int value);
void mergeSort(int lo, int hi);
private:
void merge(int lo, int mi, int hi);
};
template <class T>
void Vector<T>::expand()
{
_len = _len * 2;
T *p = new T[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
template <class T>
void Vector<T>::shrink()
{
_len = _len >> 1;
T *p = new T[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
template <class T>
void Vector<T>::adjust()
{
if (((double)_used / (double)_len) < 0.25)
shrink();
else if (((double)_used / (double)_len) > 0.75)
expand();
return;
}
template <class T>
int Vector<T>::search(T e)
{
if (_sorted == false)
return -1;
int i = 0;
for (; i <= _used && _v[i] <= e; i++)
;
return i - 1;
}
#endif