線索化二叉樹

2021-07-29 22:20:46 字數 2094 閱讀 3613

#includeusing namespace std;

#includeenum pointertag

;template struct binarytreenode_thd

};templateclass binarytree_thd

binarytree_thd(const t* a,size_t size)

//前序線索化二叉樹

void prevorder_thread()

//中序線索化二叉樹

void inorder_thread()

//後序線索化二叉樹

void postorder_thread()

//前序遍歷線索化二叉樹

void prev_thd()

binarytreenode_thd* cur = _root;

while (cur)

cout << cur->_data << " ";

cur = cur->_right;

/* while(cur && cur->_righttag == thread)

if(cur->_lefttag == link)

cur = cur->_left;

else

cur = cur->_right;*/

} cout << endl;

} //中序遍歷線索化二叉樹

void inorder_thd()

binarytreenode_thd* cur = _root;

while (cur)

cout << cur->_data << " ";

while (cur->_righttag == thread)

cur = cur->_right;

} cout << endl;

} //後序遍歷線索化二叉樹

void postorder_thd()

stack*> s;

binarytreenode_thd* cur = _root;

while (cur)

s.push(cur);

cur = cur->_left;

} while (!s.empty())

cout << endl;

}protected:

binarytreenode_thd* _createtree(const t* a, size_t size, size_t& index)

}return root;

} void _prevorder_thread(binarytreenode_thd* cur, binarytreenode_thd*& prev)

if (cur->_left == null)

if (prev && prev->_right == null)

prev = cur;

if (cur->_lefttag == link)

if (cur->_righttag == link)

}void _inorder_thread(binarytreenode_thd* cur, binarytreenode_thd*& prev)

_inorder_thread(cur->_left, prev);

if (cur->_left == null)

if (prev && prev->_right == null)

prev = cur;

_inorder_thread(cur->_right, prev);

} void _postorder_thread(binarytreenode_thd* cur, binarytreenode_thd*& prev)

_postorder_thread(cur->_left, prev);

_postorder_thread(cur->_right, prev);

if (cur->_left == null)

if (prev && prev->_right == null)

prev = cur;

}private:

binarytreenode_thd* _root;

};

線索化二叉樹以及遍歷線索化二叉樹

1.線索二叉樹基本介紹 n個結點的二叉鍊錶中含有n 1 公式 2n n 1 n 1 個空指標域。利用二叉鍊錶中的空指標域,存放指向該結點在某種遍歷次序下的前驅和後繼結點的指標 這種附加的指標稱為 線索 這種加上了線索的二叉鍊錶稱為線索鍊錶,相應的二叉樹稱為線索二叉樹 threaded binaryt...

線索化二叉樹

define crt secure no warnings 1 includeusing namespace std enum pointertag 列舉 其結構如下 void prevorderthreading 前序 void postorderthreading 後序 void inorder...

線索化二叉樹

二叉樹是一種非線性結構,遍歷二叉樹幾乎都是通過遞迴或者用棧輔助實現非遞迴的遍歷。用二叉樹作為儲存結構時,取到乙個節點,只能獲取節點的左孩子和右孩子,不能直接得到節點的任一遍歷序列的前驅或者後繼。為了儲存這種在遍歷中需要的資訊,我們利用二叉樹中指向左右子樹的空指標來存放節點的前驅和後繼資訊.二叉樹的結...