首页 > 代码库 > C++__二叉树(练习)
C++__二叉树(练习)
二叉树
文件结构:二叉树→TREE→TREE.h、TREE.cpp
→QUEUE→QUEUE.h、QUEUE.cpp
→main.cpp
queue.h
#ifndef QUEUE_H_ #define QUEUE_H_ #define SIZE 4 typedef int data_type; enum QUEUE_OP { QUEUE_ERR = -1, QUEUE_OK, QUEUE_EMPTY }; class QUEUE { private: data_type *data; int count; int iFront; int iRear; public: QUEUE(); ~QUEUE(); int getCount() const; data_type getData(unsigned int i) const; data_type *getData() const; int getFront() const; int getRear() const; void setCount(int count); void setData(data_type data, unsigned int i); void setData(data_type *data); void setFront(int front); void setRear(int rear); // QUEUE *Create(); // void Destroy(QUEUE *pQueue); int EnQueue(QUEUE *pQueue, data_type tData); int DeQueue(QUEUE *pQueue, data_type *pData); int IsEmpty(QUEUE *pQueue); void operator delete(void *pQueue); }; #endif /* QUEUE_H_ */
queue.cpp
#include "QUEUE.h" #include <iostream> using namespace std; QUEUE::QUEUE() { // TODO Auto-generated constructor stub cout<<"queue"<<endl; this->setCount(0); this->setFront(0); this->setRear(0); int *data = http://www.mamicode.com/new data_type[SIZE]; if (!data) { cout << "new data[SIZE] error" << endl; this->setData(NULL); } else { this->setData(new data_type[SIZE]); } } QUEUE::~QUEUE() { // TODO Auto-generated destructor stub cout<<"~queue"<<endl; } int QUEUE::getCount() const { return count; } data_type QUEUE::getData(unsigned int i) const { return data[i]; } data_type *QUEUE::getData() const { return data; } int QUEUE::getFront() const { return iFront; } int QUEUE::getRear() const { return iRear; } void QUEUE::setCount(int count) { this->count = count; } void QUEUE::setData(data_type data, unsigned int i) { this->data[i] = data; } void QUEUE::setData(data_type *data) { this->data =http://www.mamicode.com/ data; } void QUEUE::setFront(int front) { iFront = front; } void QUEUE::setRear(int rear) { iRear = rear; } int QUEUE::EnQueue(QUEUE *pQueue, data_type tData) { if (!pQueue) return QUEUE_ERR; if (SIZE == pQueue->getCount()) { cout << "queue en over" << endl; return QUEUE_ERR; } pQueue->setData(tData, pQueue->getRear()); pQueue->setRear(pQueue->getRear() + 1); if(SIZE == pQueue->getRear()) pQueue->setRear(0); pQueue->setCount(pQueue->getCount() + 1); return QUEUE_OK; } int QUEUE::DeQueue(QUEUE *pQueue, data_type *pData) { if ((!pQueue) || (!pData)) return QUEUE_ERR; if (0 == pQueue->getCount()) { cout << "queue de over" << endl; return QUEUE_ERR; } *pData = http://www.mamicode.com/pQueue->getData(pQueue->getFront()); pQueue->setFront(pQueue->getFront() + 1); if(SIZE == pQueue->getFront()) pQueue->setFront(0); pQueue->setCount(pQueue->getCount() - 1); return QUEUE_OK; } int QUEUE::IsEmpty(QUEUE *pQueue) { if(!pQueue) return QUEUE_ERR; if(0 == pQueue->getCount()) return QUEUE_EMPTY; return QUEUE_OK; } void QUEUE::operator delete(void *pQueue){ if(!pQueue) return; data_type *Tmp = ((QUEUE *)pQueue)->getData(); free(Tmp); free(pQueue); return; }
tree.h
#ifndef TREE_H_ #define TREE_H_ typedef int data_type; enum TREE_OP { TREE_ERR = -1, TREE_OK }; class TREE { private: TREE *left; data_type data; TREE *right; public: TREE(data_type data); ~TREE(); data_type getData() const; TREE *getLeft() const; TREE *getRight() const; void setData(data_type data); void setLeft(TREE *left); void setRight(TREE *right); int Create(TREE *pRoot, data_type data); void PreOrder(TREE *pRoot); void InOrder(TREE *pRoot); void PostOrder(TREE *pRoot); void NoOrder(TREE *pRoot); void operator delete(void *pRoot); }; #endif /* TREE_H_ */
tree.cpp
#include "TREE.h" #include "../QUEUE/QUEUE.h" #include <iostream> using namespace std; TREE::TREE(data_type data) { // TODO Auto-generated constructor stub this->setData(data); this->setLeft(NULL); this->setRight(NULL); } TREE::~TREE() { // TODO Auto-generated destructor stub } data_type TREE::getData() const { return data; } TREE *TREE::getLeft() const { return left; } TREE *TREE::getRight() const { return right; } void TREE::setData(data_type data) { this->data =http://www.mamicode.com/ data; } void TREE::setLeft(TREE *left) { this->left = left; } void TREE::setRight(TREE *right) { this->right = right; } int TREE::Create(TREE *pRoot, data_type data) { if (!pRoot) return TREE_ERR; TREE *New = new TREE(data); if (!New) { cout << "new New error" << endl; return TREE_ERR; } TREE *Tmp = pRoot; while (1) { if (New->getData() > Tmp->getData()) { if (!Tmp->getRight()) { Tmp->setRight(New); break; } else { Tmp = Tmp->getRight(); } } else { if (!Tmp->getLeft()) { Tmp->setLeft(New); break; } else { Tmp = Tmp->getLeft(); } } } return TREE_OK; } void TREE::PreOrder(TREE *pRoot) { if (!pRoot) return; cout << pRoot->getData() << " "; pRoot->PreOrder(pRoot->getLeft()); pRoot->PreOrder(pRoot->getRight()); // cout << endl; return; } void TREE::InOrder(TREE *pRoot) { if (!pRoot) return; pRoot->InOrder(pRoot->getLeft()); cout << pRoot->getData() << " "; pRoot->InOrder(pRoot->getRight()); // cout << endl; return; } void TREE::PostOrder(TREE *pRoot) { if (!pRoot) return; pRoot->PostOrder(pRoot->getLeft()); pRoot->PostOrder(pRoot->getRight()); cout << pRoot->getData() << " "; //<< endl; return; } void TREE::NoOrder(TREE *pRoot) { if (!pRoot) return; data_type data; TREE *Tmp = NULL; QUEUE *pQueue = new QUEUE; if (!pQueue) { cout << "new queue error" << endl; return; } if (QUEUE_OK != pQueue->EnQueue(pQueue, (data_type) pRoot)) { cout << "en tree error" << endl; return; } while (QUEUE_OK == pQueue->IsEmpty(pQueue)) { if (QUEUE_OK != pQueue->DeQueue(pQueue, &data)) { cout << "de tree error" << endl; break; } Tmp = (TREE *) data; cout << Tmp->getData() << " "; if (Tmp->getLeft()) { pQueue->EnQueue(pQueue, (data_type) Tmp->getLeft()); } if (Tmp->getRight()) { pQueue->EnQueue(pQueue, (data_type) Tmp->getRight()); } } cout << endl; delete pQueue; return; } void TREE::operator delete(void *pRoot) { if (!pRoot) return; delete ((TREE *) pRoot)->getLeft(); delete ((TREE *) pRoot)->getRight(); free(pRoot); return; }
main.cpp
#include "TREE/TREE.h" #include "QUEUE/QUEUE.h" #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; void function() { srand((unsigned int)time(NULL)); int i = rand() % 100; cout<<i<<" "; TREE *pRoot = new TREE(i); if (!pRoot) { cout << "new root error" << endl; return; } int *data = http://www.mamicode.com/new int[10]; i = 10; while (i--) { data[i] = rand() % 100; pRoot->Create(pRoot, data[i]); cout << data[i] << " "; } cout << endl; pRoot->PreOrder(pRoot);cout << endl; pRoot->InOrder(pRoot);cout << endl; pRoot->PostOrder(pRoot);cout << endl; pRoot->NoOrder(pRoot); delete pRoot; } int main() { function(); return 0; }
C++__二叉树(练习)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。