二叉链表查找

23 篇文章 0 订阅

Problem Description

有一棵二叉树,其结点值为字符型并假设各值互不相等,采用二叉链表存储。现输入其扩展二叉树的前序遍历序列,建立该二叉树,要求在该二叉树中查找字符值为x的结点,找到x时,输出x的相关信息,没找到x则输出"not find"。

 Input

第一行为一个整数n,表示以下有n组数据,每组数据占两行,每组第一行表示扩展二叉树的前序遍历序列;第二行表示待查找的字符x。

 Output

若查找成功,先输出该结点的双亲值,再输出其左右孩子值:(左孩子,右孩子),若其双亲或者孩子值为空输出#;查找失败输出“not find”。
 

 Sample Input

4
AB#D##C##
A
AB#D##C##
B
AB#D##C##
D
AB#D##C##
G

 Sample Output

#(B,C)
A(#,D)
B(#,#)
not find
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;


typedef struct BiNode {

    char data;
    struct BiNode* lchild,*rchild;


} BiNode,*BiTree;

class Tree {
public:
    Tree() {
        root=Creat();
        temp=NULL;
        p=NULL;
        rtemp=ltemp='#';
        fine=false;
    }
    ~Tree() {
        Release(root);
    }

    void preorder() {
        preorder(root);
    }

    bool Find(char ch) {
        Find(root,ch);
        return fine;
    }


    void Parent(char ch) {

        Parent(root,p,ch);
        if(!temp) {
            cout<<"#";
        } else {
            cout<<temp->data;
        }
    }

    void Children(char ch) {

        Children(root,ch);
        cout<<"("<<ltemp<<","<<rtemp<<")"<<endl;
    }


private:
    BiTree root;
    bool fine;
    void Find(BiTree root,char ch) {
        if(root) {
            if(root->data==ch) {
                fine=true;

            } else {
                Find(root->lchild,ch);
                Find(root->rchild,ch);
            }
        }

    }

    void Release(BiTree t) {
        if(t) {
            Release(t->lchild);
            Release(t->rchild);
            delete t;
        }

    }
    void preorder(BiTree root) {
        if(root) {
            cout<<root->data;
            preorder(root->lchild);
            preorder(root->rchild);
        }
    }
    BiTree Creat() {
        BiTree bt;
        char ch;
        cin>>ch;
        if(ch=='#') {
            bt=NULL;
        } else {
            bt=new BiNode;
            bt->data=ch;
            bt->lchild=Creat();
            bt->rchild=Creat();
        }
        return bt;
    }
    BiTree temp;
    BiTree p;
    void Parent(BiTree root,BiTree p,char ch) {

        if(root) {
            if(root->data==ch) {

                temp=p;
            }
            Parent(root->lchild,root,ch);
            Parent(root->rchild,root,ch);
        }

    }
    char rtemp,ltemp;
    void Children(BiTree root,char ch) {

        if(root) {
            if(ch==root->data) {
                if(root->lchild)
                    ltemp=root->lchild->data;
                if(root->rchild)
                    rtemp=root->rchild->data;
            }
            Children(root->lchild,ch);
            Children(root->rchild,ch);
        }

    }


};

int main() {

   
    int n;
    while(cin>>n) {
        while(n--) {
            Tree t;
            char ch;
            cin>>ch;
            if(t.Find(ch)) {
                t.Parent(ch);
                t.Children(ch);
            } else {
                cout<<"not find"<<endl;
            }

        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haibianyoushark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值