二叉树的遍历(c++)

二叉树遍历二叉树存储结构二叉树类先序遍历中序遍历后序遍历层序遍历建立二叉树本篇文章使用c++描述普通二叉树的遍历操作代码详见:https://github.com/rebelsisyphus/vscode/blob/main/.vscode/data%20structure/Bintree/tree.h二叉树存储结构使用链式存储结构template <class T>struct BinTreeNode{ T data; int height; BinTree
摘要由CSDN通过智能技术生成


本篇文章使用c++描述普通二叉树的遍历操作
代码详见:https://github.com/rebelsisyphus/vscode/blob/main/.vscode/data%20structure/Bintree/tree.h

二叉树存储结构

使用链式存储结构

template <class T>
struct BinTreeNode{
   
    T data;
    int height;
    BinTreeNode<T> *leftChild,*rightChild;
    BinTreeNode():leftChild(NULL),rightChild(NULL),height(-1){
   };
    BinTreeNode(T x,BinTreeNode<T> *l=NULL,BinTreeNode<T> *r=NULL){
   
        data=x;
        leftChild=l;
        rightChild=r;
        height=0;
    }
};

二叉树类

二叉树类,实现二叉树的建造,求树高/元素数,四种遍历方式,不进行插入删除操作
若遍历函数使用递归实现,那么形参需要二叉树与visit函数,root需要作为形参,
而考虑到程序的封装性,我们并不希望主函数可以直接访问数据
因此将递归函数设置为保护成员,在公有函数中设置接口,调用递归函数,visit函数可以由主函数提供
增强其封装性

/*************************************************************************
 二叉树类,实现二叉树的建造,求树高/元素数,四种遍历方式,不进行插入删除操作
***********************************************************************/
template <class T>
class BinTree{
   
    protected:
       BinTreeNode<T> *root;
       void deleteall(BinTreeNode<T> *subtree);
       int getheight(BinTreeNode<T> *subtree);  //树高
       int getsize(BinTreeNode<T> *subtree);  //树内元素数
       void Inorder(BinTreeNode<T> *subtree,void (*visit)(BinTreeNode<T> *s));     //中序遍历
       void Preorder(BinTreeNode<T> *subtree,void (*visit)(BinTreeNode<T> *s));    //前序遍历
       void Postorder(BinTreeNode<T> *subtree,void (*visit)(BinTreeNode<T> *s));   //后序遍历
       void Levelorder(BinTreeNode<T> *subtree,void (*visit)(BinTreeNode<T> *s));  //层序遍历
       /************************************************************************
        若遍历函数使用递归实现,那么形参需要二叉树与visit函数,root需要作为形参,
        而考虑到程序的封装性,我们并不希望主函数可以直接访问数据
        因此我们将递归函数设置为私有,在公有函数中设置接口,调用递归函数,visit函数可以由主函数提供
        增强其封装性
        ***********************************************************************/
    public:
       BinTree
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种常见的数据结构遍历二叉树有多种方法,包括先序遍历、中序遍历和后序遍历。以下是在C语言中用数组表示二叉树并进行遍历的示例代码: ```c #include <stdio.h> // 定义二叉树节点结构 struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; }; // 构建二叉树 struct TreeNode nodes[10]; // 使用数组表示二叉树 void buildTree() { for (int i = 0; i < 10; i++) { nodes[i].data = i; nodes[i].left = NULL; nodes[i].right = NULL; } nodes[0].left = &nodes[1]; nodes[0].right = &nodes[2]; nodes[1].left = &nodes[3]; nodes[1].right = &nodes[4]; nodes[2].left = &nodes[5]; nodes[2].right = &nodes[6]; } // 先序遍历 void preOrder(struct TreeNode *root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } // 中序遍历 void inOrder(struct TreeNode *root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } } // 后序遍历 void postOrder(struct TreeNode *root) { if (root != NULL) { postOrder(root->left); postOrder(root->right); printf("%d ", root->data); } } int main() { buildTree(); printf("先序遍历结果:"); preOrder(&nodes[0]); printf("\n中序遍历结果:"); inOrder(&nodes[0]); printf("\n后序遍历结果:"); postOrder(&nodes[0]); return 0; } ``` 以上代码定义了一个二叉树的结构,使用数组表示节点,并实现了先序、中序和后序遍历的方法。在主函数中构建了一个二叉树,并进行了遍历操作,打印出遍历的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值