線索二叉樹

2021-08-31 01:42:27 字數 2302 閱讀 6146

/*====知識總結

=*///1線索二叉樹就是比一般的二叉樹多了個線索,即指向結點前驅和後繼的指標

//2線索二叉樹基本結構分為ltag,lchild,data,rtag,rchild.即左標誌,左指標,結點資料,右指標,右標誌。

//3標誌位的取值決定指標的指向(為線索,指向結點前驅或後繼;或位正常指標,指向左右孩子結點),這個程 序的設定就是ltag1,指向左子樹(右同)。ltag0,指向結點的前驅或後繼。

package 線索二叉樹;

//線索二叉樹的宣告。

class threadnode

}//線索二叉樹的類宣告。

class threaded_binary_tree

//建構函式,用來建立線索二叉樹,傳入引數為乙個陣列。

public threaded_binary_tree(int data)

parent=current; //令parent指向current

if(current.left_thread==1) //current的左標誌為1;即left_node為正常指標,即current結點有左子結點。

current = current.left_node; //將current。left_node的引用賦給current,即將current的左子結點設定為當前結點。

else //當前結點沒有左子結點,即當前結點為葉子結點。

current = null; //令current為0;結束本次迴圈,執行迴圈體下面的語句。

}else

parent = current; //令parent指向current

if(current.right_thread==1) //current的左標誌為1;即left_node為正常指標,即current結點有左子結點。

current = current.right_node; //將current。left_node的引用賦給current,即將current的左子結點設定為當前結點。

else

current = null; //令current為0;結束本次迴圈。} }

//將結點加入二叉樹。

if(parent.value>value)

else

return; //結束該方法,執行其他語句。

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

void print()

else

}if(tempnode!=rootnode) //如果臨時結點不等於開頭節點。輸出臨時結點的值

system.out.println("["+tempnode.value+"]");

}while(tempnode!=rootnode); //條件成立迴圈,(最後乙個結點的後繼會指向rootnode結點。) }}

public class demothreadtree ;

threaded_binary_tree tree1 = new threaded_binary_tree(data1);

system.out.println("***********************************==");

system.out.println("範例1");

system.out.println("數字由小到大的排列順序結果為: ");

tree1.print();

intdata2 = ;

threaded_binary_tree tree2 = new threaded_binary_tree(data2);

system.out.println("***********************************==");

system.out.println("範例2");

system.out.println("數字由小到大的排列順序結果為: ");

tree2.print();

}}

/程式總結

=/1.這個程式比較複雜,廢了很大的功夫,才讀懂整個程式,有很多地方設定的比較複雜,很冗餘,完全可以刪除一些語句,簡化程式。

2.例如那個建立線索二叉樹,用了乙個迴圈既然只是為了遍歷葉子結點,後面還用了乙個迴圈來新增新結點,這個完全可以合併的。自己能力有限只能看看了。

3.自己通過這個程式,學會了如何debug。下次遇到複雜的程式,自己先仔細讀懂,慢一點沒關係不著急,如果讀不懂,不知道程式如何執行的,就運用debug。

4.這個程式超出自己的知識高度,理解思想和讀懂程式就好。

線索二叉樹

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