基本的一个binarytree的遍历程序

利用先序遍历创建二叉树,并给出相应二叉树的中序遍历结果。 // // binary_tree.cpp // BinaryTreeApp // // Created by ljpc on 2018/5/3. // Copyright © 2018年 ljpc. All rights reserved. // #include "binary_tree.h" BiTreeNode* CreatBiTree(char* s, int &i, int len) // 利用先序遍历创建二叉树 // 参数:先序遍历字符串s,字符串初始下标i=0,字符串. 阅读详情

// binarytree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
struct node
{
 int value;
 int right;
 int left;
}tree[15];
void inorder(int root){
 if(tree[root].left!=-1)
  inorder(tree[root].left);
 printf("%d ",tree[root].value);
 if(tree[root].right!=-1)
  inorder(tree[root].right);
}
void preorder(int root)
{
 printf("%d ",tree[root].value);
 if(tree[root].left!=-1)
  preorder(tree[root].left);
 if(tree[root].right!=-1)
  preorder(tree[root].right);
}
void postorder(int root)
{
 if(tree[root].left!=-1)
  postorder(tree[root].left);
 if(tree[root].right!=-1)
  postorder(tree[root].left);
 printf("%d ",tree[root].value);
}
main()
{
 /*
 first to initalized the matrix of the tree
 */
 for(int i=0;i<15;i++){
  tree[i].value=i;
 }
 tree[0].left=1;
 tree[0].right=2;
 tree[1].left=3;
 tree[1].right=4;
 tree[2].left=5;
 tree[2].right=6;
 tree[3].left=7;
 tree[3].right=8;
 tree[4].left=-1;
 tree[4].right=-1;
 tree[5].left=9;
 tree[5].right=10;
 tree[6].left=11;
 tree[6].right=12;
 tree[7].left=13;
 tree[7].right=14;
 tree[8].left=-1;
 tree[8].right=-1;
 tree[9].left=-1;
 tree[9].right=-1;
 tree[10].left=-1;
 tree[10].right=-1;
 tree[11].left=-1;
 tree[11].right=-1;
 tree[12].left=-1;
 tree[12].right=-1;
 tree[13].left=-1;
 tree[13].right=-1;
 tree[14].left=-1;
 tree[14].right=-1;
    /*the order functions are put here!!!!!!!!!!!*/
printf("copyrights by fish&SEhome============================>use it freely");
printf("this is preorder's result:/n");
 preorder(0);
printf("/n");
printf("this is inorder's result:/n");
 inorder(0);
printf("/n");
printf("this is postorder's result:/n");
 postorder(0);
printf("/n");
printf("========================================================");
 system("pause");
}
/*
The structure of the tree is listed here:
                      0
    1             2
          3        4       5        6
     7       8   -1 -1   9    10   11   12
13     14 
@Vincent
the tree has been stored in the array,every element is a node and it contains 3 fields
they are Value,Left,Right means the node's value and it's left & right subtree's root.
If you want to use order function,remove the // before the function.
*/

 

MOOC数据结构 04-树5 Root of AVL Tree (25分) MOOC数据结构 04-树5 Root of AVL Tree (25分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they d... 阅读详情

相关推荐

保姆级教程:手把手调试PX4Ctrl的油门映射与姿态控制(附ROS节点配置)

本文提供PX4Ctrl的深度调参指南,涵盖油门映射、姿态控制及ROS节点配置等关键环节。通过详细解析PX4控制器的核心逻辑、递归最小二乘法(RLS)参数估计及状态机设计,帮助开发者优化无人机飞控性能。附实战案例与安全调试建议,适合PX4开发者进阶学习。

weixin_30363981的博客 382

给定一棵二叉树,计算该二叉树的深度、总节点个数和叶子节点个数。

#include "binary_tree.h" int GetTreeDepth(BiTreeNode* root) // 计算该二叉树的深度 // 参数:二叉树根节点root // 返回:二叉树的深度 { // 请在这里补充代码,完成本关任务 /********** Begin *********/ int m; int n; if(root==NULL) { return 0; } else { m=GetTreeD

weixin_46314022的博客 4654

RT-Thread Studio使用笔记(四):带片外Flash(W25Q128)的OTA(YModem)

前言 在学习OTA之前,请先参考上两篇博客写的关于SPI设备及W25Q128的使用方法,传送门: https://blog.youkuaiyun.com/tigerots/article/details/107423633 https://blog.youkuaiyun.com/tigerots/article/details/107933991 如何使用OTA 一. 制作BootLoader 打开Bootloader 在线获取地址,注册,并根据RT-thread官方文档生成BootLoader,官方文档已经写的很清楚,再此

tigerots的专栏 4008

Python之binarytree库函数使用详解

文章目录Python之binarytree库函数使用详解库函数介绍例子 Python之binarytree库函数使用详解 本篇博客旨在大致介绍binarytree中部分库函数的作用,细节需要读者查阅相关帮助文档。 库函数介绍 函数名 输入参数 功能 _is_balanced(root) 二叉树的根结点(binarytree.Node类型) 如果二叉树是平衡的则返回树的高度,否则返回...

qq7835144的博客 5211

LinuxCNC开发实战:从安装到EtherCAT配置全解析

本文详细解析LinuxCNC开发实战,从系统安装到EtherCAT总线配置全流程。涵盖实时内核优化、伺服驱动器PDO映射、HAL配置及自定义运动学开发,帮助开发者快速搭建工业级数控系统,提升运动控制精度与稳定性。

rgv234567的博客 352

避坑指南:STM32移植CanFestival时那些没人告诉你的细节(含TJA1050硬件适配)

本文深入解析STM32移植CanFestival协议栈时的关键细节与TJA1050硬件适配技巧,涵盖AVR残留代码清理、定时器中断优化及CAN通信稳定性提升方案。特别针对TJA1050在特定波特率下的信号畸变问题提供实战解决方案,帮助开发者规避移植过程中的隐形陷阱,实现工业级稳定通信。

work3的博客 760

层次遍历二叉树

void HierarchyOrder(BiTreeNode* root) // 二叉树的层次遍历(队列实现) // 参数:二叉树根节点root // 输出:二叉树的层次遍历,中间没有空格,末尾不换行。 { // 请在这里补充代码,完成本关任务 /********** Begin *********/ queue<BiTreeNode*> q; if(root!=NULL) { q.push(root); } while(!

weixin_46314022的博客 1905

【一文读懂】卫星轨道的轨道参数(六根数)和位置速度矢量转换及其在终端距离、相对速度和多普勒频移计算中的应用

本文首先介绍了卫星轨道的六根数参数:半长轴 a、偏心率 e、轨道倾角 i、升交点赤经 Ω、近地点幅角 ω 以及真近点角 ν,并探讨了如何利用这些参数计算卫星与地面终端之间的距离、相对速度及由此产生的多普勒频移。利用开普勒运动公式计算当前瞬间的轨道半径;根据真近点角 ν 在围心坐标系中计算位置矢量和速度矢量;利用旋转矩阵(依次旋转 ω、i、Ω)将围心坐标系的状态矢量转换到地心惯性坐标系;最终结合地面终端信息,计算卫星与终端的欧氏距离、沿视线的相对速度及多普勒频移。

u011376987的博客 9487

高级数据结构与算法 | 二叉搜索树(Binary Search Tree)

文章目录二叉搜索树的概念二叉搜索树的作用排序查找实现思路查找插入删除删除、拷贝等代码实现K模型KV模型 二叉搜索树的概念 二叉搜索树又叫做二叉排序树,他是一个具有以下特性的二叉树。 1. 二叉搜索树的左孩子比父节点小,右孩子比父节点大。 2. 二叉搜索树的左子树的全部节点都小于根节点,右子树的全部节点都大于根节点。 3. 所有节点的左右子树都为二叉搜索树 4. 键值是唯一的,所以二叉搜索树不能有相同的键值。 例如以下数据 int arr[] = { 5, 3, 7, 9, 1, 4 }; 按照上面的规则

凌桓丶的博客 1298

C# 量子级跨平台开发:用Uno Platform实现Windows、iOS、Android的“量子纠缠”UI同步

墨瑾轩推出编程学习平台,通过XAML的“量子纠缠”技术,实现跨平台开发的高效与一致性。传统方案中,多平台代码重复率高,UI差异大,而量子级方案则通过一套代码构建全平台应用,UI原子化同步,开发效率提升5倍。Uno Platform的“量子纠缠”架构解决了代码碎片化、性能瓶颈和调试难题,实现UI原子化同步、性能提升300%和跨平台数据无缝同步。核心架构包括XAML的跨平台渲染、MVVM的量子叠加态架构、平台差异的量子退相干处理以及性能优化的量子隧穿效应,为开发者提供高效、一致的跨平台开发体验。

java专栏 1165

硅电容的工艺结构原理及选型参数总结

硅电容,也称为硅质电容或硅二极管电容,是一种特殊的电容器。它利用硅二极管的结构来实现电容的功能。本文对硅电容的工作原理,结构特点,选型参数及设计注意事项进行总结。

fydar的博客 2330

二叉数查表

工作这么多年从来没用过二叉树,Hash用的比较多或者直接数组遍历了,按照自己的理解写了个二叉树遍历,后面再去补理论 import binarytree.BinaryTree; public class HelloWorld { public static void main(String[] args) { // TODO Auto-generated method...

oria2006的专栏 246

【DRAM存储器四十三】LPDDR5介绍--寻址

简要说明LPDDR5 X16、x8 die的寻址

highman110的博客 966

Leetcode 437. 路径总和 III

2、当node为空时,应该直接返回0,因为已经在上一步的递归中对恰好满足的进行了+1计算,满足循环不变量的原则。这道题递推并不难,但是有细节需要注意,主要就是区间可以不是根节点,以及结束不一定是叶子结点这两个限制。1、由于终点不一定是叶子结点,所以当递归的target等于0时,需要加1。3、由于起点不一定是根节点,所以需要对整个树进行dfs遍历。其余的动态规划即可。

普通攻击往后拉的博客 301

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

二叉树 | Binary Tree | C/C++实现

问题描述 给定有根二叉树T,请编写一个程序,输出其各结点u的如下信息: u的结点编号 u的深度 u的父结点 u的高 u的兄弟结点 结点的种类(根、内部结点、叶) u的子结点数 设给定二叉树拥有n个结点,编号分别为0至n-1。 输入: 第1行输入结点的个数n。接下来n行按照下述格式输入各结点的信息,每个结点占1行。 id left right id为结点编号,left为左子结点编号,right为右子...

qq_40998706的博客 455

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

BinaryTreecpp

#include #include using namespace std; template struct BSTNode { T _val; BSTNode* _left; BSTNode* _right; BSTNode(const T& val = T()) : _val(val) , _left(nullptr) , _right(nullptr) { } }; template class BSTree { public: typedef BSTNode Node; Node*

weixin_45258996的博客 193

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

5-6 Root of AVL Tree (25分) (二叉平衡树的旋转操作)

5-6 Root of AVL Tree   (25分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they dif

老问题的博客 638

04-树5 Root of AVL Tree (25分)

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is

戴璞微的学习之路 2289

1043 Is It a Binary Search Tree (25 分)(算法笔记)

1043 Is It a Binary Search Tree (25 分) … Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N integer keys are given...

ostrich的博客 946

浙大数据结构习题笔记:04-树5 Root of AVL Tree (25分)

04-树5 Root of AVL Tree (25分) 没什么好说的……其实就是插入平衡二叉树的过程,具体实现慕课中已经讲过了,写一下再注意一下输入格式就好了。 #include <iostream> #include <malloc.h> typedef struct AVLNode *AVLTree; struct AVLNode{ int data; // 存值 AVLTree left; // 左子树 AVLTree right; // 右子树

Jimobbb的博客 374

6-3 Height of Binary Search Tree (25 分)

You are supposed to write a function of finding the height of a binary search tree with the given postorder sequence. Format of function: int Height_of_BST( int postorder[], int N ); where the postorder sequence is stored in int postorder[], and the integ

weixin_45928161的博客 1574

7-20 Binary Search Tree(25 分)

https://pintia.cn/problem-sets/16/problems/682A binary search tree is uniquely determined by a given ordered insertions of a sequence of positive integers. On the other hand, a given binary search tr...

itorly的博客 3207

04-树5 Root of AVL Tree (25 分)【每日一题】

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...

JJJ's blog 1148

7-20 Binary Search Tree (25 分)

二叉搜索树 把两棵树都建好再比较 #include <cstdio> #include <iostream> #include <algorithm> using namespace std; struct tree{ int val; tree* left, *right; tree(int x){ val = x; left = right ...

姚军 417

asp.net里面页面定位

我要在这里写的,完全是在我这个菜鸟的基础上,如果您看到觉得简单,请不要抱怨。如果您发现错误,我会虚心听您指教。如果要交朋友……常来我的blog座座。QQ:94768815                                                                                                                 

Vincent--programming life 3535

一个利用堆栈实现的正整数10进制向16进制的转化程序

/*用STACK将任意一个十进制数转化为十六进制数,输出*/#include "Stdio.h"#include "Conio.h"#define stackinitsize 100;#define stackincrement 10;typedef struct{    int *base;   int *top;    int stacksize;}sqstack; 

Vincent--programming life 2785
上一篇: 参加ACM大赛手记(final)
下一篇: 大学一年级做过的一道程序题——螺旋矩阵
fishstudio
博客等级 码龄23年 4粉丝 41原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值