当前位置:   article > 正文

二叉树简介及C++实现_c++创建一个二叉树

c++创建一个二叉树

二叉树是每个结点最多有两个子树的树结构,即结点的度最大为2。通常子树被称作”左子树”和”右子树”。二叉树是一个连通的无环图。

二叉树是递归定义的,其结点有左右子树之分,逻辑上二叉树有五种基本形态:(1)、空二叉树;(2)、只有一个根结点的二叉树;(3)、只有左子树;(4)、只有右子树;(5)、完全二叉树

二叉树类型:

(1)、满二叉树:深度(层数)为k,且有2^k-1个结点的二叉树。这种树的特点是每一层上的结点数都是最大结点数。即除了叶结点外每一个结点都有左右子树且叶节点都处在最低层。

(2)、完全二叉树:除最后一层外,其余层都是满的,并且最后一层或者是满的,或者是在右边缺少连续若干节点,即叶子结点都是从左到右依次排布。具有n个节点的完全二叉树的深度为floor(log(2n))+1。深度为k的完全二叉树,至少有2^(k-1)个结点,至多有(2^k)-1个结点。

(3)、平衡二叉树:又被称为AVL树,它是一颗二叉排序树,且具有以下性质:它是一颗空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一颗平衡二叉树。

(4)、斜树:所有的结点都只有左子树(左斜树),或者只有右子树(右斜树)。

(5)、二叉搜素树(或二叉排序树):特殊的二叉树,每个结点都不比它左子树的任意元素小,而且不比它的右子树的任意元素大。二叉搜索树的左右子树也都是二叉搜索树。按中序遍历,则会得到按升序排列的有序数据集。

二叉树不是树的一种特殊情形。

遍历二叉树:按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。对一颗二叉树的遍历有四种情况:先序遍历、中序遍历、后序遍历、按层遍历。

(1)、先序遍历:先访问根结点,再先序遍历左子树,最后再先序遍历右子树,即先访问根结点-------左子树------右子树。

(2)、中序遍历:先中序遍历左子树,然后再访问根结点,最后再中序遍历右子树,即先访问左子树------根结点------右子树。

(3)、后序遍历:先后序遍历左子树,然后再后序遍历右子树,最后再访问根结点,即先访问左子树------右子树------根结点。

(4)、按层遍历:从上到下,从左到右依次访问结点。

下面代码是二叉搜索树的实现,主要包括树的创建、插入、删除、查找、遍历、保存、载入。

binary_search_tree.hpp:

  1. #ifndef FBC_CPPBASE_TEST_BINARY_SEARCH_TREE_HPP_
  2. #define FBC_CPPBASE_TEST_BINARY_SEARCH_TREE_HPP_
  3. #include <vector>
  4. #include <fstream>
  5. #include <string>
  6. namespace binary_search_tree_ {
  7. typedef struct info {
  8. int id; // suppose id is unique
  9. std::string name;
  10. int age;
  11. std::string addr;
  12. } info;
  13. typedef struct node {
  14. info data;
  15. node* left = nullptr;
  16. node* right = nullptr;
  17. } node;
  18. class BinarySearchTree {
  19. public:
  20. BinarySearchTree() = default;
  21. ~BinarySearchTree() { DeleteTree(tree); }
  22. typedef std::tuple<int, int, std::string, int, std::string> row; // flag(-1: no node, 0: have a node), id, name, age, addr
  23. int Init(const std::vector<info>& infos); // create binary search tree
  24. bool Search(int id, info& data) const;
  25. int Insert(const info& data);
  26. int Delete(int id); // delete a node
  27. int Traversal(int type) const; // 0: pre-order, 1: in-order, 2: post-order, 3: level
  28. int GetMaxDepth() const; // get tree max depth
  29. int GetMinDepth() const; // get tree min depth
  30. int GetNodesCount() const; // get tree node count
  31. bool IsBinarySearchTree() const; // whether or not is a binary search tree
  32. //bool IsBinaryBalanceTree() const; // whether ot not is a binary balance tree
  33. int GetMinValue(info& data) const;
  34. int GetMaxValue(info& data) const;
  35. int SaveTree(const char* name) const; // tree write in txt file
  36. int LoadTree(const char* name);
  37. protected:
  38. void PreorderTraversal(const node* ptr) const;
  39. void InorderTraversal(const node* ptr) const;
  40. void PostorderTraversal(const node* ptr) const;
  41. void LevelTraversal(const node* ptr) const;
  42. void LevelTraversal(const node* ptr, int level) const;
  43. void DeleteTree(node* ptr);
  44. void Insert(node* ptr, const info& data);
  45. const node* Search(const node* ptr, int id) const;
  46. void IsBinarySearchTree(const node* ptr, bool is_bst) const;
  47. int GetNodesCount(const node* ptr) const;
  48. int GetMaxDepth(const node* ptr) const;
  49. int GetMinDepth(const node* ptr) const;
  50. //bool IsBinaryBalanceTree(const node* ptr) const;
  51. node* Delete(node* ptr, int id); // return new root
  52. node* GetMinValue(node* ptr);
  53. void NodeToRow(const node* ptr, std::vector<row>& rows, int pos) const;
  54. void RowToNode(node* ptr, const std::vector<row>& rows, int n, int pos);
  55. private:
  56. node* tree = nullptr;
  57. bool flag;
  58. };
  59. int test_binary_search_tree();
  60. } // namespace binary_search_tree_
  61. #endif // FBC_CPPBASE_TEST_BINARY_SEARCH_TREE_HPP_

binary_search_tree.cpp:

  1. #include "binary_search_tree.hpp"
  2. #include <set>
  3. #include <iostream>
  4. #include <limits>
  5. #include <tuple>
  6. #include <string>
  7. #include <sstream>
  8. #include <string.h>
  9. #include <algorithm>
  10. namespace binary_search_tree_ {
  11. int BinarySearchTree::Init(const std::vector<info>& infos)
  12. {
  13. std::vector<int> ids;
  14. for (const auto& info : infos) {
  15. ids.emplace_back(info.id);
  16. }
  17. std::set<int> id_set(ids.cbegin(), ids.cend());
  18. if (id_set.size() != ids.size()) {
  19. fprintf(stderr, "id must be unique\n");
  20. return -1;
  21. }
  22. for (const auto& info : infos) {
  23. Insert(info);
  24. }
  25. return 0;
  26. }
  27. bool BinarySearchTree::Search(int id, info& data) const
  28. {
  29. const node* root = tree;
  30. const node* tmp = Search(root, id);
  31. if (tmp) {
  32. data = tmp->data;
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. const node* BinarySearchTree::Search(const node* ptr, int id) const
  39. {
  40. if (ptr) {
  41. if (ptr->data.id == id) {
  42. return ptr;
  43. } else if (ptr->data.id > id) {
  44. return Search(ptr->left, id);
  45. } else {
  46. return Search(ptr->right, id);
  47. }
  48. } else {
  49. return nullptr;
  50. }
  51. }
  52. int BinarySearchTree::Insert(const info& data)
  53. {
  54. flag = true;
  55. if (tree) {
  56. Insert(tree, data);
  57. } else {
  58. tree = new node;
  59. tree->data = data;
  60. tree->left = nullptr;
  61. tree->right = nullptr;
  62. }
  63. return (int)flag;
  64. }
  65. void BinarySearchTree::Insert(node* ptr, const info& data)
  66. {
  67. if (ptr->data.id == data.id) {
  68. flag = false;
  69. return;
  70. }
  71. if (ptr->data.id < data.id) {
  72. if (ptr->right) {
  73. Insert(ptr->right, data);
  74. } else {
  75. ptr->right = new node;
  76. ptr->right->data = data;
  77. ptr->right->left = nullptr;
  78. ptr->right->right = nullptr;
  79. }
  80. } else {
  81. if (ptr->left) {
  82. Insert(ptr->left, data);
  83. } else {
  84. ptr->left = new node;
  85. ptr->left->data = data;
  86. ptr->left->left = nullptr;
  87. ptr->left->right = nullptr;
  88. }
  89. }
  90. }
  91. bool BinarySearchTree::IsBinarySearchTree() const
  92. {
  93. bool is_bst = true;
  94. const node* root = tree;
  95. IsBinarySearchTree(root, is_bst);
  96. return is_bst;
  97. }
  98. void BinarySearchTree::IsBinarySearchTree(const node* ptr, bool is_bst) const
  99. {
  100. static int last_data = std::numeric_limits<int>::min();
  101. if (!ptr) return;
  102. IsBinarySearchTree(ptr->left, is_bst);
  103. if (last_data < ptr->data.id) last_data = ptr->data.id;
  104. else {
  105. is_bst = false;
  106. return;
  107. }
  108. IsBinarySearchTree(ptr->right, is_bst);
  109. }
  110. int BinarySearchTree::Traversal(int type) const
  111. {
  112. if (!tree) {
  113. fprintf(stderr, "Error: it is an empty tree\n");
  114. return -1;
  115. }
  116. const node* root = tree;
  117. if (type == 0)
  118. PreorderTraversal(root);
  119. else if (type == 1)
  120. InorderTraversal(root);
  121. else if (type == 2)
  122. PostorderTraversal(root);
  123. else if (type == 3)
  124. LevelTraversal(root);
  125. else {
  126. fprintf(stderr, "Error: don't suppot traversal type, type only support: 0: pre-order, 1: in-order, 2: post-order\n");
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. void BinarySearchTree::PreorderTraversal(const node* ptr) const
  132. {
  133. if (ptr) {
  134. fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",
  135. ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());
  136. PreorderTraversal(ptr->left);
  137. PreorderTraversal(ptr->right);
  138. }
  139. }
  140. void BinarySearchTree::InorderTraversal(const node* ptr) const
  141. {
  142. if (ptr) {
  143. InorderTraversal(ptr->left);
  144. fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",
  145. ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());
  146. InorderTraversal(ptr->right);
  147. }
  148. }
  149. void BinarySearchTree::PostorderTraversal(const node* ptr) const
  150. {
  151. if (ptr) {
  152. PostorderTraversal(ptr->left);
  153. PostorderTraversal(ptr->right);
  154. fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",
  155. ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());
  156. }
  157. }
  158. void BinarySearchTree::LevelTraversal(const node* ptr) const
  159. {
  160. int h = GetMaxDepth();
  161. for (int i = 1; i <= h; ++i)
  162. LevelTraversal(ptr, i);
  163. }
  164. void BinarySearchTree::LevelTraversal(const node* ptr, int level) const
  165. {
  166. if (!ptr) return;
  167. if (level == 1)
  168. fprintf(stdout, "Info: id: %d, name: %s, age: %d, addr: %s\n",
  169. ptr->data.id, ptr->data.name.c_str(), ptr->data.age, ptr->data.addr.c_str());
  170. else if (level > 1) {
  171. LevelTraversal(ptr->left, level-1);
  172. LevelTraversal(ptr->right, level-1);
  173. }
  174. }
  175. void BinarySearchTree::DeleteTree(node* ptr)
  176. {
  177. if (ptr) {
  178. DeleteTree(ptr->left);
  179. DeleteTree(ptr->right);
  180. delete ptr;
  181. }
  182. }
  183. int BinarySearchTree::GetNodesCount() const
  184. {
  185. const node* root = tree;
  186. return GetNodesCount(root);
  187. }
  188. int BinarySearchTree::GetNodesCount(const node* ptr) const
  189. {
  190. if (!ptr) return 0;
  191. else return GetNodesCount(ptr->left) + 1 + GetNodesCount(ptr->right);
  192. }
  193. int BinarySearchTree::GetMaxDepth() const
  194. {
  195. const node* root = tree;
  196. return GetMaxDepth(root);
  197. }
  198. int BinarySearchTree::GetMaxDepth(const node* ptr) const
  199. {
  200. if (!ptr) return 0;
  201. int left_depth = GetMaxDepth(ptr->left);
  202. int right_depth = GetMaxDepth(ptr->right);
  203. return std::max(left_depth, right_depth) + 1;
  204. }
  205. int BinarySearchTree::GetMinDepth() const
  206. {
  207. const node* root = tree;
  208. return GetMinDepth(root);
  209. }
  210. int BinarySearchTree::GetMinDepth(const node* ptr) const
  211. {
  212. if (!ptr) return 0;
  213. int left_depth = GetMaxDepth(ptr->left);
  214. int right_depth = GetMaxDepth(ptr->right);
  215. return std::min(left_depth, right_depth) + 1;
  216. }
  217. /*bool BinarySearchTree::IsBinaryBalanceTree() const
  218. {
  219. const node* root = tree;
  220. return IsBinaryBalanceTree(root);
  221. }
  222. bool BinarySearchTree::IsBinaryBalanceTree(const node* ptr) const
  223. {
  224. // TODO: code need to modify
  225. if (GetMaxDepth(ptr) - GetMinDepth(ptr) <= 1) return true;
  226. else return false;
  227. }*/
  228. int BinarySearchTree::GetMinValue(info& data) const
  229. {
  230. if (!tree) {
  231. fprintf(stderr, "Error: it is a empty tree\n");
  232. return -1;
  233. }
  234. const node* root = tree;
  235. while (root->left) root = root->left;
  236. data = root->data;
  237. return 0;
  238. }
  239. int BinarySearchTree::GetMaxValue(info& data) const
  240. {
  241. if (!tree) {
  242. fprintf(stderr, "Error: it is a empty tree\n");
  243. return -1;
  244. }
  245. const node* root = tree;
  246. while (root->right) root = root->right;
  247. data = root->data;
  248. return 0;
  249. }
  250. int BinarySearchTree::Delete(int id)
  251. {
  252. if (!tree) {
  253. fprintf(stderr, "Error: it is a empty tree\n");
  254. return -1;
  255. }
  256. const node* root = tree;
  257. const node* ret = Search(root, id);
  258. if (!ret) {
  259. fprintf(stdout, "Warning: this id don't exist in the tree: %d", id);
  260. return 0;
  261. }
  262. tree = Delete(tree, id);
  263. return 0;
  264. }
  265. node* BinarySearchTree::GetMinValue(node* ptr)
  266. {
  267. node* tmp = ptr;
  268. while (tmp->left) tmp = tmp->left;
  269. return tmp;
  270. }
  271. node* BinarySearchTree::Delete(node* ptr, int id)
  272. {
  273. if (!ptr) return ptr;
  274. if (id < ptr->data.id)
  275. ptr->left = Delete(ptr->left, id);
  276. else if (id > ptr->data.id)
  277. ptr->right = Delete(ptr->right, id);
  278. else {
  279. if (!ptr->left) {
  280. node* tmp = ptr->right;
  281. delete ptr;
  282. return tmp;
  283. } else if (!ptr->right) {
  284. node* tmp = ptr->left;
  285. delete ptr;
  286. return tmp;
  287. }
  288. node* tmp = GetMinValue(ptr->right);
  289. ptr->data = tmp->data;
  290. ptr->right = Delete(ptr->right, tmp->data.id);
  291. }
  292. return ptr;
  293. }
  294. int BinarySearchTree::SaveTree(const char* name) const
  295. {
  296. std::ofstream file(name, std::ios::out);
  297. if (!file.is_open()) {
  298. fprintf(stderr, "Error: open file fail: %s\n", name);
  299. return -1;
  300. }
  301. const node* root = tree;
  302. int max_depth = GetMaxDepth(root);
  303. int max_nodes = (1 << max_depth) -1;
  304. root = tree;
  305. int nodes_count = GetNodesCount(root);
  306. //fprintf(stdout, "max_depth: %d, max nodes: %d\n", max_depth, max_nodes);
  307. file<<nodes_count<<","<<max_depth<<std::endl;
  308. std::vector<row> vec(max_nodes, std::make_tuple(-1, -1, " ", -1, " "));
  309. root = tree;
  310. NodeToRow(root, vec, 0);
  311. for (const auto& v : vec) {
  312. file << std::get<0>(v)<<","<<std::get<1>(v)<<","<<std::get<2>(v)<<","<<std::get<3>(v)<<","<<std::get<4>(v)<<std::endl;
  313. }
  314. file.close();
  315. return 0;
  316. }
  317. void BinarySearchTree::NodeToRow(const node* ptr, std::vector<row>& rows, int pos) const
  318. {
  319. if (!ptr) return;
  320. rows[pos] = std::make_tuple(0, ptr->data.id, ptr->data.name, ptr->data.age, ptr->data.addr);
  321. if (ptr->left) NodeToRow(ptr->left, rows, 2 * pos + 1);
  322. if (ptr->right) NodeToRow(ptr->right, rows, 2 * pos + 2);
  323. }
  324. int BinarySearchTree::LoadTree(const char* name)
  325. {
  326. std::ifstream file(name, std::ios::in);
  327. if (!file.is_open()) {
  328. fprintf(stderr, "Error: open file fail: %s\n", name);
  329. return -1;
  330. }
  331. std::string line, cell;
  332. std::getline(file, line);
  333. std::stringstream line_stream(line);
  334. std::vector<int> vec;
  335. while (std::getline(line_stream, cell, ',')) {
  336. vec.emplace_back(std::stoi(cell));
  337. }
  338. if (vec.size() != 2) {
  339. fprintf(stderr, "Error: parse txt file fail\n");
  340. return -1;
  341. }
  342. fprintf(stdout, "nodes count: %d, max depth: %d\n", vec[0], vec[1]);
  343. int max_nodes = (1 << vec[1]) - 1;
  344. std::vector<row> rows;
  345. while (std::getline(file, line)) {
  346. std::stringstream line_stream2(line);
  347. std::vector<std::string> strs;
  348. while (std::getline(line_stream2, cell, ',')) {
  349. strs.emplace_back(cell);
  350. }
  351. if (strs.size() != 5) {
  352. fprintf(stderr, "Error: parse line fail\n");
  353. return -1;
  354. }
  355. row tmp = std::make_tuple(std::stoi(strs[0]), std::stoi(strs[1]), strs[2], std::stoi(strs[3]), strs[4]);
  356. rows.emplace_back(tmp);
  357. }
  358. if (rows.size() != max_nodes || std::get<0>(rows[0]) == -1) {
  359. fprintf(stderr, "Error: parse txt file line fail\n");
  360. return -1;
  361. }
  362. node* root = new node;
  363. root->data = {std::get<1>(rows[0]), std::get<2>(rows[0]), std::get<3>(rows[0]), std::get<4>(rows[0])};
  364. root->left = nullptr;
  365. root->right = nullptr;
  366. tree = root;
  367. RowToNode(root, rows, max_nodes, 0);
  368. file.close();
  369. return 0;
  370. }
  371. void BinarySearchTree::RowToNode(node* ptr, const std::vector<row>& rows, int n, int pos)
  372. {
  373. if (!ptr || n == 0) return;
  374. int new_pos = 2 * pos + 1;
  375. if (new_pos < n && std::get<0>(rows[new_pos]) != -1) {
  376. ptr->left = new node;
  377. ptr->left->data = {std::get<1>(rows[new_pos]), std::get<2>(rows[new_pos]), std::get<3>(rows[new_pos]), std::get<4>(rows[new_pos])};
  378. ptr->left->left = nullptr;
  379. ptr->left->right = nullptr;
  380. RowToNode(ptr->left, rows, n, new_pos);
  381. }
  382. new_pos = 2 * pos + 2;
  383. if (new_pos < n && std::get<0>(rows[new_pos]) != -1) {
  384. ptr->right = new node;
  385. ptr->right->data = {std::get<1>(rows[new_pos]), std::get<2>(rows[new_pos]), std::get<3>(rows[new_pos]), std::get<4>(rows[new_pos])};
  386. ptr->right->left = nullptr;
  387. ptr->right->right = nullptr;
  388. RowToNode(ptr->right, rows, n, new_pos);
  389. }
  390. }
  391. int test_binary_search_tree()
  392. {
  393. fprintf(stdout, "create binary search tree:\n");
  394. std::vector<info> infos{{1004, "Tom", 8, "Beijing"}, {1005, "Jack", 9, "Tianjin"}, {1003, "Mark", 6, "Hebei"}, {1009, "Lisa", 11, "Beijiing"}, {1007, "Piter", 4, "Hebei"}, {1001, "Viner", 6, "Beijing"}};
  395. BinarySearchTree bstree;
  396. bstree.Init(infos);
  397. fprintf(stdout, "\ninsert operation:\n");
  398. std::vector<info> infos2{{1007, "xxx", 11, "yyy"}, {1008, "Lorena", 22, "Hebie"}, {1002, "Eillen", 14, "Shanxi"}};
  399. for (const auto& info : infos2) {
  400. int flag = bstree.Insert(info);
  401. if (flag) fprintf(stdout, "insert success\n");
  402. else fprintf(stdout, "Warning: id %d already exists, no need to insert\n", info.id);
  403. }
  404. fprintf(stdout, "\ntraversal operation:\n");
  405. fprintf(stdout, "pre-order traversal:\n");
  406. bstree.Traversal(0);
  407. fprintf(stdout, "in-order traversal:\n");
  408. bstree.Traversal(1);
  409. fprintf(stdout, "post-order traversal:\n");
  410. bstree.Traversal(2);
  411. fprintf(stdout, "level traversal:\n");
  412. bstree.Traversal(3);
  413. fprintf(stdout, "\nsearch operation:\n");
  414. std::vector<int> ids {1009, 2000};
  415. for (auto id : ids) {
  416. info ret;
  417. bool flag = bstree.Search(id, ret);
  418. if (flag)
  419. fprintf(stdout, "found: info: %d, %s, %d, %s\n", ret.id, ret.name.c_str(), ret.age, ret.addr.c_str());
  420. else
  421. fprintf(stdout, "no find: no id info: %d\n", id);
  422. }
  423. fprintf(stdout, "\nwhether or not is a binary search tree operation:\n");
  424. bool flag2 = bstree.IsBinarySearchTree();
  425. if (flag2) fprintf(stdout, "it is a binary search tree\n");
  426. else fprintf(stdout, "it is not a binary search tree\n");
  427. fprintf(stdout, "\ncalculate node count operation:\n");
  428. int count = bstree.GetNodesCount();
  429. fprintf(stdout, "tree node count: %d\n", count);
  430. fprintf(stdout, "\ncalculate tree depth operation:\n");
  431. int max_depth = bstree.GetMaxDepth();
  432. int min_depth = bstree.GetMinDepth();
  433. fprintf(stdout, "tree max depth: %d, min depth: %d\n", max_depth, min_depth);
  434. /*fprintf(stdout, "\nwhether or not is a binary balance tree operation:\n");
  435. flag2 = bstree.IsBinaryBalanceTree();
  436. if (flag2) fprintf(stdout, "it is a binary balance tree\n");
  437. else fprintf(stdout, "it is not a binary balance tree\n");*/
  438. fprintf(stdout, "\nget min and max value(id):\n");
  439. info value;
  440. bstree.GetMinValue(value);
  441. fprintf(stdout, "tree min value: id: %d\n", value.id);
  442. bstree.GetMaxValue(value);
  443. fprintf(stdout, "tree max value: id: %d\n", value.id);
  444. fprintf(stdout, "\ndelete node operation:\n");
  445. bstree.Delete(1005);
  446. bstree.Traversal(1);
  447. fprintf(stdout, "\nsave tree operation:\n");
  448. #ifdef _MSC_VER
  449. char* name = "E:/GitCode/Messy_Test/testdata/binary_search_tree.model";
  450. #else
  451. char* name = "testdata/binary_search_tree.model";
  452. #endif
  453. bstree.SaveTree(name);
  454. fprintf(stdout, "\nload tree operation:\n");
  455. BinarySearchTree bstree2;
  456. bstree2.LoadTree(name);
  457. int count2 = bstree2.GetNodesCount();
  458. int max_depth2 = bstree2.GetMaxDepth();
  459. int min_depth2 = bstree2.GetMinDepth();
  460. fprintf(stdout, "tree node count: %d, tree max depth: %d, min depth: %d\n", count2, max_depth2, min_depth2);
  461. bstree2.Traversal(1);
  462. return 0;
  463. }
  464. } // namespace binary_search_tree_

支持Linux和Windows直接编译,Windows通过VS,linux下执行prj/linux_cmake_CppBaseTest/build.sh脚本。执行结果如下:

保存的binary_search_tree.model结果如下:

  1. 7,4
  2. 0,1004,Tom,8,Beijing
  3. 0,1003,Mark,6,Hebei
  4. 0,1009,Lisa,11,Beijiing
  5. 0,1001,Viner,6,Beijing
  6. -1,-1, ,-1,
  7. 0,1007,Piter,4,Hebei
  8. -1,-1, ,-1,
  9. -1,-1, ,-1,
  10. 0,1002,Eillen,14,Shanxi
  11. -1,-1, ,-1,
  12. -1,-1, ,-1,
  13. -1,-1, ,-1,
  14. 0,1008,Lorena,22,Hebie
  15. -1,-1, ,-1,
  16. -1,-1, ,-1,

GitHub: https://github.com/fengbingchun/Messy_Test  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/245935
推荐阅读
相关标签
  

闽ICP备14008679号