zoukankan      html  css  js  c++  java
  • 二叉树的线索化

    最近学了线索二叉树 自己也写了一个中序线索化 二叉树  但出现了错误  无法实现 希望高手指点一下 发现哪错了 谢谢

     #include<iostream.h>

    struct Node{//二叉树结点
    int data;
    Node*left;
    Node*right;
    int ltag;//线索化标志
    int rtag;//线索化标志
    };
    class binarytree{
    protected:
    Node*root;
    void preonder(Node*T);//前序遍历
    public:
        binarytree(){root=NULL;}
    void creattree(Node*&T);//创建数的函数
    void show1();//前序遍历函数
    void Inthreading(Node*&T,Node*&pre);//线索化二叉树
    void INthreadingtree();// 主调函数 调用 前一个函数Inthreading
    Node*& getroot();//得到根节点
    };
    void binarytree::creattree(Node*&T){
    int x;
    cin>>x;
    if(x==0) T=NULL;
    else{
        T=new Node;
        T->data=x;
    T->ltag=0;
    T->rtag=0;
             creattree(T->left);
             creattree(T->right);
    }
    }
    void binarytree::preonder(Node*T){
    if(T){
    cout<<T->data;
            preonder(T->left);
            preonder(T->right);
    }
    }
    void binarytree::show1(){
    preonder(root);
    }
    Node*& binarytree::getroot(){
    return root;
    }
    void binarytree::Inthreading(Node*&T,Node*&pre){
       if(T==NULL) return ;
    Inthreading(T->left,pre);//pre 为标记 p遍历的前驱
    if(!T->left){
    T->left=pre;
    T->ltag=1;
    }
    if(!pre->right&&pre){ 
    pre->rtag=1;
        pre->right=T;
    }
      pre=T;//修改pre 使他指向前一结点
             Inthreading(T->right,pre);
    }
    void binarytree::INthreadingtree(){
    Node*pre=NULL;
         Inthreading(root,pre);
    }
     
     
    void main(){
    binarytree b;
    b.creattree(b.getroot());
    b.INthreadingtree();//这个线索化函数导致 程序崩溃 导致其他功能无实现
    b.show1();
    }
    ----转载请注明出处http://www.cnblogs.com/JerryWang1991/ 谢谢!
  • 相关阅读:
    eCharts_数据过多底部滚动条实现数据展示
    canvas_简单练习
    canvas_基于canvan绘制的双半圆环进度条
    js_读【javascript面向对象编程指南】笔记
    csc_滤镜filter和实现透明的两种方式
    jq_从右向右的滑入滑出效果
    textarea输入框实时统计输入字符数
    windows7_常用操作终端操作
    1080. Graduate Admission (30)
    1079. Total Sales of Supply Chain (25)
  • 原文地址:https://www.cnblogs.com/JerryWang1991/p/3936401.html
Copyright © 2011-2022 走看看