1
我找不到任何關於搜索的內容來滿足我的問題,如果它存在,我很抱歉!將二叉樹轉換爲雙線程二叉樹?
我正在進行關於線程二叉樹的高校作業。即各種各樣的遍歷 - 雙重技術性貿易壁壘中的訂單,後序和預訂。
這是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++怪胎絆倒在這裏,不建議我STL。分配規則禁止在該程序中使用STL。 – Nilesh 2012-02-09 09:05:51