//////////////////////////////////////////////////////////////////////////////////////////////////// //二.中序线索二叉树 //二.1 中序线索化 //按"左根右"的顺寻进行 void mid_thread(Bin_tree tree) //对二叉树进行中序线索化 { if(tree != NULL) { mid_thread(tree->left_child); //先对tree的左子树线索化 //到此,tree节点的左子树不存在或已线索化,接下来对tree线索化 if(tree->left_child == NULL) //tree的左孩子不存在则进行前驱线索 { tree->left_child = pre; //建立当前节点tree的前驱线索 tree->left_flag = THREAD; } else { tree->left_flag = LINK; //置tree的left_child指针为指向左孩子标志 } if(pre->right_child == NULL) //pre的右孩子不存在则进行后继线索 { pre->right_child = tree; //建立节点pre的后继线索 pre->right_flag = THREAD; } else { pre->right_flag = LINK; //置p的right_child指针为指向右孩子标志 } pre = tree; //pre移至tree节点 mid_thread(tree->right_child); //对tree的右子树线索化 } }
void mid_thread(Node *root){ if (root != NULL){ mid_thread(root->left->child); if (root->left->child == NULL){ root->left->tag = LINK; root->left->child = ptr; //global val } if (ptr != NULL && ptr->right->child == NULL){ ptr->right->tag = LINK; ptr->right->child = root; } ptr = root; mid_thread(root->right->child); } }
//二.2 创建中序线索二叉树 //创建一个root根节点,对tree进行中序线索化 Bin_tree create_mid_thread(Bin_tree tree) { Bin_tree root = NULL; root = create_node(); //创建头节点 root->left_flag = LINK; root->right_flag = THREAD; if(tree == NULL) //二叉树为空 { root->left_child = root; } else { root->left_child = tree; //root的left_child指针指向二叉树根节点tree pre = root; mid_thread(tree); //对二叉树tree进行中序线索化 pre->right_child = root; //最后处理,加入指向头节点的线索 pre->right_flag = THREAD; root->right_child = pre; //头节点的right_child指针线索化为指向最后一个节点 } return root; //返回线索化后指向二叉树的头节点的指针root }