> For the complete documentation index, see [llms.txt](https://garychang.gitbook.io/data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garychang.gitbook.io/data-structure/lecture2-tree-and-binary-tree/lecture2.6-thread-binary-tree.md).

# 2.6 - Thread Binary Tree

### &#x20;1. 介紹

n個節點的二元樹若以Link List表示則會有n+1個指標指向`NULL`，為了**充分利用**這些指標，將他們改指向其他節點，則稱為**引線二元樹**。其中這些指標稱為Thread Pointers。

* 左引線:若 `x->lchild = NULL`則改指向中序排序中的前一個節點。
* 右引線:若 `x->rchild = NULL`則改指向中序排序中的後一個節點。

節點儲存結構:

\[Left Thread] \[Lchild] \[Data] \[Rchild] \[Right Thread]

* Left Thread: `True`表示Lchild是左引線；`False`表示Lchild指向左子點
* Right Thread: `True`表示Rchild是右引線；`False`表示Rchild指向左子點

另外增加一個**Head**不存Data。

圖示：&#x20;

![](/files/-LHhBW6TOTeNcnrtZ2HQ)

### 2. 操作

#### 2.1 找出中序排序中x的下一個節點

```
node *Insuc(node *x){
    node *temp = x->rchild;
    if (x->RightThread == False){ //若x->RightThread為True, temp即找到
        while(temp->LeftThread == False)
            temp = temp->lchild;
    }
    return temp;
}
```

#### 2.2 在引線二元樹中插入一個節點(insert a right child)

```
void InsertRightChild(node *s, node *t){  //s為樹中節點, t為新節點
    t->RightThread = s->RightThread;      //設置t的右子點、右引線
    t->rchild = s->rchild;
    t->LeftThread = True;                 //設置t的左子點、左引線
    t-lchild = s;
    s->RightThread = False;               //將s連到t
    s->rchild = t;
    if (s->RightThread == False){         //若s無右子點
        node *temp = Insuc(s);            //中序排序中s的下一個節點
        temp->lchild = t;
    }
}
```

#### 2.3 在引線二元樹中插入一個節點(insert a left child)

```
void InsertLeftChild(node *s, node *t){  //s為樹中節點, t為新節點
    t->LeftThread = s->LeftThread;       //設置t的左子點、左引線
    t->lchild = s->lchild;
    t->RightThread = True;               //設置t的右子點、右引線
    t-rchild = s;
    s->LeftThread = False;               //將s連到t
    s->lchild = t;
    if (s->LeftThread == False){         //若s無左子點
        node *temp = Pre(s);             //中序排序中s的上一個節點
        temp->rchild = t;
    }
}

node *Pre(node *x){
    node *temp = x->lchild;
    if (x->LeftThread == False){ //若x->RightThread為True, temp即找到
        while(temp->RightThread == False)
            temp = temp->rchild;
    }
    return temp;
}
```

### 3. 應用

簡化中序追蹤的演算法

```
void Inorder(node *head){
    node *hd = head;
    node *temp;
    while(temp == hd){
        temp = Insuc(head);
        head = temp;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://garychang.gitbook.io/data-structure/lecture2-tree-and-binary-tree/lecture2.6-thread-binary-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
