| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4782 人关注过本帖, 2 人收藏
标题:二叉树的输入和前序遍历
只看楼主 加入收藏
草狼
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:577
专家分:1040
注 册:2010-4-6
结帖率:94.44%
收藏(2)
已结贴  问题点数:20 回复次数:10 
二叉树的输入和前序遍历
为什么这代码输入后会自动关闭啊??

#include<stdio.h>
#include<stdlib.h>
struct jj
{
    char a;
    struct jj *l;
    struct jj *r;
};
void input(struct jj *p)//输入二叉树
{
    char c;
    if((c=getchar())==' ')
        p=NULL;
    else
    {
        p=(struct jj *)malloc(sizeof(struct jj));
        p->a=c;
        input(p->l);
        input(p->r);
    }
   
}

void output(struct jj *p)//前序历遍二叉树
{
      if(p)
      {
        printf("%c ",p->a);
        if(p->l!=NULL) output(p->l);
        if(p->r!=NULL) output(p->r);

      }

}

int main()
{
    struct jj *p;
    input(p);
    output(p);
}
搜索更多相关主题的帖子: 二叉树 遍历 输入 
2010-05-14 01:04
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
程序能跑起来?
你的 struct jj *p;没有初始化
要传的话 你可以直接传地址 &p 然后用**p接收
比如
void A(int **p);
int main()
{int *p;
A(&p);
}
if((c=getchar())==' ')
getchar只从缓冲区接收一个字符 但是并不清空缓冲区

如果你连续的输入字符 那么getchar只会拿走最前面的一个赋给c 第二次又继续取

比如你连续的输入ab 回车
那么第一次getchar()将a 赋给变量c 第二次不用你输入就会将b赋给c
第三次也不用你输入将\n赋给c
你可以用这个小程序验证下
int main()
{
    char c;
    while(1){
        c = getchar();
        printf("%d\n",int(c));
        printf("____\n");}
    return 0;
}
2010-05-14 08:16
草狼
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:577
专家分:1040
注 册:2010-4-6
收藏
得分:0 
回复 2楼 hahayezhe
getchar()的用法我知道啊,我用的是递归  所以每次就输入一个字符没错的啊  **P我就不理解了  我用的是一维的 为什么要取两次地址啊  表示不明白
2010-05-14 09:57
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:5 
因为你的p指针指向不确定 即为野指针
你将一个指针指向的不确定的地址传给形参指针
形参指针又怎么去操作这个地址呢
就相当于这个操作
int *p,*q;
p=q;
*p=5;//我这么写是合理 因为按照你的理解p已经初始化了 它的指向是确定的
printf("%d",q);明显报错 当然你这里说5是个常量
那么改下
int *p.*q;
p=q;
int a=5;
*p=a;
printf("%d",q);你觉得跟上面有区别么 换成p=&a也一样
p=q 你用一个没有初始化的指针去初始化另外一个指针......??
所以如果你不想初始化指针的指向
那么你就只能将这个指针本身的地址传给形参
让形参帮你决定我这个实参指针应该指向那里
比如 int **p,*q;
p=&q; 注意p是一个指向int*的指针 那么这里初始化p就对了
p里面存储的是q的地址,然后再对*p进行操作
*p=&a 就相当于给q的内存赋值了q里面存储的是a的地址
那么q的指向就确定了
2010-05-14 10:51
草狼
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:577
专家分:1040
注 册:2010-4-6
收藏
得分:0 
回复 4楼 hahayezhe
那您能用C写个 输入2叉树 前序遍历二叉树的代码给我研究下吗? 可以不 谢谢啦
2010-05-14 12:02
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:5 
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100

typedef struct NodeTree{
    char _data;
    struct NodeTree *m_pleftTree;
    struct NodeTree *m_prightTree;
}*Node;
void IniValue(Node *p){
    *p = NULL;
}
void CreateTree(Node *p,char *a){
    bool bk;
    Node pp;
    Node s[MAX_SIZE];
    int i = 0;
    *p = NULL;
    int top = -1;
    while(a[i]!='\0'){
        switch(a[i]){
            case ' ':
                break;
            case '(':
                if((MAX_SIZE - 1) == top){
                    printf("栈空间不够\n");
                    exit(1);
                }
                top++;
                s[top] = pp;
                bk = true;
                break;
            case ')':
                if(-1 == top){
                    printf("你的输入有误\n");
                    exit(1);
                }
                top--;
                break;
            case ',':
                bk = false;
                break;
            default:
                pp = (Node) malloc(sizeof(struct NodeTree));
                pp->_data = a[i];
                pp->m_pleftTree = NULL;
                pp->m_prightTree = NULL;
                if(*p == NULL){
                    *p = pp;
                }
                else{
                    if(bk)
                        s[top]->m_pleftTree = pp;
                    else
                        s[top]->m_prightTree = pp;
                }
        }
        i++;
    }
}
bool EmptyTree(Node p){
    if(p == NULL)
        return 1;
    else
        return 0;
}
void OutPutTree(Node p){
    if(p != NULL){
        printf("%c",p->_data);
        if(p->m_pleftTree != NULL || p->m_prightTree != NULL){
            printf("(");
            OutPutTree(p->m_pleftTree);
            if(p->m_prightTree != NULL){
                printf(",");
            }
            OutPutTree(p->m_prightTree);
            printf(")");
        }

    }
}
void ClearTree(Node *p){
    if(*p != NULL){
        ClearTree(&((*p)->m_pleftTree));
        ClearTree(&((*p)->m_prightTree));
        free(*p);
        *p = NULL;
    }
}
void FrontOrderTree(Node p){
    if(p != NULL){
        printf("%c",p->_data);
        FrontOrderTree(p->m_pleftTree);
        FrontOrderTree(p->m_prightTree);
    }
}
void MiddleOrderTree(Node p){
    if(p != NULL){
        MiddleOrderTree(p->m_pleftTree);
        printf("%c",p->_data);
        MiddleOrderTree(p->m_prightTree);
    }
}
void AfterOrderTree(Node p){
    if(p != NULL){
        AfterOrderTree(p->m_pleftTree);
        AfterOrderTree(p->m_prightTree);
        printf("%c",p->_data);
    }
}
int main(){
    Node bt; /* 指向二叉树根结点的指针 */
    char *b;    /* 用于存入二叉树广义表的字符串 */
    char x, *px;
    IniValue(&bt);
    printf("输入二叉树广义表的字符串:\n");
    /* scanf("%s", b); */
    b = "a(b(c), d(e(f, g), h(, i)))";
    CreateTree(&bt, b);
    if(bt != NULL)
        printf(" %c ", bt->_data);
    printf("以广义表的形式输出:\n");
    OutPutTree(bt);   /* 以广义表的形式输出二叉树 */
    printf("\n前序:");  /* 前序遍历 */
    FrontOrderTree(bt);
    printf("\n中序:");  /* 中序遍历 */
    MiddleOrderTree(bt);
    printf("\n后序:");  /* 后序遍历 */
    AfterOrderTree(bt);
return 0:
}

这是广义表
2010-05-14 12:10
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
#ifndef _ELLEN_H_
#define _ELLEN_H_
template<class T>class BinaryTree;
template<class T>
class BinaryTreeNode{
    friend class BinaryTree<T>;
private:
    BinaryTreeNode<T>* m_Left;
    BinaryTreeNode<T>* m_right;
    T element; //二叉树结点数据域
public:
    BinaryTreeNode():element(0),m_Left(0),m_right(0){};   //缺省构造函数
    BinaryTreeNode(const T&ele):element(ele),m_Left(0),m_right(0){};  //给定数据的构造函数
    //给定了节点值和左右结点
    BinaryTreeNode(const T&ele,BinaryTreeNode<T>*l,BinaryTreeNode<T>*r):
    element(ele),m_Left(l),m_right(r){};
        
    T value()const; //返回当前结点数据
    BinaryTreeNode<T> * leftchild() const;//返回左子树
    BinaryTreeNode<T> * rightchild() const;//返回右子树
    void setLeftchild(BinaryTreeNode<T>*);//设置当前结点的左子树
    void setRightchild(BinaryTreeNode<T>*);//设置当前结点的右子树
    void setValue(const T&val);//设置当前结点的数据域
    bool isLeaf()const;//判定当前结点是否为叶结点
    //重载赋值操作符
    BinaryTreeNode<T>&operator=(const BinaryTreeNode<T>&Node);
};
template<class T>//返回数据域
T BinaryTreeNode<T>::value()const
{
    return element;
}
template<class T>//返回左结点
BinaryTreeNode<T>* BinaryTreeNode<T>::leftchild() const
{
    return m_Left;
}
template<class T>//返回左结点
BinaryTreeNode<T>* BinaryTreeNode<T>::rightchild() const
{
    return m_right;
}
template<class T>//设置左节点
void BinaryTreeNode<T>::setLeftchild(BinaryTreeNode<T>*l)
{
    m_Left = l;
}
template<class T>//设置右结点
void BinaryTreeNode<T>::setRightchild(BinaryTreeNode<T>*r)
{
    m_right = r;
}
template<class T>//设置数据域
void BinaryTreeNode<T>::setValue(const T &val)
{
    element = val;
}
template<class T>//判断结点是否为树叶
bool BinaryTreeNode<T>::isLeaf()const
{
    if (m_Left==NULL&&m_right==NULL)
        return 0;
    else
        return 1;
}
template<class T>//赋值重载
BinaryTreeNode<T>&BinaryTreeNode<T>::operator=(const BinaryTreeNode<T>&Node)
{
    if (this == &Node)
        return *this;
    delete this;
    element = Node.element;
    m_Left = Node.m_Left;
    m_right = Node.m_right;
    return *this;   
}

template<class T>
class BinaryTree{
private:
    BinaryTreeNode<T> *root;//二叉树根结点
    //从二叉树的root结点开始,查找current结点的父结点
    BinaryTreeNode<T>*GetParent(BinaryTreeNode<T> *root,BinaryTreeNode<T> *current);
public:
    BinaryTree():root(0){};//构造函数
    ~BinaryTree(){DeleteBinaryTree(root);};//析构函数

    bool isEmpty()const;//判断二叉树是否为空
    BinaryTreeNode<T> *Root(){return root;};//返回根结点

    BinaryTreeNode<T> *Parent(BinaryTreeNode<T>*current);//返回current的父结点
    BinaryTreeNode<T> *LeftSibling(BinaryTreeNode<T>*current);//返回current的左兄弟
    BinaryTreeNode<T> *RightSibling(BinaryTreeNode<T>*current);//返回current的右兄弟
    //以elem为根结点,构造新的二叉树
    void CreateTree(const T&elem,BinaryTree<T>&leftTree,BinaryTree<T>&rightTree);

    void PreOrder(BinaryTreeNode<T>*root);//前序周游有二叉树或其子树
    void InOrder(BinaryTreeNode<T>*root);//中序周游有二叉树或其子树
    void PostOrder(BinaryTreeNode<T>*root);//后序周游有二叉树或其子树
//    void LevelOrder(BinaryTreeNode<T>*root);//按层次周游有二叉树或其子树
    void DeleteBinaryTree(BinaryTreeNode<T>*root);//删除二叉树或其子树
};
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::GetParent(BinaryTreeNode<T> *root,BinaryTreeNode<T> *current)
{//找父节点
    BinaryTreeNode<T>* temp;
    if (root == NULL)
        return NULL;
    if((root->leftchild()==current)||(root->rightchild()==current))
        return root;
    if((temp=GetParent(root->leftchild(),current))!=NULL)
        return temp;
    else
        return GetParent(root->leftchild(),current);

}
template<class T>//寻找父节点
BinaryTreeNode<T> * BinaryTree<T>::Parent(BinaryTreeNode<T>*current){
    if((current == NULL)||(current == root))
        return NULL;
    return GetParent(root,current);
}
template<class T>//返回current的左兄弟
BinaryTreeNode<T> * BinaryTree<T>::LeftSibling(BinaryTreeNode<T>*current){
    if(current)
    {
        BinaryTreeNode<T>*temp = Parent(current);
        if((temp==NULL)||(temp->leftchild()==NULL))
            return NULL;
        else
            return temp->leftchild();
    }
    return NULL;
}
template<class T>//返回current的右兄弟
BinaryTreeNode<T> * BinaryTree<T>::RightSibling(BinaryTreeNode<T>*current){
    if(current)
    {
        BinaryTreeNode<T>*temp = Parent(current);
        if((temp==NULL)||(temp->rightchild()==NULL))
            return NULL;
        else
            return temp->rightchild();
    }
    return NULL;
}
template<class T>//创建新树
void BinaryTree<T>::CreateTree(const T&elem,BinaryTree<T> &leftTree,BinaryTree<T> &rightTree)
{
    root = new BinaryTreeNode<T>(elem,leftTree.root,rightTree.root);
    leftTree.root = rightTree.root = NULL;
}
template<class T>//删除模板
void BinaryTree<T>::DeleteBinaryTree(BinaryTreeNode<T>*root)
{
    if (root)
    {
        DeleteBinaryTree(root->m_Left);
        DeleteBinaryTree(root->m_right);
        delete root;
    }
}
template<class T>
void BinaryTree<T>::PreOrder(BinaryTreeNode<T>*root){//前序周游有二叉树或其子树
    if (root!=NULL)
    {
        Root(root);
        PreOrder(root->leftchild());
        PreOrder(root->rightchild());
    }
}

template<class T>
void BinaryTree<T>::InOrder(BinaryTreeNode<T>*root){//中序周游有二叉树或其子树
    if (root!=NULL)
    {        
        InOrder(root->leftchild());
        Root(root);
        InOrder(root->rightchild());
    }
}
template<class T>
void BinaryTree<T>::PostOrder(BinaryTreeNode<T>*root){//后序周游有二叉树或其子树
    if (root!=NULL)
    {
        PostOrder(root->leftchild());
        PostOrder(root->rightchild());
        Root(root);
    }
}
//void BinaryTree<T>::LevelOrder(BinaryTreeNode<T>*root)
#endif

C++写的未完善的
2010-05-14 12:14
xu362726904
Rank: 6Rank: 6
等 级:侠之大者
帖 子:160
专家分:471
注 册:2009-6-18
收藏
得分:10 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define max 20
typedef struct tree
{
        char data;
        struct tree *lchild;
        struct tree *rchild;
}*Tree;
int CreatTree(Tree &T)//前序建立二叉树
{
    char ch;
    scanf("%c",&ch);
    if(ch == ' ') T = NULL;
    else
    {
         if(!(T = (struct tree *)malloc(sizeof(struct tree))))
        {
         printf("溢出\n");
         exit(0);
         }
         T->data = ch;
         CreatTree(T->lchild);
         CreatTree(T->rchild);
    }
         return 0;
}
/*
int CreatTree(Tree &T)//层次建立二叉树
{
    Tree Q[max];
    int front=1, rear=0;   
    Tree s, root;
     char ch;
    while((ch=getchar()) !='#')   
    {   
      s=NULL;  
      if(ch != ' ')   
      {
               s=(struct tree *)malloc(sizeof(struct tree));
               
               s->data = ch;
               
               s->lchild = NULL;
               
               s->rchild = NULL;
               
       }
      
     rear++;
      
      Q[rear] = s;
      
      if(rear == 1)
      
      root = s;

      else

      {   
          if(s&&Q[front])
        
           {
           
            if(!(rear % 2))
         
            Q[front]-> lchild = s;
         
            else Q[front]-> rchild = s;
         
            if(rear % 2 )
         
            front++;
          }
      }      
      
   }                     
   
    T = root;
   
    return 0;

} */

void preorder(Tree T)//前序遍历

{

     if(T)

     {

          printf("%c ",T->data);

          preorder(T->lchild);

          preorder(T->rchild);
   
     }

}
 
 void mid(Tree T)//中序遍历

{

     if(T)

     {
         
         mid(T->lchild);

         printf("%c ",T->data);

          mid(T->rchild);

       }

}

void last(Tree T)//后序遍历

{

     if(T)

     {
         
          last(T->lchild);
         
          last(T->rchild);
         
          printf("%c ",T->data);
     }
     
}  

void LevelOrder(Tree b)//层次遍历

{

    Tree p;

    Tree qu[max];

    int front,rear;

    front=rear=-1;

    rear++;

    qu[rear]=b;

    while(front!=rear)

    {

        front=(front+1)%max;

        p=qu[front];

        printf("%c ",p->data);

        if(p->lchild!=NULL)

        {

            rear=(rear+1)%max;

            qu[rear]=p->lchild;

        }   

        if(p->rchild!=NULL)

        {

            rear=(rear+1)%max;

            qu[rear]=p->rchild;

        }     

    }   

}  

int main()

{

    Tree  T=NULL;

    printf("先序遍历建立二叉树 空格为空指针:\n");
   
    //printf("层次遍历建立二叉树 空格为空指针‘#’输入结束:\n");  
   
    CreatTree(T);

    printf("先序遍历:\n");

    preorder(T);

    printf("\n");

    printf("中序遍历:\n");

    mid(T);

    printf("\n");

    printf("后序遍历:\n");

    last(T);

    printf("\n");

    printf("层次遍历:\n");

    LevelOrder(T);
   
    printf("\n");
     
    system("pause");
   
    return 0;
}
2010-05-14 12:49
草狼
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:577
专家分:1040
注 册:2010-4-6
收藏
得分:0 
回复 8楼 xu362726904
这代码应该跟你的一样了吧  为什么不行能  你的能编译通过么?

#include<stdio.h>
#include<stdlib.h>
struct jj
{
    char a;
    struct jj *l;
    struct jj *r;
};
void input(struct jj &p)//变&编译都不能通过了么  你的 能通过么?
{
    char c;
    if((c=getchar())==' ')
        p=NULL;
    else
    {
        p=(struct jj *)malloc(sizeof(struct jj));
        p->a=c;
        input(p->l);
        input(p->r);
    }
   
}

void output(struct jj *p)//前序历遍二叉树
{
      if(p)
      {
        printf("%c ",p->a);
        if(p->l!=NULL) output(p->l);
        if(p->r!=NULL) output(p->r);

      }

}

int main()
{
    struct jj *p=NULL;
    input(p);
    output(p);
}
2010-05-14 15:19
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
程序代码:
/*

 * Author: Devil Wang

 * Data: 01-21-2010

 * Notes: Template For Binary Tree

 */
#ifndef __BINTREE_H_
#define __BINTREE_H_
#include<iostream>
#include<cstdlib>
using std::cin;
using std::cout;
struct Exception{};
template<class KeyType,class InfoType>
struct TNode
{
    KeyType key;
    InfoType info;
    TNode<KeyType,InfoType> *left;
    TNode<KeyType,InfoType> *right;
    TNode<KeyType,InfoType> *parent;
    TNode<KeyType,InfoType>():left(NULL),right(NULL),parent(NULL){}
    TNode<KeyType,InfoType>(KeyType val, InfoType infoval=0):key(val),left(NULL),right(NULL),parent(NULL)
        {
            info=infoval;
        }
    friend ostream & operator<<(ostream &out,TNode<KeyType,InfoType> &Node)
        {
            out<<Node.key;
            return out;
        }
} ;
template<class KeyType, class InfoType>
class BinTree
{
private:
    TNode<KeyType,InfoType> *root;
    size_t Size;
protected:
    TNode<KeyType,InfoType> *minimum( TNode<KeyType,InfoType> *point)const
        {
            TNode<KeyType,InfoType> *p=point;
            while( NULL != p->left )
            {
                p=p->left;
            }
            try{
                if( NULL == p )
                    throw Exception();
                return p;
            }catch( Exception e )
            {
                exit;
            }
        }
    TNode<KeyType,InfoType> * maximum(TNode<KeyType,InfoType> *point)const
        {
            TNode<KeyType,InfoType> *p=point;
            while( NULL != p->right )
            {
                p=p->right;
            }
            try{
                if( NULL == p )
                    throw Exception();
                return p;
            }catch( Exception e )
            {
                exit;
            }

        }
    int leafcont(TNode<KeyType,InfoType> *pointer )
        {
            static int cont=0;
            if (NULL!=pointer)
            {
                leafcont(pointer->left);
                if(NULL== pointer->left && NULL==pointer->right)
                {
                    cont++;
                }
                leafcont(pointer->right);
                return cont;
            }
        }
    void travel(TNode<KeyType,InfoType> *T)const
        {
            if( NULL!=T )
            {
                travel(T->left);
                cout<<*T<<" ";
                travel(T->right);
            }
            return ;
        }
public:
    BinTree<KeyType,InfoType>():root(NULL),Size(0){}
    BinTree<KeyType,InfoType>(int n):root(NULL),Size(0)
        {
            KeyType m;
            for(int i=0; i<n;i++)
            {
                cin>>m;
                BTinsert(m);
            }
        }
    BinTree<KeyType,InfoType>( BinTree<KeyType,InfoType> & tree)
        {
            

        }
    BinTree<KeyType,InfoType>& operator=(BinTree<KeyType,InfoType> & tree)
        {
            
        }
    ~BinTree()
        {
            TNode<KeyType,InfoType> *p;
            while( NULL != root->left || NULL != root->right)
            {
                p=minimum(root);
                BTDelete(p);
            }
            if( NULL !=root )
            {
                delete root;
            }
        }
    void BTInsert(KeyType val)
        {
            if( NULL == root)
            {
                root=new TNode<KeyType,InfoType>(val);
                root->parent=root;
                Size++;
                return ;
            }
            else
            {
                TNode<KeyType,InfoType> *pr=root,*cur;
                cur=(root->key>val)?root->left:root->right;
                while( NULL != cur )
                {
                    pr=cur;
                    cur=cur->key>val?cur->left:cur->right;
                }
                try{
                    if( NULL != cur )
                        throw Exception();
                    cur=new TNode<KeyType,InfoType> (val);
                    cur->parent=pr;
                    if( pr->key > val)
                        pr->left=cur;
                    else
                        pr->right=cur;
                }catch( Exception())
                {
                    cout<<"Catch An Exception"<<endl;
                    cout<<"BTInsert"<<endl;
                    exit;
                }
                Size++;
            }
        }
    TNode<KeyType,InfoType> * BTSearch(KeyType val)const
        {
            TNode<KeyType,InfoType> * p=root;
            while( NULL != p && p->key!=val )
            {
                p=(p->key>val)?p->left:p->right;
            }
            try
            {
                if( NULL == p )
                    throw Exception();
                return p;
            }catch ( Exception e)
            {
                cout<<"Catch An Exception"<<endl;
                cout<<"BTSearch"<<endl;
                return NULL;
            }
        }
    TNode<KeyType,InfoType> *BTMaximum()const{return maximum(root);}
    TNode<KeyType,InfoType> *BTMinimum()const{return minimum(root);}
    size_t size()
        {
            return Size;
        }
    bool empty()
        {
            return Size==0;
        }
    TNode<KeyType,InfoType> *BTSuccessor( TNode<KeyType,InfoType> *pointer )const
        {
            try
            {
                TNode<KeyType,InfoType> *point=pointer;
                if( NULL == point || point== maximum(root))
                {
                    throw Exception();
                }
                if ( NULL != point->right )
                    return point->right;
                TNode<KeyType,InfoType> *pr=point->parent;
                if( NULL == pr )
                    throw Exception();
                while( NULL != pr && point==pr->right )
                {
                    point=pr;
                    pr=pr->parent;
                }
                return pr;
            }catch( Exception e)
            {
                cout<<"Catch An Exception" <<endl;
                cout<<"BTSuccessor"<<endl;
                return NULL;
            }
        }
    TNode<KeyType,InfoType> * BTPredecessor(TNode<KeyType,InfoType> *pointer )const
        {
            try
            {
                TNode<KeyType,InfoType> *point=pointer,*pr;
                if( NULL == point || point == BinTree::minimum(root))
                    throw Exception();
                if( NULL != point->left )
                    return point->left;
                pr=point->parent;
                while( pr->left == point && NULL != pr )
                {
                    point=pr;
                    pr=pr->parent;
                }
                return pr;
            }catch ( Exception e )
            {
                cout<<"Catch An Exception" <<endl;
                cout<<"BTPredecessor"<<endl;
                return NULL;
            }
        }
    void BTDelete( TNode<KeyType,InfoType> * z)
        {
            TNode<KeyType,InfoType> *x,*y;
            if ( NULL == z->left && NULL == z->right && z==z->parent )
            {
                delete z;
                z=NULL;
                root=NULL;
                Size--;
                return ;
            }
            if( NULL==z->left || NULL == z->right )
                y=z;
            else
                y=BinTree::BTSuccessor(z);
            if( NULL != y->left)
                x=y->left;
            else
                x=y->right;
            if ( NULL !=x )
                x->parent=y->parent;
            if( y->parent==y)
            {
                root=x;
                x->parent=x;
            }
            else if (y== y->parent->left)
                y->parent->left=x;
            else
                y->parent->right=x;
            if( z!=y )
            {
                z->key=y->key;
                z->info=y->info;
            }
            delete y;
            Size--;
        }
    void BTTravel()
        {
            travel(root);
            cout<<endl;
        }
    int BTLeafCont(){return leafcont(root);} 
};
#endif
2010-05-15 00:29
快速回复:二叉树的输入和前序遍历
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060620 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved