求遍历 生成一个树目录的算法。
将取到的(0,A) (0,B) (0,C)(A,A1) (B,B1) (C,C1)
(A,A2) (B,B2)。。。。。。。。。这种规律的一组数据
以树目录显示
如:
A
...A1
...A2
B
...B1
...B2
C
...C1
.
.
.
#ifndef _CUserFileClass_H_ #define _CUserFileClass_H_ #ifndef NAME_LENGTH #define NAME_LENGTH 50 #endif #ifndef MAX_PATH #define MAX_PATH 250 #endif //------------------------------------------ // 作者:守候 | // 日期:2013.2.7 | // 内容:多叉树 | //------------------------------------------ #include <iostream> using namespace std; #include <vector> #include <iterator> class CUserFileClass; class CUserFileNode { public: friend class CUserFileClass; CUserFileNode(); ~CUserFileNode(); CUserFileNode(const CUserFileNode &t); CUserFileNode & operator = (const CUserFileNode &t); public: BOOL setFileName(const CHAR* _str); // BOOL setFilePath(const CHAR* _str); // BOOL setFileType(const CHAR* _str); CUserFileNode *getLeftChild() { return m_pLeftChild; } CUserFileNode *getRightBrother() { return m_pRightBrother; } __int32 getDeep() { return m_nDeep; } CHAR *getFileName() { return m_strFileName; } private: //----function---- void clear(); void copy(const CUserFileNode& t); //----member---- CHAR m_strFileName[NAME_LENGTH]; // CHAR m_strFileType[NAME_LENGTH]; // CHAR m_strFilePath[MAX_PATH]; __int32 m_nDeep; CUserFileNode *m_pLeftChild; CUserFileNode *m_pRightBrother; }; //---------------------------------- #define FILE_OK 0 class CUserFileClass { public: CUserFileClass(); ~CUserFileClass(); CUserFileClass(const CUserFileClass & t); CUserFileClass &operator=(const CUserFileClass & t); //----insert---- CUserFileNode* insert_root(const CUserFileNode *root); void insert_left(CUserFileNode* root_val,CUserFileNode *root_aim); void insert_right(CUserFileNode* root_val,CUserFileNode *root_aim); void clear() { m_size = 0; m_Array.clear(); delete_all(m_root->m_pLeftChild); m_root->m_pLeftChild = NULL; m_root->m_pRightBrother = NULL; } //----check and find ---- CUserFileNode* getRoot() { return m_root; } __int32 getSize(); vector<CUserFileNode*>& midArray(); void refresh(); private: CUserFileNode *m_root; __int32 m_size; vector<CUserFileNode*> m_Array; //------------function--------- void delete_all(CUserFileNode *root); void copy(CUserFileNode **root_val,const CUserFileNode *root_aim,__int32 _deep); //---check and find--- void algorithm_size(const CUserFileNode *root); void algorithm_mid(const CUserFileNode *root); }; #endif
#include "stdafx.h" #include "CUserFileClass.h" //------------------------Node----------------------- CUserFileNode::CUserFileNode() { clear(); } CUserFileNode::~CUserFileNode() { clear(); } CUserFileNode::CUserFileNode(const CUserFileNode &t) { copy(t); } CUserFileNode & CUserFileNode::operator =(const CUserFileNode&t) { if(this == &t) return *this; copy(t); return *this; } void CUserFileNode::clear() { memset(m_strFileName,0,NAME_LENGTH); // memset(m_strFileType,0,NAME_LENGTH); // memset(m_strFilePath,0,MAX_PATH); m_pLeftChild = NULL; m_pRightBrother = NULL; m_nDeep = 0; } void CUserFileNode::copy(const CUserFileNode& t) { memcpy(m_strFileName,t.m_strFileName,NAME_LENGTH); // memcpy(m_strFileType,t.m_strFileType,NAME_LENGTH); // memcpy(m_strFilePath,t.m_strFilePath,MAX_PATH); // m_pLeftChild = t.m_pLeftChild; // m_pRightBrother = t.m_pRightBrother; } BOOL CUserFileNode::setFileName(const CHAR* _str) { __int32 len = _tcslen(_str); if(len > NAME_LENGTH) return FALSE; memset(m_strFileName,0,NAME_LENGTH); memcpy(m_strFileName,_str,len); return TRUE; } // BOOL CUserFileNode::setFilePath(const CHAR* _str) // { // __int32 len = _tcslen(_str); // if(len > MAX_PATH) // return FALSE; // memset(m_strFilePath,0,MAX_PATH); // memcpy(m_strFilePath,_str,len); // return TRUE; // } // BOOL CUserFileNode::setFileType(const CHAR* _str) // { // int len = _tcslen(_str); // if(len > NAME_LENGTH) // return FALSE; // memset(m_strFileType,0,NAME_LENGTH); // memcpy(m_strFileType,_str,len); // return TRUE; // } //------------------------ //-------------------------root---------------------- CUserFileClass::CUserFileClass() { m_root = new CUserFileNode; m_root->m_nDeep = 0; m_size = 0; } CUserFileClass::~CUserFileClass() { delete_all(m_root); m_root = NULL; m_size = 0; } CUserFileClass::CUserFileClass(const CUserFileClass & t) { delete_all(m_root); copy(&m_root,t.m_root,t.m_root->m_nDeep); m_size = t.m_size; } CUserFileClass & CUserFileClass::operator=(const CUserFileClass & t) { if(this == &t) return *this; delete_all(m_root); copy(&m_root,t.m_root,t.m_root->m_nDeep); m_size = t.m_size; return *this; } //-----------------insert----------------- CUserFileNode* CUserFileClass::insert_root(const CUserFileNode *root) { CUserFileNode *_pR = m_root->m_pLeftChild; if(_pR == NULL) { copy(&m_root->m_pLeftChild,root,m_root->m_nDeep + 1); return m_root->m_pLeftChild; } else { while(_pR->m_pRightBrother) _pR = _pR->m_pRightBrother; copy(&_pR->m_pRightBrother,root,m_root->m_nDeep + 1); return _pR->m_pRightBrother; } return NULL; } void CUserFileClass::insert_left(CUserFileNode* root_val,CUserFileNode *root_aim) { if(root_val->m_pLeftChild == NULL) copy(&root_val->m_pLeftChild,root_aim,root_val->m_nDeep + 1); else { CUserFileNode *_pl = root_val->m_pLeftChild; CUserFileNode *_paim = root_aim; while(_paim->m_pLeftChild) _paim = _paim->m_pLeftChild; copy(&root_val->m_pLeftChild,root_aim,root_val->m_nDeep + 1); _paim->m_pLeftChild = _pl; } } void CUserFileClass::insert_right(CUserFileNode* root_val,CUserFileNode *root_aim) { if(root_val->m_pRightBrother == NULL) copy(&root_val->m_pRightBrother,root_aim,root_val->m_nDeep); else { CUserFileNode *_pl = root_val->m_pRightBrother; CUserFileNode *_paim = root_aim; while(_paim->m_pRightBrother) _paim = _paim->m_pRightBrother; copy(&root_val->m_pRightBrother,root_aim,root_val->m_nDeep); _paim->m_pRightBrother = _pl; } } //---------------------------------------- void CUserFileClass::delete_all(CUserFileNode *root) { if(root != NULL) { delete_all(root->m_pLeftChild); delete_all(root->m_pRightBrother); delete root; } } void CUserFileClass::copy(CUserFileNode **root_val,const CUserFileNode *root_aim,__int32 _deep) { if(root_aim != NULL) { *root_val = new CUserFileNode; ASSERT(root_val != NULL); **root_val = *root_aim; (*root_val)->m_nDeep = _deep; copy(&(*root_val)->m_pLeftChild,root_aim->m_pLeftChild, (*root_val)->m_nDeep + 1); copy(&(*root_val)->m_pRightBrother,root_aim->m_pRightBrother, (*root_val)->m_nDeep); } } //-------------check and find-------------------- void CUserFileClass::refresh() { m_size = 0; m_Array.clear(); algorithm_size(m_root->m_pLeftChild); algorithm_mid(m_root->m_pLeftChild); } __int32 CUserFileClass::getSize() { return m_size; } vector<CUserFileNode*>& CUserFileClass::midArray() { return m_Array; } void CUserFileClass::algorithm_size(const CUserFileNode *root) { if(root != NULL) { m_size ++; algorithm_size(root->m_pLeftChild); algorithm_size(root->m_pRightBrother); } } void CUserFileClass::algorithm_mid(const CUserFileNode *root) { if(root != NULL) { m_Array.push_back((CUserFileNode*)root); algorithm_mid(root->m_pLeftChild); algorithm_mid(root->m_pRightBrother); } }