首页 > 其他分享 >Binary tree traversal-- beadth-first and depth-first【1月23日学习笔记】

Binary tree traversal-- beadth-first and depth-first【1月23日学习笔记】

时间:2024-01-23 22:00:40浏览次数:16  
标签:node Binary right 23 root travptr left data first

点击查看代码
//Binary tree traversal-- beadth-first and depth-first
#include<iostream>
#include<queue>//STL
using namespace std;

struct node {
    int data;
    node *left, *right;
};

node* getnewnode(int x)
{
    node* temp = new node;
    temp->data = x;
    temp->left = temp->right = NULL;

    return temp;
}

void insert(node*& travptr, int data) {//Node*& travptr 表示 travptr 是一个引用,引用的是一个指向 Node 类型的指针

    if (travptr == NULL)
    {
        travptr = getnewnode(data);
        return;
    }
    (data > travptr->data) ? insert(travptr->right, data) : insert(travptr->left, data);
}

//先根遍历
void preorder(node* root) {
    if (root == NULL)  return;//既是特殊情况,又是中止情况
    cout << root->data << " ";
    preorder(root->left);
    preorder(root->right);
}//空间复杂度:O(h)where h is the height of the tree

//中根遍历
void inorder(node* root) {
    if (root == NULL)  return;
    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
}//空间复杂度:O(h)where h is the height of the tree

//后根遍历
void postorder(node* root) {
    if (root == NULL)  return;
    postorder(root->left);
    postorder(root->right);
    cout << root->data << " ";
}//空间复杂度:O(h)where h is the height of the tree

int main()
{
    node* root = NULL;

    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 0);
    insert(root, 5);
    preorder(root);   cout << endl;
    inorder(root);      cout << endl;
    postorder(root);     cout << endl;
    return 0;
}

标签:node,Binary,right,23,root,travptr,left,data,first
From: https://www.cnblogs.com/whvivy/p/17983521

相关文章

  • 1.23
    模拟赛题根本不想调啊。但是真的是模拟赛题吗,显然它不应当按照模拟赛来看,但是只有这样才会让我好受些。实力本来就不足,现在我就是在为之前的颓废而买单,错误的心态,过慢的进度,大量的颓废以及贺题解的恶习终于回报了我,所有小题都狠狠地挂分,当然最后的提高难度的题也是直接暴力,菜就......
  • 1.23闲话
    推歌:光与影的对白/洛天依byCopy快期末考试了,所以来机房的只有我了但是我说的不对,今天其实有不少来的但是晚三之前就走光了找到了几张存起来的夏虫图,但是貌似有点小?唯一一张大图放末尾了阿$\infty$阿$\infty$我对贪心策略大幅改进,现在可以过掉很多数据了,感觉再改一下就......
  • Binary tree traversal-- level-order traversal using queue【1月23日学习笔记】
    点击查看代码//Binarytreetraversal--level-ordertraversalusingqueue#include<iostream>#include<queue>//STLusingnamespacestd;structnode{intdata;node*left,*right;};node*getnewnode(intx){node*temp=newnode;t......
  • 百度网盘(百度云)SVIP超级会员共享账号每日更新(2024.01.23)
    一、百度网盘SVIP超级会员共享账号可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答。我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免了U盘的物理载体,直接在网上就实现文件传输。百度网盘SVIP会员可以让自己百度账......
  • 2024-1-23URL查询参数
    目录URL查询参数小结URL查询参数在axios中查询参数需要用到params选项注意:axios在运行时把参数名和值,会拼接到url?参数名=值格式axios({ url:'目标资源地址', paramas:{ 参数名:值 }}).then(result=>{ //对服务器返回的数据做后续处理})然后这里提供一个例子(用于......
  • 0123今日收获
    今日代码1今日代码2今日代码3今日代码4今日代码5今日代码6今日代码7今日代码8今日代码9!今日收获满满......
  • 淘宝NPM站点证书已过期(2024年1月23日)
    今天前端项目执行CI发版报错:[1/4]Resolvingpackages...errorAnunexpectederroroccurred:"https://registry.npm.taobao.org/axios:certificatehasexpired". 从浏览器访问 https://registry.npm.taobao.org提示:你的连接不是专用连接攻击者可能试图从registr......
  • Find height of a binary tree【1月23日学习笔记】
    点击查看代码#include<iostream>usingnamespacestd;structNode{intdata;Node*left,*right;};Node*newNode(intx){Node*temp=newNode;temp->data=x;temp->left=temp->right=NULL;returntemp;}voidin......
  • Find min and max element in bst using recursion【1月23日学习笔记】
    点击查看代码#include<iostream>usingnamespacestd;structNode{intdata;Node*left,*right;};Node*newNode(intx){Node*temp=newNode;temp->data=x;temp->left=temp->right=NULL;returntemp;}voidin......
  • 0123所学内容
    0123所学内容复习内容hello中的常见问题变量和注释变量的定义:在内存空间中申请一块存储区域变量的注意事项1.必须声明2.必须初始化3.不能重复三种注释标识符的命名规范(Alibaba开发文档)标识符的运用练习1Nameimportjava.util.Scanner;publicclassName{pu......