// TreeNode建構子 public ThreadNode(int value) { this.value = value; this.left_Thread = 0; this.right_Thread = 0; this.left_Node = null; this.right_Node = null; }
//將指定的值加入到二元引線樹 void Add_Node_To_Tree(int value) { ThreadNode newnode = new ThreadNode(value); ThreadNode current; ThreadNode parent; ThreadNode previous = new ThreadNode(value); int pos; //設定引線二元樹的開頭節點 if (rootNode == null) { rootNode = newnode; rootNode.left_Node = rootNode; rootNode.right_Node = null; rootNode.left_Thread = 0; rootNode.right_Thread = 1; return; } //設定開頭節點所指的節點 current = rootNode.right_Node; if (current == null) { rootNode.right_Node = newnode; newnode.left_Node = rootNode; newnode.right_Node = rootNode; return; } parent = rootNode; //父節點是開頭節點 pos = 0; //設定二元樹中的行進方向 while (current != null) { if (current.value > value) { if (pos != -1) { pos = -1; previous = parent; } parent = current; if (current.left_Thread == 1) { current = current.left_Node; } else { current = null; } } else { if (pos != 1) { pos = 1; previous = parent; } parent = current; if (current.right_Thread == 1) { current = current.right_Node; } else { current = null; } } } if (parent.value > value) { parent.left_Thread = 1; parent.left_Node = newnode; newnode.left_Node = previous; newnode.right_Node = parent; } else { parent.right_Thread = 1; parent.right_Node = newnode; newnode.left_Node = parent; newnode.right_Node = previous; } return; }
public ThreadNode rootNode; //引線二元樹的根節點 //無傳入參數的建構子 public Threaded_Binary_Tree() { rootNode = null; }