9勉强能用

This commit is contained in:
iridiumR 2021-12-03 00:09:43 +08:00
parent ebbf118aa2
commit 7f83a91eba
9 changed files with 580 additions and 0 deletions

110
ex9/BST.hpp Normal file
View file

@ -0,0 +1,110 @@
#include "bintree.hpp"
#include "stack.hpp"
#include <time.h>
#include <iostream>
#include <string>
class BST : binTree<int>
{
private:
binNodeArray(int) p;
public:
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;
}
}
}
}
void trav_level()
{
Queue<binNodeArray(int)> q;
q.enqueue(root());
while (!q.empty())
{
binNodeArray(int) node = q.dequeue();
std::cout << node->pos;
printf("-%02d:%02d \n", node->data / 60, node->data % 60);
if (hasLC(node))
q.enqueue(node->lc);
if (hasRC(node))
q.enqueue(node->rc);
}
}
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;
}
};

47
ex9/binnode.hpp Normal file
View file

@ -0,0 +1,47 @@
#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()
{
parent = NULL;
lc = NULL;
rc = NULL;
}
binNode(binNode *p, binNode *l, binNode *r)
{
parent = p;
lc = l;
rc = r;
}
~binNode()
{
}
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
ex9/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)

51
ex9/bintree.hpp Normal file
View file

@ -0,0 +1,51 @@
#ifndef _bintree_hpp_
#define _bintree_hpp_
#include "binnode.hpp"
#include "list.hpp"
#include "queue.hpp"
template <class T>
class binTree
{
protected:
binNodeArray(T) _root;
List<T> plist;
int height;
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"

12
ex9/ex9.cpp Normal file
View file

@ -0,0 +1,12 @@
#include <iostream>
#include "BST.hpp"
int main()
{
printf("初始化\n");
BST bst(31);
printf("层次遍历\n");
bst.trav_level();
return 0;
}

123
ex9/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
ex9/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
ex9/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
ex9/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