您好,登錄后才能下訂單哦!
前序線索化:
void _PrevOrderThreading(Node *cur, Node *& prev) { if(cur == NULL) { return; } if(cur->_left == NULL) { cur->_leftTag == THREAD; cur->_left = prev; } if(prev && prev->_right == NULL) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; _PrevOrderThreading(cur->_left, prev); _PrevOrderThreading(cur->right, prev); }
前序遍歷:
void PrevOrderThd() { Node *cur = _root; while(cur) { while(cur->_leftTag == LINK) { cout << cur->_data << " "; cur = cur->_left; } cout << cur->_data << " "; cur = cur->right; // // while(cur->_rightTag == THREAD) // { // cur = cur->_right; // cout << cur->_data << " "; // } // if(cur->_leftTag == LINK) // { // cur = cur->_left; // } // else // { // cur = cur->_right; // } } }
中序線索化:
void _InOrderThreading(Node *cur, Node *& prev) { if(cur == NULL) { return; } _InOrderThreading(cur->_left, prev); //線索化 if(cur->_left == NULL) { cur->_leftTag = THREAD; cur->_left = prev; } if(prev && prev->_right == NULL) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; _InOrderThreading(cur->_right, prev); }
中序遍歷:
void InOrderThd() { Node *cur = _root; while(cur) { //找最左節點 while(cur->_leftTag == LINK) { cur = cur->_left; } cout << cur->_data << " "; //訪問連續后繼 while(cur->_rightTag == THREAD) { cur = cur->_right; cout << cur->_data << " "; } cur = cur->_right; } }
后序線索化:
void _PostOrderThreading(Node *root, Node *&prev) { if (root == NULL) { return; } _PostOrderThreading(root->_LChild, prev); _PostOrderThreading(root->_RChild, prev); if (root->_LChild == NULL) { root->_LTag = THREAD; root->_LChild = prev; } if (prev->_RChild == NULL) { prev->_RTag = THREAD; prev->_RChild = root; } prev = root; }
后序遍歷:
采用三叉鏈 或寫一個查找根節點的方法
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。