win32实现红黑树插入删除
Posted fulianzhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了win32实现红黑树插入删除相关的知识,希望对你有一定的参考价值。
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#define APP_NAME ("RBTreeGUI")
#define OPR_NAME ("OprWnd")
#define TOP_OFFSET (10)
#define BUTTOM_OFFSET (30)
#define NODE_WIDTH (30)
#define NODE_HIGHT (30)
#define TEXT_TOP_OFFSET (7)
#define NODE_WIDTH_OFFSET (5)
#define NODE_HIGHT_OFFSET (NODE_HIGHT + 10)
#define WM_OPR_KEY (WM_USER + 1)
#define WM_ADDNODE (WM_USER + 2)
#define WM_DELNODE (WM_USER + 3)
#define WM_MODNODE (WM_USER + 4)
#define WM_FINDNODE (WM_USER + 5)
#define WM_WORKLIST (WM_USER + 6)
#define WM_START_STOP (WM_USER + 7)
#define WM_NEXT_STEP (WM_USER + 8)
#define WM_AUTO (WM_USER + 10)
#define STEP_TIMERID (1000)
struct OprData
int cmd;
int key;
OprData()
OprData(const OprData& rst)
this->cmd = rst.cmd;
this->key = rst.key;
bool operator==(const OprData& src)
if (this->cmd == src.cmd &&
this->key == src.key)
return true;
return false;
;
#endif
filelog.h
#ifndef _FILE_LOG_H_
#define _FILE_LOG_H_
#define LOG_DIR ("log")
#define LOG_FILENAME ("rbtree")
class FileLog
public:
enum
TYPE_DEBUG,
TYPE_INFO,
TYPE_ERROR,
TYPE_RAW
;
private:
static FileLog* pThis;
private:
char szPath[1024];
void* hMutex;
public:
static FileLog* getInstance()
if (!pThis)
pThis = new FileLog;
pThis->init();
return pThis;
FileLog();
~FileLog();
public:
void init();
void uninit();
void doLog(const char* file, const int lineno, const char* func,int level, const char* fmt, ...);
;
#define LOGI(fmt, ...) FileLog::getInstance()->doLog(__FILE__, __LINE__, __FUNCTION__, FileLog::TYPE_DEBUG, fmt, __VA_ARGS__)
#define LOGD(fmt, ...) FileLog::getInstance()->doLog(__FILE__, __LINE__, __FUNCTION__, FileLog::TYPE_INFO, fmt, __VA_ARGS__)
#define LOGE(fmt, ...) FileLog::getInstance()->doLog(__FILE__, __LINE__, __FUNCTION__, FileLog::TYPE_ERROR, fmt, __VA_ARGS__)
#define LOGR(fmt, ...) FileLog::getInstance()->doLog(__FILE__, __LINE__, __FUNCTION__, FileLog::TYPE_RAW, fmt, __VA_ARGS__)
#endif
mainWnd.h
#ifndef _MAIN_WND_H_
#define _MAIN_WND_H_
class MainWnd
private:
void* hWnd;
public:
MainWnd();
~MainWnd();
public:
bool Create(void* hInst, const char* title, int cx, int cy, void* wndproc);
void Show(int iCmd);
void* GetHwnd() return hWnd;
;
#endif
oprWnd.h
#ifndef _OPR_WND_H_
#define _OPR_WND_H_
#include <list>
#include "common.h"
class OprWnd
private:
void* hWnd;
void* hWorkList;
void* hStartStop;
void* hNextStep;
void* hAuto;
bool mStart;
bool mAuto;
std::list<OprData> mOprQueue;
void* hMutex;
void* hStepSemp;
bool SempUsed;
public:
OprWnd();
~OprWnd();
private:
bool GetKeyText(int &key);
void FormatOpr(const OprData& opr, char* text, size_t sz);
public:
bool Create(void* hInst, const char* title, int cx, int cy, void* wndproc);
void Show(int iCmd);
void* GetHwnd() return hWnd;
void OnAddNode();
void OnDelNode();
void OnModNode();
void OnFindNode();
void OnStartStop();
void OnNextStep();
void OnAuto();
bool GetNextOpr(OprData& opr);
bool IsAuto() return mAuto;
void WaitSignal();
;
#endif
RBTree.h
#ifndef _RB_TREE_H_
#define _RB_TREE_H_
#include <utility>
enum
RED = 0,
BLACK,
HIGHLIGHT
;
template <typename K, typename V>
class RBTree
public:
RBTree<K, V>* left;
RBTree<K, V>* right;
RBTree<K, V>* parent;
std::pair<K, V> kv;
unsigned char col;
public:
RBTree() :
left(nullptr),
right(nullptr),
parent(nullptr),
col(RED)
RBTree(const std::pair<K, V>& _kv) :
left(nullptr),
right(nullptr),
parent(nullptr),
kv(_kv),
col(RED)
~RBTree()
;
#endif
wndMng.h
#ifndef _WND_MNG_H_
#define _WND_MNG_H_
#include "mainWnd.h"
#include "oprWnd.h"
#include "RBTree.h"
typedef struct ElemType
int count;
int pos_x;
int pos_y;
int paint_x;
int paint_y;
bool highlight;
ElemType;
class WndMng
enum
PRE_ORDER,
IN_ORDER,
POST_ORDER
;
private:
MainWnd mMainWnd;
OprWnd *mOprWnd;
RBTree<int, ElemType>* mRbTree;
private:
void* redBrush;
void* blackBrush;
void* highlightBrush;
private:
int RBTreeCalcPos();
void RBTreeSimpleInsert(const std::pair<int, ElemType>& item);
void RBTreeCalcPaintPos(int deep);
void RBTreePreTraver(RBTree<int, ElemType>* node);
void RBTreeInTraver(RBTree<int, ElemType>* node);
void RBTreePostTraver(RBTree<int, ElemType>* node);
void RBTreeTraver(unsigned char order);
void RBTreeRotateL(RBTree<int, ElemType>* node); // 左旋树
void RBTreeRotateR(RBTree<int, ElemType>* node); // 右旋树
char* RBTreeGetNodeInfo(RBTree<int, ElemType>* cur);
char* RBTreeGetTreeInfo(RBTree<int, ElemType>* cur, RBTree<int, ElemType>* parent, RBTree<int, ElemType>* grandfather, RBTree<int, ElemType>* uncle);
public:
void RBTreeInsert(const std::pair<int, ElemType>& key);
bool RBTreeDelete(const std::pair<int, ElemType>& key);
RBTree<int, ElemType>* RBTreeFind(const std::pair<int, ElemType>& key);
RBTree<int, ElemType>* RBTreeModify(const std::pair<int, ElemType>& key);
void DoWork();
public:
void OnRButtonUp(void* hwnd, unsigned long lpParam);
void ReportAndWait();
public:
WndMng();
~WndMng();
void Init();
void Create(void* hInst);
void StartWork();
void PaintNode(void* hdc, int x, int y, const char* text, unsigned char col = RED, bool highlight = false);
void PaintTree(void* hdc);
void HideOprWnd();
void OnOprWndCommand(void* hwnd, unsigned long wParam, unsigned long lParam);
;
#endif
filelog.cpp
#include "../include/filelog.h"
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <stdarg.h>
FileLog* FileLog::pThis = NULL;
FileLog::FileLog()
hMutex = NULL;
FileLog::~FileLog()
uninit();
void FileLog::init()
char appPath[512] = 0 ;
GetModuleFileNameA(NULL, appPath, sizeof(appPath));
const char* lpchr = strrchr(appPath, '\\\\');
if (!lpchr)
return;
char appDir[512] = 0 ;
strncpy_s(appDir, appPath, lpchr - appPath);
strcat_s(appDir, "\\\\log");
CreateDirectoryA(appDir, NULL);
time_t now = time(0);
tm tnow;
localtime_s(&tnow, &now);
char szTime[64] = 0 ;
strftime(szTime, sizeof(szTime), "%Y-%m-%d", &tnow);
sprintf_s(szPath, sizeof(szPath), "%s\\\\%s_log.txt", appDir, szTime);
hMutex = OpenMutexA(SYNCHRONIZE, FALSE, "rbtree");
void FileLog::uninit()
if (hMutex)
CloseHandle(hMutex);
hMutex = NULL;
void FileLog::doLog(const char* file, const int lineno, const char* func, int level, const char* fmt, ...)
char fileBuff[4096] = 0 ;
va_list ap;
va_start(ap, fmt);
vsnprintf_s(fileBuff, sizeof(fileBuff), fmt, ap);
va_end(ap);
WaitForSingleObject(hMutex, INFINITE);
FILE* fp = NULL;
fopen_s(&fp, szPath, "a+");
if (fp)
switch (level)
case TYPE_DEBUG:
fprintf_s(fp, "[%s:%d %s][DEBUG]%s\\n", strrchr(file,'\\\\'以上是关于win32实现红黑树插入删除的主要内容,如果未能解决你的问题,请参考以下文章