void last_thread(Node *root){ if (root != NULL){ last_thread(root->left->child); last_thread(root->right->child); if (root->left->child == NULL){ root->left->child = last; root->left->tag = LINK; } if (last != NULL && last->right->child == NULL){ last->right->child = root; last->right->tag = LINK; } last = root; } }
//三.后序线索二叉树 //三.1 后序线索化 //按照"左右根"的顺序进行 void last_thread(Bin_tree tree) { if(tree != NULL) { last_thread(tree->left_child); last_thread(tree->right_child); if(tree->left_child == NULL) { tree->left_child = pre; tree->left_flag = THREAD; } if(pre->right_child == NULL) { pre->right_child = tree; pre->right_flag = THREAD; } pre = tree; } }
int main(){ ThreadTree root; const char *str = "ABDHM###I##EG###CFK###GL###"; //const char *str = "ABC###DE##F##"; root = create_thread_tree(&str); last_trav(root); last_thread(root); printf("\n"); last_thread_trav(root); return 0; }
//三.2 创建后序线索二叉树 //创建新节点root指向原来的tree Bin_tree create_last_thread(Bin_tree tree) { Bin_tree root = NULL; root = create_node(); //新节点root root->right_flag = LINK; root->right_child = root; if(tree == NULL) //无二叉树时 { root->left_child = root; } else { pre = root; root->left_child = tree; //root指向tree root->left_flag = LINK; last_thread(tree); //后序线索化tree root->right_child = pre; } return root; }