c++ tree data structure
//tree.cpp #pragma once #include <iostream> class node { public: int data; node *left; node *right; node(int val) { data = val; left = NULL; right = NULL; } node *insert(node *root, int val) { // if (root == NULL) { return get_new_node(val); } else if (root->data < val) { root->right = insert(root->right, val); } else if (root->data > val) { root->left = insert(root->left, val); } return root; } node *get_new_node(int val) { node *new_node = new node(val); new_node->data = val; new_node->left = NULL; new_node->right = NULL; return new_node; } node *search(node *root, int key) { if (root == NULL || root->data == key) { return root; } else if (root->data < key) { return search(root->right, key); } return search(root->left, key); } void traverse_pre_order(node *node) { if (node != NULL) { std::cout << node->data << "\t"; traverse_pre_order(node->left); traverse_pre_order(node->right); } } void traverse_in_order(node *node) { if (node != NULL) { traverse_in_order(node->left); std::cout << "\t" << node->data; traverse_in_order(node->right); } } void traverse_post_order(node *node) { if (node != NULL) { traverse_post_order(node->left); traverse_post_order(node->right); std::cout << "\t" << node->data; } } }; //main.cpp #include <atomic> #include <chrono> #include <cmath> #include <cstddef> #include <forward_list> #include <functional> #include <future> #include <iomanip> #include <iostream> #include <latch> #include <list> #include <map> #include <mutex> #include <queue> #include <random> #include <stack> #include <string> #include <thread> #include <utility> #include <uuid/uuid.h> #include <vector> #include "tree.cpp" char *uuid_value = (char *)malloc(40); char *get_uuid_value() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } std::string get_time_now(bool is_exact = false) { std::stringstream ss; std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now(); time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now); struct tm tm_info = *localtime(&raw_time); ss << std::put_time(&tm_info, "%Y%m%d%H%M%S"); if (is_exact) { std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); std::chrono::microseconds micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); std::chrono::nanoseconds nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()); ss << "_" << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000) << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000) << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000); } return ss.str(); } void tree_demo() { node *root=new node(1); root->left=new node(2); root->right=new node(3); root->left->left=new node(4); std::cout<<"PreOrder traversal: "; root->traverse_pre_order(root); std::cout<<std::endl; std::cout<<"Inorder traversal: "; root->traverse_in_order(root); std::cout<<std::endl; std::cout<<"PostOrder traversal: "; root->traverse_post_order(root); std::cout<<std::endl; std::cout<<"Finish in "<<__FUNCTION__<<std::endl; } int main(int args, char **argv) { tree_demo(); std::cout << get_time_now(true) << ",thread id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl; }
Compile
g++-12 -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2020-06-05 C# deflatestream compress and decompress, compare their respective md5
2020-06-05 C# BinaryWriter BinaryReader demo
2019-06-05 C# NPOI Export DataTable C# NPOI导出DataTable 单元格自适应大小