二叉树的建立(链式存储,遍历,深度,节点数,路径)

数据结构编程练习(六)

题目:

1)能够调用递归函数读取相应的数据建立二叉树,相应数据格式自行设计; 

2)实现先序、中序、后序遍历二叉树

3)求取二叉树中的所有结点数

4)求取二叉树的深度

5)查找某节点是否存在并输出路径



输入如图所示二叉树的方式为依次输入:1,2,-1,4,-1,-1,3,5,-1,-1,6,-1,-1

代码实现:

#include "iostream"
#include "stack"
using namespace std;
struct tree
{
    int data;
    tree *lchild,*rchild;
};
class BiTree
{
public:
    BiTree();
    ~BiTree();
    int preorder(tree *node);
    int inorder(tree *node);
    int postorder(tree *node);
    int visit(tree *node);
    int create_BT(tree *&node);
    int calculate_node(tree *node,int &n);
    int depth(tree *node);
    int path(tree *node, int x);
    int max(int a,int b);
    void Release(tree *&node);
    stack <int> Path;
    bool flag;//是否查找到节点标志 
//private:
    tree *root;
};
BiTree::BiTree()
{
    root=new tree;
    root->lchild=NULL;
    root->rchild=NULL;
}
BiTree::~BiTree()
{
    Release(root);
}
void BiTree::Release(tree *&node)
{
    if (node!=NULL)
    {
        cout<<node->data<<"已被删除"<<endl;
        Release(node->lchild);   //释放左子树
        Release(node->rchild);   //释放右子树
        delete node;
    }  
}
//二叉树的建立(链式存储)  
int BiTree::create_BT(tree *&node)
{
    int n;
    cin>>n;
    if(n<0)
    {
        node=NULL;
        return 0;
    }
    else
    {
        node=new tree;
        node->data=n; 
        create_BT(node->lchild);
        create_BT(node->rchild);
    }
}
//访问结点 
int BiTree::visit(tree *node)
{
    cout<<node->data<<" ";
    return 0;
}
//先序遍历
int BiTree::preorder(tree *node)
{
    if(node!=NULL)
    {
        visit(node);
        preorder(node->lchild);
        preorder(node->rchild);
    }
    return 0;
} 
//中序遍历
int BiTree::inorder(tree *node)
{
    if(node!=NULL)
    {
        inorder(node->lchild);
        visit(node);
        inorder(node->rchild);
    }
    return 0;
} 
//后序遍历
int BiTree::postorder(tree *node)
{
    if(node!=NULL)
    {
        postorder(node->lchild);
        postorder(node->rchild);
        visit(node);
    }
    return 0;
} 
//计算二叉树的结点数(先序遍历法) 
int BiTree::calculate_node(tree *node,int &n)
{
    if(node!=NULL)
    {
        n++;
        calculate_node(node->lchild,n);
        calculate_node(node->rchild,n);
    }
    return 0;
} 
//求二叉树的深度
int BiTree::depth(tree *node) 
{
    if(node==NULL)
        return 0;
    else
        return max(depth(node->lchild),depth(node->rchild))+1;
}
//计算到某节点的路径
int BiTree::path(tree *node, int x)
{
    if(node!=NULL)
    {
        if(node->data==x)
        {
            flag=true; 
            cout<<"从根节点到"<<x<<"节点的路径为:" <<endl;
            while(!Path.empty())
            {
                cout<<x<<" ";
                x=Path.top();
                Path.pop();
            }
            cout<<x<<endl;
            return 1;
        }
        else
        {
            Path.push(node->data);
            path(node->lchild,x);
            path(node->rchild,x);    
            Path.pop();
        }
    }
    return 0;
} 
//取较大值 
int BiTree::max(int a,int b)
{
    return a>b?a:b;
}
int main()
{
    BiTree obj1;
    obj1.flag=false;
    int n=0;
    obj1.create_BT(obj1.root);
    cout<<"二叉树的先序遍历:"; 
    obj1.preorder(obj1.root);
    cout<<endl;
    cout<<"二叉树的中序遍历:";
    obj1.inorder(obj1.root);
    cout<<endl;
    cout<<"二叉树的后序遍历:";
    obj1.postorder(obj1.root);
    cout<<endl;
    obj1.calculate_node(obj1.root,n);
    cout<<"结点数为:"<<n<<endl;
    n=obj1.depth(obj1.root);
    cout<<"深度为:"<<n<<endl; 
    int x;
    cout<<"请输入要查询的节点值:";
    cin>>x; 
    obj1.path(obj1.root,x);
    if(!obj1.flag)
        cout<<"未找到该节点"<<endl;
    return 0;
}







  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值