【问题标题】:Converting a Binary Tree to Double Threaded Binary Tree?将二叉树转换为双线程二叉树?
【发布时间】:2012-02-28 05:09:51
【问题描述】:

我在搜索中找不到任何内容来满足我的问题,如果存在,我很抱歉!

我正在完成一项关于线程二叉树的大学作业。 IE。各种遍历 - 双 TBT 上的中序、后序和预序。

这是 TBTNode 结构:

struct TBTNode {
  TBTNode *left, *right, *parent;       
  char data;
  bool left_normal, right_normal;
  TBTNode(char d) {
    data = d;
    left = NULL;
    right = NULL;
    parent = NULL;
    left_normal = true;
    right_normal = true;
  }
};

如您所见,二叉树节点和 TBT 节点之间没有太大区别,除了节点的属性,即。 {left,right}_normal 在需要时设置为 true。

要创建树,我有这个:

class TBT {
  TBTNode *root;
public:
  TBT() {
    root = new TBTNode(0);
    root->right = root;
    root->right_normal = true;          
    cout << "Root:" ;           
    root->left = create();
    if(root->left)
      root->left_normal = true;
  }
  TBTNode* create();  
};

TBTNode* TBT::create() {
  char data;
  TBTNode *node = NULL;
  cout << endl << "Enter data (0 to quit): ";
  cin >> data;
  if(data == '0')
    return NULL;
  node = new TBTNode(data);
  cout << endl << "Enter left child of " << data;
  node->left = create();
  if(node->left)
    node->left->parent = node;
  else {
    node->left = root;
    node->right = node->parent;
    node->left_normal = node->right_normal = false;
  }
  cout << endl << "Enter right child of " << data;
  node->right = create();
  if(node->right)
    node->right->parent = node;
  else {
    node->left = node;
    node->right = node->parent->parent;
    node->left_normal = node->right_normal = false;
  }
  return node;
}

使用上述代码递归创建树后,我想将其转换为双线程二叉树。我知道左孩子与孩子的有序前任和右至有序继任者相关联的概念,但我无法创建算法。有人可以帮我吗?

【问题讨论】:

    标签: c++ binary-tree


    【解决方案1】:

    我自己找到了解决方案。首先按顺序遍历树并在继续时将节点添加到数组中。然后处理数组以链接线程,因为对于数组中的给定元素 x,x 之前的元素将是有序的前驱元素,而 x 之后的元素将是有序的后继元素。对于第一个和最后一个元素,会进行特殊检查以将它们链接到头节点(而不是根节点)。

    不需要父链接,它已被删除。

    代码如下:

    class TBT {
      TBTNode *root;
      void createInorderArray(TBTNode *T);
      TBTNode **array;
      unsigned array_size;
    public:
      TBT();
      TBTNode* create();
      void inorder();
      void preorder();
    };
    
    TBT::TBT() {
      root = new TBTNode(0);
      root->right = root;
      root->right_normal = true;
      cout << "Root:" ;
      root->left = create();    
      if(!root->left) {
        root->left_normal = false;
        root->left = root;
      }
      array = NULL;
      array_size = 0;
      createInorderArray(root->left);
      for(unsigned i = 0; i < array_size; i++) {
        if(!array[i]->left) {
          array[i]->left = i == 0 ? root : array[i-1];
          array[i]->left_normal = false;
        }
        if(!array[i]->right) {
          array[i]->right_normal = false;
          array[i]->right = i == (array_size - 1) ? root : array[i+1];
        }
      }
      free(array);
      array_size = 0;
    }
    
    void TBT::createInorderArray(TBTNode *T) {
      if(!T)
        return;
      createInorderArray(T->left);
      array = (TBTNode**) realloc(array, sizeof(TBTNode**) * ++array_size);
      array[array_size-1] = T;
      createInorderArray(T->right);  
    }
    

    【讨论】:

    • 如果有些 C++ 极客在这里绊倒了,不要建议我使用 STL。赋值规则禁止在此程序中使用 STL。
    猜你喜欢
    • 1970-01-01
    • 2012-01-09
    • 2020-08-24
    • 1970-01-01
    • 2020-03-11
    • 2021-06-07
    • 2011-07-24
    • 1970-01-01
    • 2015-09-24
    相关资源
    最近更新 更多