二叉樹的建立 線索化以及遍歷

2021-09-11 20:11:08 字數 1140 閱讀 7710

#include

#define max 20

using namespace std;

typedef struct nodetbtnode;   //節點型別

tbtnode * pre;

void thread(tbtnode * &p)

else

if(pre->rchild==null)

else

pre=p;

thread(p->rchild);

}}

tbtnode * createthread(tbtnode * b)

else

return root;

}

void * createbt(tbtnode * &root,char * str)

else                    }}

j++;

ch=str[j];

}}

void thinorder(tbtnode * tb)

p=p->rchild;  //rtag=0的情況,即有右孩子

}}

int main(){

tbtnode * root,* tbtree;

char * str=new char[max];

root=null;

cout<<"input string to build tree : "<>str;

createbt(root,str);  //建立二叉樹 

tbtree=createthread(root);   //建立線索二叉樹 

cout<<"use inorder output tree : "<

//輸入格式,例如:a(b,c(d,e))

/*

左括號前表示雙親節點,左括號後表示孩子節點

用逗號分隔左右孩子

*/

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

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

線索化二叉樹的建立以及遍歷

定義樹 class threadedbinarytree 構建中序線索化二叉樹 param node 需要構建的節點 public void threadedtree hero node 向左遞迴 threadedtree node.getleft 處理前驅節點 if node.getleft nu...

遍歷線索化二叉樹

常規的線索化方式 採用遞迴地呼叫的方式,判定條件是當前指標的左子樹是否為空 實現 public void midorder system.out.println this if this right null 對比 但是對二叉樹進行線索化之後,不存在空的左右指標,但是單獨設定每乙個指標的型別,故而條...