线索二叉树的创建、中序遍历、左右插入

#include <iostream>
#include <stdio.h>

using namespace std;

struct binTreeNode
{
    char data;
    bool lthread,rthread;
    binTreeNode *lchild, *rchild;
};


//前序初始化:
void creatBinTreePre(binTreeNode *&T)
{
    char c;
    c = getchar();
    if('^' == c)
    T = NULL;
    else{
        T = new binTreeNode;
        T->data = c;
        creatBinTreePre(T->lchild);
        creatBinTreePre(T->rchild);
    }
}


//中序遍历创建:
void inOrderThreading(binTreeNode *&T, binTreeNode *&pre)
{
    if(T){
        inOrderThreading(T->lchild,pre);
        if(!T->lchild){
            T->lthread = true;
            T->lchild = pre;
        }
        if(!pre->rchild){
            pre->rthread = true;
            pre->rchild = T;
        }
        pre = T;
        inOrderThreading(T->rchild,pre);
    }
}

//线索树的根节点初始化
void inithread(binTreeNode *&T, binTreeNode *&root)
{
    binTreeNode *pre = new binTreeNode;
    if( T ){
        pre->lchild = T;
        pre->rchild = pre;
        root = pre;
        inOrderThreading(T, pre);
        pre->rthread = true;
        pre->rchild = root;
    }
    else{
        pre->lchild = pre;
        pre->rchild = pre;
    }

}


//寻找结点的中序后继:
binTreeNode *insucc(binTreeNode *tree)
{
    binTreeNode *temp;
    temp = tree->rchild;
    if(!tree->rthread)
        while(!temp->lthread)
            temp = temp->lchild;
    return temp;
}

//线索树的中序遍历:
void tinorder(binTreeNode *tree)
{
    binTreeNode *temp = tree;
    while(1){
        temp = insucc(temp);
        if(temp == tree)
            break;
        cout<<temp->data<<endl;
    }
}

//线索树的中序遍历寻找插入节点
binTreeNode *findParentNode(char c, binTreeNode *tree)
{
    binTreeNode *temp = tree;
    while(1){
        temp =insucc(temp);
        if(temp == tree)
            break;
        if(temp->data == c)
            return temp;
    }
}

//右插入
void insertRight(binTreeNode *parent, binTreeNode *child)
{
    binTreeNode *temp = NULL;
    child->rchild = parent->rchild;
    child->rthread = parent->rthread;
    child->lchild = parent;
    child->lthread = true;
    parent->rchild = child;
    parent->rthread = false;
    if(!child->rthread){
        temp = insucc(child);
        temp->lchild = child;
    }
}

//左插入
void insertLeft(binTreeNode *parent, binTreeNode *child)
{
    binTreeNode *temp = NULL;
    if(!parent->lthread){
     temp = parent->lchild;
     while(insucc(temp) != parent){
        temp = insucc(temp);
     }
     temp->rchild = child;
     temp->rthread = true;
    }
    child->lchild = parent->lchild;
    child->lthread = parent->lthread;
    child->rchild = parent;
    child->rthread = true;
    parent->lchild = child;
    parent->lthread = false;
}

 int main()
 {
    binTreeNode *T = NULL, *root = NULL, *parent = NULL, *child = NULL;
    char c;
    creatBinTreePre( T );
    inithread(T, root);

    cout<<"线索二叉树的中序遍历结果:\n";
    tinorder(root);

    cout<<"------------右插入---------\n";
    cout<<"请输入要插入的父母节点元素值:";
    cin>>c;
    parent = findParentNode(c, root);
    cout<<"请输入要插入的孩子节点元素值:";
    cin>>c;
    child = new binTreeNode;
    child->data = c;
    insertRight(parent, child);
    cout<<"线索二叉树右插入后的中序遍历结果:\n";
    tinorder(root);

    cout<<"------------左插入---------\n";
    cout<<"请输入要插入的父母节点元素值:";
    cin>>c;
    parent = findParentNode(c, root);
    cout<<"请输入要插入的孩子节点元素值:";
    cin>>c;
    child = new binTreeNode;
    child->data = c;
    insertLeft(parent, child);
    cout<<"线索二叉树左插入后的中序遍历结果:\n";
    tinorder(root);

    return 0;
}


你可能感兴趣的:(线索二叉树的创建、中序遍历、左右插入)