【发布时间】: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