📜  双线程二进制搜索树

📅  最后修改于: 2021-04-17 11:10:15             🧑  作者: Mango

双线程二进制搜索树:是一种二进制搜索树,其中的节点不是每个左NULL指针都指向其有序前任,而右NULL指针则指向有序后继。
线程对于快速访问节点的祖先也很有用。
双线程二进制搜索树是在许多实时应用程序(例如最近插入和遍历搜索树所有元素的位置)中使用的高级数据结构类型之一。

双线程二进制搜索树的创建算法:

  • 在双线程二进制搜索树中,有五个字段,即数据字段,左,右指针,lbit和rbit,其中lbit和rbit是存储的布尔值,表示右指针指向有序后继者或新的子节点。类似地,lbit表示左指针指向有序的前任节点或新的子节点。
  • 创建双线程二进制搜索树的基本条件是根节点是否存在。如果不存在,则创建一个新节点并将其存储。
  • 否则,将当前节点的数据与要插入的新数据进行比较。如果新数据小于当前数据,则遍历到左子节点。否则,遍历到正确的子节点。
  • 如果不存在左子节点或右子节点,则将节点插入其左侧,并将其左子节点和右子节点分别指向有序的前任节点和后继节点。

下面是上述方法的实现:

C++
// C++ implementation of the double
// threaded binary searighth tree
 
#include 
using namespace std;
 
// Class of the Node
class Node {
    int lbit, rbit;
    int value;
    Node *left, *right;
 
public:
    // Constructor of the
    // Node of the Tree
    Node()
    {
        lbit = rbit = 0;
        value = 0;
        left = right = NULL;
    }
    friend class DTBT;
};
 
// Class of the Threaded
// Binary search tree
class DTBT {
    Node* root;
 
public:
    // Constructor of the
    // Threaded of the Binary
    // Search Tree
    DTBT()
    {
        root = new Node();
 
        // Initalise the dummy node
        // to any random value of
        // your choice.
        root->value = 9999;
 
        // Considering our whole
        // tree is at left of
        // dummy node
        root->rbit = 1;
        root->lbit = 0;
 
        // Consider your whole tree
        // lies to the left of
        // this dummy node.
        root->left = root;
        root->right = root;
    }
    void create();
    void insert(int value);
    void preorder();
    Node* preorderSuccessor(Node*);
    void inorder();
    Node* inorderSuccessor(Node*);
};
 
// Function to create the Binary
// search tree
void DTBT::create()
{
    int n = 9;
 
    // Insertion of the nodes
    this->insert(6);
    this->insert(3);
    this->insert(1);
    this->insert(5);
    this->insert(8);
    this->insert(7);
    this->insert(11);
    this->insert(9);
    this->insert(13);
}
 
// Function to insert the nodes
// into the threaded binary
// search tree
void DTBT::insert(int data)
{
    // Condition to check if there
    // is no node in the binary tree
    if (root->left == root
        && root->right == root) {
 
        Node* p = new Node();
        p->value = data;
        p->left = root->left;
        p->lbit = root->lbit;
        p->rbit = 0;
        p->right = root->right;
 
        // Inserting the node in the
        // left of the dummy node
        root->left = p;
        root->lbit = 1;
        return;
    }
 
    // New node
    Node* cur = new Node;
    cur = root->left;
    while (1) {
        // Condition to check if the
        // data to be inserted is
        // less than the current node
        if (cur->value < data) {
            Node* p = new Node();
            p->value = data;
            if (cur->rbit == 0) {
                p->right = cur->right;
                p->rbit = cur->rbit;
                p->lbit = 0;
                p->left = cur;
 
                // Inserting the node
                // in the right
                cur->rbit = 1;
                cur->right = p;
                return;
            }
            else
                cur = cur->right;
        }
 
        // Otherwise insert the node
        // in the left of current node
        if (cur->value > data) {
            Node* p = new Node();
            p->value = data;
            if (cur->lbit == 0) {
                p->left = cur->left;
                p->lbit = cur->lbit;
                p->rbit = 0;
 
                // Pointing the right child
                // to its inorder Successor
                p->right = cur;
                cur->lbit = 1;
                cur->left = p;
                return;
            }
            else
                cur = cur->left;
        }
    }
}
 
// In Threaded binary search tree
// the left pointer of every node
// points to its Inorder predecessor,
// whereas its right pointer points
// to the the Inorder Successor
void DTBT::preorder()
{
    Node* c = root->left;
 
    // Loop to traverse the tree in
    // the preorder fashion
    while (c != root) {
        cout << " " << c->value;
        c = preorderSuccessor(c);
    }
}
 
// Function to find the preorder
// Successor of the node
Node* DTBT::preorderSuccessor(Node* c)
{
    if (c->lbit == 1) {
        return c->left;
    }
    while (c->rbit == 0) {
        c = c->right;
    }
    return c->right;
}
 
// In Threaded binary search tree
// the left pointer of every node
// points to its Inorder predecessor
// whereas its right pointer points
// to the the Inorder Successor
void DTBT::inorder()
{
    Node* c;
    c = root->left;
    while (c->lbit == 1)
        c = c->left;
 
    // Loop to traverse the tree
    while (c != root) {
        cout << " " << c->value;
        c = inorderSuccessor(c);
    }
}
 
// Function to find the inorder
// successor of the node
Node* DTBT::inorderSuccessor(Node* c)
{
    if (c->rbit == 0)
        return c->right;
    else
        c = c->right;
    while (c->lbit == 1) {
        c = c->left;
    }
    return c;
}
 
// Driver Code
int main()
{
    DTBT t1;
 
    // Creation of the Threaded
    // Binary search tree
    t1.create();
 
    cout << "Inorder Travesal of DTBST\n";
    t1.inorder();
 
    cout << "\nPreorder Travesal of DTBST\n";
    t1.preorder();
    return 0;
}


输出:
Inorder Travesal of DTBST
 1 3 5 6 7 8 9 11 13
Preorder Travesal of DTBST
 6 3 1 5 8 7 11 9 13