数据结构基础
#include <iostream> #include <Windows.h> using namespace std; typedef struct _NODE_ { _NODE_* pRight; _NODE_* pLeft; int e; int Count; void InitMember(int e) { pRight = pLeft = NULL; Count = 1; this->e = e; } }Node,*pNode; typedef struct _QUEUE_//存储兄弟节点 { pNode pNodeTemp; _QUEUE_* pNext; void InitMember() { pNodeTemp = NULL; pNext = NULL; } }Queue,*pQueue; typedef struct _QUEUELIST_ { pQueue pHead; pQueue pTail; int nCount; void InitMember() { nCount = 0; pHead = pTail = NULL; } }QueueList,*pQueueList; BOOL InQueue(pQueueList pQueueListTemp,pNode pNodeTemp); //BinaryTree Node Insert QueueList BOOL OutQueue(pQueueList pQueueListTemp,pNode& pNodeTemp); BOOL IsQueueEmpty(pQueueList pQueueListTemp); void DestroyQueue(pQueueList pQueueListTemp); BOOL CreateTree(pNode& pNodeTemp); BOOL PreTravelTree(pNode pNodeTemp); BOOL MidTravelTree(pNode pNodeTemp); //! SortTree; BOOL PostTravelTree(pNode pNodeTemp); BOOL LevelTravelTree(pNode pNodeTemp); //! Queue void DestroyTree(pNode& pNodeTemp); int GetDepth(pNode pNodeTemp); BOOL GetFather(pNode pNodeTemp,int e,pNode& pNodeFather); BOOL GetLeftSon(pNode pNodeFather); BOOL GetRightSon(); BOOL GetLeftBrother(pNode pNodeTemp); BOOL GetRightBrother(pNode pNodeTemp); BOOL FindNode(pNode pNodeRoot,int e,pNode& pNodeTemp); BOOL CreateSortTree(pNode& pNodeTemp,int a); BOOL InsertNode(); BOOL DeleteNode(); //LineTree ListTree //Parent //Brother //BinaryTree 3叉List ---》双向二叉树 //Binary Tree Sort Tree AVL 索引Tree 线索Tree 伸展Tree BTree B+ Tree B- Tree //字典Tree 键Tree 双键链Tree 红黑树 //SortTree //通信 //sys HideProcss int main() { pNode pNodeTemp = NULL; int a[] = {1,20,9,9,67,87,-2,-100,43,-2}; int i = 0; for (i=0;i<sizeof(a)/sizeof(int);i++) { if (CreateSortTree(pNodeTemp,a[i])==FALSE) //MidTravel { return 0; } } PreTravelTree( pNodeTemp); // MidTravelTree( pNodeTemp); //! SortTree; // PostTravelTree( pNodeTemp); // LevelTravelTree( pNodeTemp); MidTravelTree(pNodeTemp); cout<<endl; cout<<GetDepth(pNodeTemp)<<endl; pNode pNodeFather = NULL; if (GetFather(pNodeTemp,87,pNodeFather)==TRUE) { cout<<pNodeFather->e<<endl; } DestroyTree(pNodeTemp); /* pNode pNodeTemp = NULL; if(CreateTree(pNodeTemp)==FALSE) { return; } cout<<"Pre:"<<endl; PreTravelTree(pNodeTemp); cout<<endl; cout<<"Mid:"<<endl; MidTravelTree(pNodeTemp); cout<<endl; cout<<"Post:"<<endl; PostTravelTree(pNodeTemp); cout<<endl; cout<<"Level:"<<endl; LevelTravelTree(pNodeTemp); cout<<endl; DestroyTree(pNodeTemp); } BOOL CreateTree(pNode& pNodeTemp) { int a = 0; cin>>a; if(a==-1) { return FALSE; } pNodeTemp = (pNode)malloc(sizeof(Node)); if(pNodeTemp==NULL) { return FALSE; } pNodeTemp->InitMember(a); CreateTree(pNodeTemp->pLeft); CreateTree(pNodeTemp->pRight); return TRUE;*/ } BOOL CreateSortTree(pNode& pNodeTemp,int a) //3 { if (pNodeTemp!=NULL) { if (a>pNodeTemp->e) // 1 { CreateSortTree(pNodeTemp->pLeft,a); } else if(a<pNodeTemp->e) { CreateSortTree(pNodeTemp->pRight,a); } else { pNodeTemp->Count++; return TRUE; } } else { pNodeTemp = (pNode)malloc(sizeof(Node)); if (pNodeTemp==NULL) { return FALSE; } pNodeTemp->InitMember(a); return TRUE; } } BOOL PreTravelTree(pNode pNodeTemp) //Travel { if(pNodeTemp!=NULL) { int i = 0; for (i=0;i<pNodeTemp->Count;i++) { cout<<pNodeTemp->e<<" "; } //for for if(pNodeTemp->pLeft!=NULL) { PreTravelTree(pNodeTemp->pLeft); } if(pNodeTemp->pRight!=NULL) { PreTravelTree(pNodeTemp->pRight); } } return TRUE; } BOOL MidTravelTree(pNode pNodeTemp) { if(pNodeTemp!=NULL) { //for for if(pNodeTemp->pLeft!=NULL) { MidTravelTree(pNodeTemp->pLeft); } int i = 0; for (i=0;i<pNodeTemp->Count;i++) { cout<<pNodeTemp->e<<" "; } if(pNodeTemp->pRight!=NULL) { MidTravelTree(pNodeTemp->pRight); } } return TRUE; } BOOL PostTravelTree(pNode pNodeTemp) { if(pNodeTemp!=NULL) { //for for if(pNodeTemp->pLeft!=NULL) { PostTravelTree(pNodeTemp->pLeft); } if(pNodeTemp->pRight!=NULL) { PostTravelTree(pNodeTemp->pRight); } int i = 0; for (i=0;i<pNodeTemp->Count;i++) { cout<<pNodeTemp->e<<" "; } } return TRUE; } BOOL LevelTravelTree(pNode pNodeTemp) { QueueList QueueListTemp; QueueListTemp.InitMember(); pNode pNodeNew = NULL; if(pNodeTemp!=NULL) { if(InQueue(&QueueListTemp,pNodeTemp)==FALSE) { return FALSE; } while(!IsQueueEmpty(&QueueListTemp)) { if(OutQueue(&QueueListTemp,pNodeNew)==FALSE) { return FALSE; } cout<<pNodeNew->e<<endl; if(pNodeNew->pLeft!=NULL) { InQueue(&QueueListTemp,pNodeNew->pLeft); } if(pNodeNew->pRight!=NULL) { InQueue(&QueueListTemp,pNodeNew->pRight); } } } return TRUE; } void DestroyTree(pNode& pNodeTemp) { if(pNodeTemp!=NULL) { if(pNodeTemp->pLeft!=NULL) { DestroyTree(pNodeTemp->pLeft); } if(pNodeTemp->pRight!=NULL) { DestroyTree(pNodeTemp->pRight); } free(pNodeTemp); } } BOOL InQueue(pQueueList pQueueListTemp,pNode pNodeTemp) { pQueue pQueueTemp = (pQueue)malloc(sizeof(Queue)); if(pQueueTemp==NULL) { return FALSE; } pQueueTemp->InitMember(); pQueueTemp->pNodeTemp = pNodeTemp; if(pQueueListTemp->pHead==NULL) { pQueueListTemp->pHead = pQueueListTemp->pTail = pQueueTemp; } else { pQueueListTemp->pTail->pNext = pQueueTemp; pQueueListTemp->pTail = pQueueTemp; } pQueueListTemp->nCount++; return TRUE; } BOOL OutQueue(pQueueList pQueueListTemp,pNode& pNodeTemp) { if(IsQueueEmpty(pQueueListTemp)) { return FALSE; } pNodeTemp = pQueueListTemp->pHead->pNodeTemp; pQueue pQueueTemp = NULL; pQueueListTemp->pHead = pQueueListTemp->pHead->pNext; free(pQueueTemp); pQueueListTemp->nCount--; if(pQueueListTemp->nCount==0) { pQueueListTemp->pTail = NULL; } return TRUE; } //1 i=1; j = 0 int GetDepth(pNode pNodeTemp) //Line List { if (pNodeTemp==NULL) { return -1; } int i = 0; int j = 0; if (pNodeTemp->pLeft!=NULL) { i = GetDepth(pNodeTemp->pLeft); //i = -1; } else { i = 0; } if (pNodeTemp->pRight!=NULL) { j = GetDepth(pNodeTemp->pRight); } else { j = 0; } return i>j?i+1:j+1; } void DestroyQueue(pQueueList pQueueListTemp) { pNode pNodeTemp = NULL; while (!IsQueueEmpty(pQueueListTemp)) { OutQueue(pQueueListTemp,pNodeTemp); //NULL = 10; } } BOOL IsQueueEmpty(pQueueList pQueueListTemp) { if(pQueueListTemp->nCount==0) { return TRUE; } return FALSE; } BOOL FindNode(pNode pNodeRoot,int e,pNode& pNodeTemp) { QueueList QueueListTemp; QueueListTemp.InitMember(); if (pNodeRoot==NULL) { return FALSE; } else { if (pNodeRoot->e==e) { pNodeTemp = pNodeRoot; return TRUE; } else { InQueue(&QueueListTemp,pNodeRoot); pNode pNodeNew = NULL; while (!IsQueueEmpty(&QueueListTemp)) { OutQueue(&QueueListTemp,pNodeNew); if (pNodeNew->pLeft!=NULL) { if (pNodeNew->pLeft->e==e) { pNodeTemp = pNodeNew->pLeft; return TRUE; } else { InQueue(&QueueListTemp,pNodeNew->pLeft); } } if (pNodeNew->pRight!=NULL) { if (pNodeNew->pRight->e==e) { pNodeTemp = pNodeNew->pRight; return TRUE; } else { InQueue(&QueueListTemp,pNodeNew->pRight); } } } } } return FALSE; } BOOL GetFather(pNode pNodeTemp,int e,pNode& pNodeFather) { pNode pNodeSon = NULL; QueueList QueueListTemp; QueueListTemp.InitMember(); if (FindNode(pNodeTemp,e,pNodeSon)==FALSE) { return FALSE; } if(pNodeSon==pNodeTemp) { return FALSE; } InQueue(&QueueListTemp,pNodeTemp); pNode pNodeNew = NULL; while (!IsQueueEmpty(&QueueListTemp)) { OutQueue(&QueueListTemp,pNodeNew); if (pNodeNew->pLeft==pNodeSon) { pNodeFather = pNodeNew; return TRUE; } else if (pNodeNew->pRight==pNodeSon) { pNodeFather = pNodeNew; return TRUE; } else { InQueue(&QueueListTemp,pNodeNew->pLeft); InQueue(&QueueListTemp,pNodeNew->pRight); } } DestroyQueue(&QueueListTemp); return FALSE; } BOOL GetLeftSon(pNode pNodeFather) { return TRUE; } BOOL GetRightSon() { return TRUE; } BOOL GetLeftBrother(pNode pNodeTemp) { return TRUE; } BOOL GetRightBrother(pNode pNodeTemp) { return TRUE; }
爱程序 不爱bug
爱生活 不爱黑眼圈
我和你们一样 我和你们不一样
我不是凡客 我要做geek