線索二叉樹

2021-10-01 04:37:08 字數 1132 閱讀 4417

線索二叉樹

線索鍊錶

線索:將二叉鍊錶中的空指標域指向前驅結點和後繼結點的指標被稱為線索;

線索化:使二叉鍊錶中結點的空鏈域存放其前驅或後繼資訊的過程稱為線索化;

線索二叉樹:加上線索的二叉樹稱為線索二叉樹。

結點結構:ltag data child rtag

ltag=

rtag=

enum flag ; 

template struct thrnode

;template class inthrbitree;

template thrnode* inthrbitree::creat( )

return root;

}

中序線索化二叉樹:遞迴實現

基本思想:

在遍歷的過程中完成線索化

可以採用前序、中序、後序遍歷建立前序線索二叉樹、中序線索二叉樹和後序線索二叉樹。

中序線索二叉樹的構造方法

中序線索化根結點的左子樹;

對根結點進行線索化;

中序線索化根結點的右子樹;

template void thrbitree::thrbitree (thrnode*root) 

if (!root->rchild) root->rtag = thread;

if(pre != null)

pre = root;

thrbitree(root->rchild);

}

線索二叉樹的建立

template inthrbitree::inthrbitree( )

在中序線索樹中查詢結點的中序遍歷的後繼

template thrnode* inthrbitree::next(thrnode* p)

}return q;

}

線索鍊錶的遍歷演算法:中序遍歷中序線索樹

template void inthrbitree::inorder(thrnode*root)

coutwhile (p->rchild!=null)

cout<}

線索二叉樹

當用二叉鍊錶作為二叉樹的儲存結構時,因為每個結點中只有指向其左 右兒子結點的指標,所以從任一結點出發只能直接找到該結點的左 右兒子。在一般情況下靠它無法直接找到該結點在某種遍歷序下的前驅和後繼結點。如果在每個結點中增加指向其前驅和後繼結點的指標,將降低儲存空間的效率。我們可以證明 在n個結點的二叉鍊...

線索二叉樹

1.線索二叉樹結構和操作定義 threadbintree.h 功能 線索標誌域所有值 typedef enumnodeflag 功能 線索二叉樹結構體 typedef struct threadtreethreadbintree 前驅節點指標 threadbintree previous null ...

線索二叉樹

原始碼 中序線索二叉樹 author 菜鳥 version 2014.7.23 include include include typedef char datatype using namespace std 定義線索二叉樹的結構體 typedef struct nodethreadbitreen...