基于广义表二叉树字符串递归生成二叉树,各种递归非递归遍历二叉树,查找二叉树操作,求二叉树高度,深度,结点度数,叶子结点度数集合_广义表生成二叉树递归法-程序员宅基地

技术标签: C++  

基本思路都在代码注释里

#include <string>
#include <iostream>
#include <algorithm>
#include <stack>

using namespace std;

struct treeNode {
   
    
    string data;

    treeNode *leftChild{
   
    };
    treeNode *rightChild{
   
    };

    int leftTag;
    int rightTag;
};

//返回的是中点逗号的位置
//1.当inputData为“C(D,E),F”时,返回“6”,
//2.当inputData为“C,D”时,返回“1”
//3.当inputData为“,A(B,C)”时,返回“0”
//4.当inputData为“C(D,E)”时,返回“-1”
int findMiddlePosition(string inputData) {
   
    
    int result = -1;

    int leftK = 0;
    int rightK = 0;
    int position = 0;
    while (inputData[position]) {
   
    
        if (inputData[position] == '(') {
   
    
            ++leftK;
        } else if (inputData[position] == ')') {
   
    
            ++rightK;
        } else if (leftK == rightK && inputData[position] == ',') {
   
    
            result = position;
            break;
        }
        ++position;
    }

    return result;
}

//提取出数据
//1.当left为true时,提取的是左边的值
//  1.当inputData为“C(D,E),F”时,返回“C(D,E)”,
//  2.当inputData为“,A(B,C)”时,返回空字符串
//  3.当inputData为“C(D,E)”时,返回“C(D,E)”

//2.当left为false时,提取的是右边的值
//  1.当inputData为“C(D,E),F”时,返回“F”
//  2.当inputData为“C(D,E)”时,返回空字符串
string getString(const string &inputData, bool left) {
   
    
    string result;

    int middlePosition = findMiddlePosition(inputData);
    if (left) {
   
    
        if (middlePosition == -1) {
   
    
            result = inputData;
        } else if (middlePosition != 0) {
   
    
            result = inputData.substr(0, middlePosition);
        }
    } else {
   
    
        if (middlePosition != -1) {
   
    
            result = inputData.substr(middlePosition + 1, inputData.length() - middlePosition - 1);
        }
    }

    return result;
}

//对字符串进行处理
//inputData会有两种输入情况
//  1.输入的只是一个字符,此时直接返回空字符串
//  2.输入的是一个形如A(B(C,D),E)的形式,去掉第一,第二,最后一个字符,将返回B(C,D),E字符串
string eraseString(const string &inputData) {
   
    
    string result;

    if (inputData.length() != 1) {
   
    
        result = inputData.substr(2, inputData.length() - 2 - 1);
    }

    return result;
}

//返回第一个字符
string getData(const string &inputData) {
   
    
    return inputData.substr(0, 1);
}

//函数返回的是根节点
treeNode *buildTree(const string &inputData) {
   
    
    auto *node = new treeNode;
    node->data = getData(inputData);

    string newInputData = eraseString(inputData);

    
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/vancece/article/details/103356968

智能推荐

序列解包相关_序列解包 编码规范-程序员宅基地

文章浏览阅读53次。>>> #序列解包>>> x,y,z=(20,30,10)>>> x20>>> y30>>> z10>>> (a,b,c)=(9,8,10)>>> x20>>> y30>>> a9>>> [a,b,c]=[10,20,30]>>> a10>>> b20&g_序列解包 编码规范

selenium WebDriver 中的几种等待--sleep(),implicitly_wait(),WebDriverWait()-程序员宅基地

文章浏览阅读296次。强制等待:sleep()import timesleep(5) #等待5秒设置固定休眠时间,单位为秒。 由python的time包提供, 导入 time 包后就可以使用。缺点:不智能,使用太多的sleep会影响脚本运行速度。隐式等待:implicitly_wait()driver.implicitly_wait(10) #隐式等待10秒由webdriver提供的方法,一旦设..._selenium wait until 能与sleep一起用吗?

Unity 绘制物体运动轨迹_unity 弹道轨迹怎么画-程序员宅基地

文章浏览阅读7.7k次,点赞6次,收藏83次。unity 物体运动轨迹绘制① create empty,命名为LineRender② 在Assects中新建材质,选择Shader为Sprites/Default,并设置轨迹颜色,如下图:③ 选择①中创建的object,添加Line Render属性,然后将②中新建的材质赋给该object,如下图:展开Line Render,拖动Width可设置轨迹宽度④ 创建c#脚本,拖至运动物体上,代码如下:using System.Collections;using System.Collect_unity 弹道轨迹怎么画

VISIO画立体图——VISIO画图技巧-程序员宅基地

文章浏览阅读6w次,点赞48次,收藏250次。3分钟你将学到VISIO基础操作线图形与文本移动VISIO画流程图连接线的使用VISIO画立体图组合功能高阶使用实例分享VISIO基础操作线图形与文本移动VISIO画流程图连接线的使用VISIO画立体图组合功能高阶使用实例分享..._visio画立体图

SQL Server安装失败,SQL Server卸载不干净_sql安装失败如何彻底清理干净-程序员宅基地

文章浏览阅读2w次,点赞18次,收藏122次。SQL Server安装失败,SQL Server卸载不干净版本:SQL Server2019问题一:找不到安装路径Can’t Install SQL Server 2019 (Express Edition) | Exit code (Decimal): -2147467259Error description: The system cannot find the path specified一、卸载干净SQL Server1.在控制面板卸载与SQL Server相关的组件以win10为例,_sql安装失败如何彻底清理干净

GridSearchCV与cross_validation区别_cross_validate gridsearchcv-程序员宅基地

文章浏览阅读1.2k次。转自https://blog.csdn.net/qq_32241189/article/details/80182114一.交叉验证 交叉验证就是将原始数据集(dataset)划分为两个部分.一部分为训练集用来训练模型,另外一部分作为测试集测试模型效果. 作用: 1) 交叉验证是用来评估模型在新的数据集上的预测效果,也可以一定程度上减小模型的过拟合 ..._cross_validate gridsearchcv

随便推点

Alpine Linux 安装SSH samba-server python3_alpine samba-程序员宅基地

文章浏览阅读2.2k次。sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositoriesapkupdateInstallationInstall theopensshpackage:apk add opensshService commandsEnable the sshd service so that it starts at boot:rc-update add sshdList services..._alpine samba

远程管理WinRM,Enter-PSSession-程序员宅基地

文章浏览阅读852次。wmimgmt.msc-------打开windows管理体系结构(WMI)启用PowerShell远程管理:1)在本地计算机(需要管理远程计算机的计算机)上运行Set-item wsman:localhost\client\trustedhosts –value *,添加trusthost列表2)在远程计算机(需要被远程管理的计算机上)上运行Enable-PSremoting -force即可=..._enter-pssession与winrs都会形成交互式命令行,不同的是winrs作用在cmd和powershel

Rockchip Android13平台提取kernel环境编译KO_clang编译ko-程序员宅基地

文章浏览阅读4.2k次,点赞2次,收藏29次。当需要给第三方提供kernel的ko编译环境时,又不想提供完整的kernel源码,则可以对kernel进行裁剪提取出最小的编译环境和编译器提供给第三方即可。_clang编译ko

Linux 线路规程_linux线路规程-程序员宅基地

文章浏览阅读1.2k次。line discipline(LDISC) 线路规程,是linux和类unix系统终端子系统的一个软件驱动层。终端子系统从上到下可划分为三层:顶层tty core驱动层提供字符设备接口(因为所有的终端设备都是字符设备);最底层是tty driver层用来和硬件进行通讯,实现tty_operations供tty core和 LDISC层调用;中间层line discipline实现终端输入_linux线路规程

易居住房8(“个人中心”--编辑资料)_易居房评编辑-程序员宅基地

文章浏览阅读282次。在“易居住房7”的基础上进行增加或修改代码“pages”中添加“UserAuth.java”“pages”中添加编辑资料相关–“personal.jsp”,“personalEdit.jsp”;个人认证相关–“verify.jsp”,“verifyApply.jsp”我的资料–编辑资料“IUserDao.java”增加代码 void updateUserInfo(UserInf..._易居房评编辑

listview计算滑动高度 判断上滑下滑 隐藏控件_abslistview.onscrolllistener判断向上向下滑-程序员宅基地

文章浏览阅读406次。lv_followlistview.setOnScrollListener(new AbsListView.OnScrollListener() { private SparseArray recordSp = new SparseArray(0); private int mCurrentfirstVisibleItem = 0; @Override publ_abslistview.onscrolllistener判断向上向下滑