This commit is contained in:
iridiumR 2021-12-03 15:33:31 +08:00
parent a887c5e047
commit ae5ac85603
2 changed files with 148 additions and 18 deletions

View file

@ -1,8 +1,9 @@
#include "bintree.hpp"
#include "stack.hpp"
#include <time.h>
#include <iostream>
#include <string>
#include <time.h>
#include <cmath>
class BST : binTree<int>
{
@ -10,6 +11,10 @@ private:
binNodeArray(int) p;
public:
~BST()
{
}
BST(int num)
{
srand(time(NULL));
@ -70,23 +75,7 @@ public:
}
}
}
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)
@ -107,4 +96,133 @@ public:
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;
}
};

View file

@ -5,8 +5,20 @@ int main()
{
printf("初始化\n");
BST bst(31);
printf("层次遍历\n");
printf("========层次遍历========\n");
bst.trav_level();
printf("\n========先序遍历========\n");
bst.trav_pre();
printf("========中序遍历========\n");
bst.trav_in();
printf("========后序遍历========\n");
bst.trav_post();
printf("高度为%d\n",bst.height());
return 0;
}