/********************************** WinTreeConDes.cpp == Windows Last Left Threaded Data Tree Constructors and Distracters. If the pointers all stay legitimate then TreeData's distracter will free all the memory held by any remaining tree node a application termination. WinTree project file that holds TreeNode and TreeData class constructors and destructors. ***********************************/ #include "WinTree.h" // WinTree1.h is 'ifndef' so wont be multipled defind // ************************************************************************ // ++++++++++++++++++++ Class constructors and destructors. ++++++++++++++++ // TreeNode constructor initializes all pointers and data to NULL. // 'new' is used in TreeNode contructor so 'delete' is needed when a node is removed from the tree. TreeNode::TreeNode(const TCHAR *cData){ pLeftPoint = NULL; pRightPoint = NULL; pLeftThread = NULL; pParentNode = NULL; // TCHAR *szData; szData = new TCHAR[ wcslen( cData ) + 1]; assert( szData != 0 ); wcscpy( szData, cData); wcscpy( szAdata, L"............\0"); wcscpy( szBdata, L"............\0"); wcscpy( szCdata, L"............\0"); } //End constructor... // TreeNode destructor deallocates dynamically allocated memory if any... TreeNode::~TreeNode() { int stop ; stop = 1 ; delete szData ; pLeftPoint = NULL; pRightPoint = NULL; pLeftThread = NULL; pParentNode = NULL; } //End TreeNode destructor... // TreeData constructor initializes with pointer to Root of Data Tree. TreeData::TreeData(TreeNode* pROOT) { pRoot = pROOT; // Now Tree Data Class has address of tree root. // NULL out all other pointers and data members so program starts with fresh face. pLestSignificantNode = NULL ; pLastNodeViewed = NULL ; szCurrentTreeMembers[0] = NULL ; szNodeToRemove[0] = NULL ; szLastNodeRemoved[0] = NULL ; // Booleans set to false. fUpThread = false ; fLastNodeReached = false ; fLestSignificantNodeReached = false ; // iCurrentTreeMemberCount is a tally of the number of current tree nodes. iCurrentTreeMemberCount = 0 ; // Setting the year value to 0 will show if time stamp on node removed has happend or not... st1.wYear = 0 ; } // End TreeData constructor ... // TreeData destructor deallocates dynamically allocated memory if any... TreeData::~TreeData() { if( pRoot ) // Must check pointer to Root, if all tree members were deleted before app. termination { // then doing the following will throw an access acceptation. TreeNode* pTP ; TreeNode* pCP = pRoot ; bool fUpLLT = FALSE; // Just walk through what is left of the tree deleting as you go. At root can you go left, // if you can and you did not come up a 'last left thread' then go down the left pointer, else // if you came up a 'last left thread' or there is no left pointer go down the right pointer. // If you can't go left and you can't go right then go up the thread, if there is no thread, // that is if the LeftPointer is == NULL || LeftPointer is != NULL but fUpLLT (flag gone Up Last Left Thread) // is true && RightPointer == NULL && ThreadPointer == NULL // you are at the last node in the tree. while(1) { while(2) { if( pCP->FetchLeftPointer() != NULL && !(fUpLLT) ) pCP = pCP->FetchLeftPointer() ; else break ; } // End on while(2)... pTP = pCP ; if( pCP->FetchRightPointer() != NULL ) { pTP = pCP ; pCP = pCP->FetchRightPointer() ; fUpLLT = FALSE ; delete pTP ; continue ; } else { if( pCP->FetchLeftThreadPointer() != NULL ) { pTP = pCP ; pCP = pCP->FetchLeftThreadPointer() ; fUpLLT = TRUE ; delete pTP ; } else { break ; // Tree is deleted... } // End first else above... } // End second else above... } // End of while(1)... delete pCP ; } // End top 'if'... } //End TreeNode destructor... // ----------------- Class constructors and detructors. ------------------- // ************************************************************************