diff --git a/ex9/BST.hpp b/ex9/BST.hpp new file mode 100644 index 0000000..53f25e2 --- /dev/null +++ b/ex9/BST.hpp @@ -0,0 +1,110 @@ +#include "bintree.hpp" +#include "stack.hpp" +#include +#include +#include + +class BST : binTree +{ +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 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 *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; + } +}; \ No newline at end of file diff --git a/ex9/binnode.hpp b/ex9/binnode.hpp new file mode 100644 index 0000000..09cd7e3 --- /dev/null +++ b/ex9/binnode.hpp @@ -0,0 +1,47 @@ +#ifndef _binnode_hpp_ +#define _binnode_hpp_ +#include +#include +#define binNodeArray(T) binNode * + +template +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 \ No newline at end of file diff --git a/ex9/bintree.h b/ex9/bintree.h new file mode 100644 index 0000000..bfa99b3 --- /dev/null +++ b/ex9/bintree.h @@ -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) \ No newline at end of file diff --git a/ex9/bintree.hpp b/ex9/bintree.hpp new file mode 100644 index 0000000..c85dc02 --- /dev/null +++ b/ex9/bintree.hpp @@ -0,0 +1,51 @@ +#ifndef _bintree_hpp_ +#define _bintree_hpp_ +#include "binnode.hpp" +#include "list.hpp" +#include "queue.hpp" +template +class binTree +{ + protected: + binNodeArray(T) _root; + List plist; + int height; + + public: + binNodeArray(T) root() + { + return _root; + } + binTree() + { + _root = new binNode; + } + + binNodeArray(T) addLC(binNodeArray(T) node,T data) + { if(node->lc) + return NULL; + + binNodeArray(T) temp = new binNode; + 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; + temp->data = data; + node->rc = temp; + return temp; + } + void trav_level() + { + + } +}; + +#endif + +#include "bintree.h" \ No newline at end of file diff --git a/ex9/ex9.cpp b/ex9/ex9.cpp new file mode 100644 index 0000000..c18608f --- /dev/null +++ b/ex9/ex9.cpp @@ -0,0 +1,12 @@ +#include +#include "BST.hpp" + +int main() +{ + printf("初始化\n"); + BST bst(31); + printf("层次遍历\n"); + bst.trav_level(); + + return 0; +} \ No newline at end of file diff --git a/ex9/list.hpp b/ex9/list.hpp new file mode 100644 index 0000000..0966c4a --- /dev/null +++ b/ex9/list.hpp @@ -0,0 +1,123 @@ +#ifndef _LIST_HPP_ +#define _LIST_HPP_ +#include +#include +#define listNode ln * + +template +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 +List::List() +{ + _size = 0; + header->data = 0; + header->succ = trailer; + header->pred = NULL; + + trailer->data = 0; + trailer->succ = NULL; + trailer->pred = header; +} + +template +void List::clear() +{ + while (0 < _size) + remove(0); +} + +template +T List::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 +T List::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 +T List::removeLast() +{ + return remove(trailer->pred); +} + +template +T List::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 \ No newline at end of file diff --git a/ex9/queue.hpp b/ex9/queue.hpp new file mode 100644 index 0000000..2234165 --- /dev/null +++ b/ex9/queue.hpp @@ -0,0 +1,45 @@ +#ifndef _QUEUE_HPP_ +#define _QUEUE_HPP_ + +#include +#include "list.hpp" +template +class Queue +{ +private: + List 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 \ No newline at end of file diff --git a/ex9/stack.hpp b/ex9/stack.hpp new file mode 100644 index 0000000..f49d067 --- /dev/null +++ b/ex9/stack.hpp @@ -0,0 +1,64 @@ +#ifndef _STACK_HPP_ +#define _STACK_HPP_ +#include + + +template +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 \ No newline at end of file diff --git a/ex9/vector.hpp b/ex9/vector.hpp new file mode 100644 index 0000000..8f24532 --- /dev/null +++ b/ex9/vector.hpp @@ -0,0 +1,119 @@ +#ifndef _VECTOR_HPP_ +#define _VECTOR_HPP_ + +#include + +template +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 +void Vector::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 +void Vector::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 +void Vector::adjust() +{ + if (((double)_used / (double)_len) < 0.25) + shrink(); + else if (((double)_used / (double)_len) > 0.75) + expand(); + return; +} + + +template +int Vector::search(T e) +{ + if (_sorted == false) + return -1; + + int i = 0; + for (; i <= _used && _v[i] <= e; i++) + ; + + return i - 1; +} + +#endif \ No newline at end of file