二叉树基本操作C++
#include <cstdio>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <string> struct Node {
int value = ;
struct Node *left = nullptr;
struct Node *right = nullptr;
}; void preorder_print_aux(Node *T) {
if (T == nullptr) {
return ;
}
std::cout << T->value << ' ';
preorder_print_aux(T->left);
preorder_print_aux(T->right);
} void preorder_print(Node *T) {
preorder_print_aux(T);
std::cout << std::endl;
} Node *CreateBiTreeAux(Node *&T, std::vector<int>::const_iterator &ite) {
// 按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,
// 构造二叉链表表示的二叉树T。
int elem = *ite++; if (elem == INT_MIN) {
T = nullptr;
} else {
T = new Node;
T->value = elem; // 生成根结点
CreateBiTreeAux(T->left, ite); // 构造左子树
CreateBiTreeAux(T->right, ite); // 构造右子树
}
return T;
} Node *create_tree(Node *&T, const std::vector<int> &input) {
std::vector<int>::const_iterator ite = input.begin();
CreateBiTreeAux(T, ite); return T;
} void free(Node *&T)
{
if (T == nullptr)
return; free(T->left);
free(T->right);
delete T;
T = nullptr;
} int sum(struct Node *tree, int *max_sum) {
if (tree->left == nullptr && tree->right == nullptr) {
*max_sum = tree->value;
return tree->value;
} int left_sum = INT_MIN;
int l_max_sum = INT_MIN;
if (nullptr != tree->left) {
left_sum = sum(tree->left, &l_max_sum);
} int right_sum = INT_MIN;
int r_max_sum = INT_MIN;
if (nullptr != tree->right) {
right_sum = sum(tree->right, &r_max_sum);
} int ret = tree->value + left_sum + right_sum;
if (ret < l_max_sum) {
ret = l_max_sum;
}
if (ret < r_max_sum) {
ret = r_max_sum;
} *max_sum = ret;
return ret;
} void assert_equal(int expect, std::vector<int> tree) {
Node *T = nullptr;
create_tree(T, tree);
preorder_print(T);
int ret = ;
sum(T, &ret);
if (expect != ret) {
fprintf(stderr, "Expect %d, but result is %d.\n", expect, ret);
exit();
}
free(T);
} int main() {
assert_equal(, {, INT_MIN, INT_MIN});
assert_equal(, {, , INT_MIN, INT_MIN, , INT_MIN, INT_MIN});
assert_equal(, {, -, INT_MIN, INT_MIN, , INT_MIN, INT_MIN});
assert_equal(, {-, , -, INT_MIN, INT_MIN, , INT_MIN, INT_MIN, , INT_MIN, INT_MIN});
assert_equal(, {-, -, -, INT_MIN, INT_MIN, , INT_MIN, INT_MIN, -, INT_MIN, INT_MIN}); std::cout << "OK\n"; return ;
}
二叉树基本操作C++的更多相关文章
- c++学习笔记—二叉树基本操作的实现
		
用c++语言实现的二叉树基本操作,包括二叉树的创建.二叉树的遍历(包括前序.中序.后序递归和非递归算法).求二叉树高度,计数叶子节点数.计数度为1的节点数等基本操作. IDE:vs2013 具体实现代 ...
 - c语言二叉树基本操作
		
编译器为vs2013 #include "stdafx.h" #include<malloc.h> #include<stdlib.h> #define O ...
 - 二叉树基本操作C代码
		
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct ChainTree) struct ChainTr ...
 - c++(排序二叉树线索化)
		
前面我们谈到了排序二叉树,还没有熟悉的同学可以看一下这个,二叉树基本操作.二叉树插入.二叉树删除1.删除2.删除3.但是排序二叉树也不是没有缺点,比如说,如果我们想在排序二叉树中删除一段数据的节点怎么 ...
 - 基于Java实现红黑树的基本操作
		
首先,在阅读文章之前,我希望读者对二叉树有一定的了解,因为红黑树的本质就是一颗二叉树.所以本篇博客中不在将二叉树的增删查的基本操作了,需要了解的同学可以到我之前写的一篇关于二叉树基本操作的博客:htt ...
 - Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
		
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
 - 07. Go 语言接口
		
Go 语言接口 接口本身是调用方和实现方均需要遵守的一种协议,大家按照统一的方法命名参数类型和数量来协调逻辑处理的过程. Go 语言中使用组合实现对象特性的描述.对象的内部使用结构体内嵌组合对象应该具 ...
 - cb23a_c++_标准模板库STL_set_multiset_关联容器
		
cb23a_c++_标准模板库STL_set_multiset_关联容器 set(集)数据不能重复.multiset(多集)可以重复.操作数据速度快,数据自动排序.红黑树(数据结构)红黑树-二叉树基本 ...
 - cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器
		
cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器map(映射,key不能重复,一对一对的,value_type(1, "one")),mu ...
 
随机推荐
- VBA_Excel_教程:字典类型
			
VBA中的字典类型需要添加Microsoft Scripting Runtime引用,在Tools菜单下添加 Sub testDic() Dim strV As String Dim key As S ...
 - HTML5学习总结
			
一.HTML5概念 HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web应用开发的一系列标准,成为第一个将Web做为应用开发平台的HTML语言. HTML5定义了一系列新 ...
 - express 快速教程
			
阅读 express 官方文档的记录. hello world example var express = require('express') var app = express() app.get ...
 - javaweb 拦截器报错
			
拦截器报错 The content of element type "interceptor-ref" must match "(param)*".内容元素 ...
 - JUC.Lock(锁机制)学习笔记[附详细源码解析]
			
锁机制学习笔记 目录: CAS的意义 锁的一些基本原理 ReentrantLock的相关代码结构 两个重要的状态 I.AQS的state(int类型,32位) II.Node的waitStatus 获 ...
 - 登陆判读,并跳转到指定页面(window.location.href='http://localhost/index.html')
			
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 functio ...
 - python学习:环境搭建
			
1.图解eclipse环境下安装python3.x插件支持:http://www.tuicool.com/articles/M3Afyu 其中如果 然后,选择Add按钮,Name:Python3,Lo ...
 - vsftp 根据用户设置
			
#vsftpd.conf ###############pam_service_name=vsftpduserlist_enable=YEStcp_wrappers=YESlocal_root=/da ...
 - Python-os
			
os.listdir(path)返回一个list,其中包括该目录下所以文件和文件夹的名字,是str格式.ex.['file_1.ext','folder_name'] file_name, exten ...
 - 一个C#语法高亮插件
			
语法高亮对程序员阅读代码来说有着不小的帮助,虽然VisualStudio本身支持C#语法高亮,但也只是对关键字.类名.字符串等少数元素加了标记,而我们代码中主题:变量.函数.属性.事件等都没有进行高亮 ...