C++二叉排序树的创建和插入
来源:互联网 发布:中国软件培训 编辑:程序博客网 时间:2024/06/04 23:27
加/**/表示另一种二叉排序树的创建方法
第一类
#include <iostream>using namespace std;class TreeNode {public: int data; TreeNode *LeftChild; TreeNode *RightChild; TreeNode() :LeftChild(NULL), RightChild(NULL) {}};class BiSortTree {private: TreeNode *root; int *info; int size; int loc; TreeNode *CreateTree(); TreeNode* InsertNode(TreeNode *p, int d); TreeNode* Search(TreeNode *p, int d); void Delete(int d);public: BiSortTree(int s, int arr[]); void CreateBiSortTree(); void InsertBiSortTree(int d); void InOder(TreeNode *p); void InOderTree(); void SearchData(int d); void DeleteData(int d);};BiSortTree::BiSortTree(int s, int arr[]) { /* loc = 1; */ loc = 0; count = 0; size = s; info = new int[size]; for (int i = 0; i < size; i++) info[i] = arr[i];}void BiSortTree::CreateBiSortTree() { /* root = new TreeNode(); if (size) root->data = info[0]; CreateTree(root); */ CreateTree();}TreeNode *BiSortTree::CreateTree() { while (loc < size) root = InsertNode(root, info[loc++]); /* if (loc < size) { if (p->data > info[loc]) { if (!p->LeftChild) { p->LeftChild = new TreeNode(); p->LeftChild->data = info[loc++]; } else CreateTree(p->LeftChild); } else if (p->data < info[loc]) { if (!p->RightChild) { p->RightChild = new TreeNode(); p->RightChild->data = info[loc++]; } else CreateTree(p->RightChild); } CreateTree(root); } return NULL; */ return NULL;}void BiSortTree::InOderTree() { InOder(root);}void BiSortTree::InOder(TreeNode *p) { if (p) { InOder(p->LeftChild); cout << p->data << ' '; InOder(p->RightChild); }}TreeNode* BiSortTree::InsertNode(TreeNode *p, int d) { TreeNode *T; if (!p) { T = new TreeNode(); T->data = d; return T; } if (p->data > d) p->LeftChild = InsertNode(p->LeftChild, d); else if (p->data < d) p->RightChild = InsertNode(p->RightChild, d); return p;}void BiSortTree::InsertBiSortTree(int d) { InsertNode(root, d);}int main() { int t; cin >> t; while (t--) { int s; cin >> s; int *arr = new int[s]; for (int i = 0; i < s; i++) cin >> arr[i]; BiSortTree sTree(s, arr); sTree.CreateBiSortTree(); sTree.InOderTree(); cout << endl; int m; cin >> m; //插入节点个数 while (m--) { int d; cin >> d; //数据插入 sTree.InsertBiSortTree(d); sTree.InOderTree(); cout<<endl; } delete arr[]; } //system("pause"); return 0;}
第二类
#include <iostream>using namespace std;class TreeNode {public: int data; TreeNode *LeftChild; TreeNode *RightChild; TreeNode() :LeftChild(NULL), RightChild(NULL) {}};class BiSortTree {private: TreeNode *root; int *info; int size; int loc; int count; TreeNode *CreateTree(TreeNode *p); TreeNode* InsertNode(TreeNode *p, int d); TreeNode* Search(TreeNode *p, int d); void Delete(int d);public: BiSortTree(int s, int arr[]); void CreateBiSortTree(); void InsertBiSortTree(int d); void InOder(TreeNode *p); void InOderTree(); void SearchData(int d); void DeleteData(int d);};BiSortTree::BiSortTree(int s, int arr[]) { loc = 1; count = 0; size = s; info = new int[size]; for (int i = 0; i < size; i++) info[i] = arr[i];}void BiSortTree::CreateBiSortTree() { root = new TreeNode(); if (size) root->data = info[0]; CreateTree(root);}TreeNode *BiSortTree::CreateTree(TreeNode *p) { if (loc < size) { if (p->data > info[loc]) { if (!p->LeftChild) { p->LeftChild = new TreeNode(); p->LeftChild->data = info[loc++]; } else CreateTree(p->LeftChild); } else if (p->data < info[loc]) { if (!p->RightChild) { p->RightChild = new TreeNode(); p->RightChild->data = info[loc++]; } else CreateTree(p->RightChild); } CreateTree(root); } return NULL;}void BiSortTree::InOderTree() { InOder(root);}void BiSortTree::InOder(TreeNode *p) { if (p) { InOder(p->LeftChild); cout << p->data << ' '; InOder(p->RightChild); }}TreeNode* BiSortTree::InsertNode(TreeNode *p, int d) { TreeNode *T; if (!p) { T = new TreeNode(); T->data = d; return T; } if (p->data > d) p->LeftChild = InsertNode(p->LeftChild, d); else if (p->data < d) p->RightChild = InsertNode(p->RightChild, d); return p;}int main() { int t; cin >> t; while (t--) { int s; cin >> s; int *arr = new int[s]; for (int i = 0; i < s; i++) cin >> arr[i]; BiSortTree sTree(s, arr); sTree.CreateBiSortTree(); sTree.InOderTree(); //中序遍历输出有序序列 cout << endl; int m; cin >> m; while (m--) { int d; cin >> d; sTree.InsertBiSortTree(d); sTree.InOderTree(); cout<<endl; } } system("pause");}/*Input1622 33 55 66 11 443775010*//*OutPut11 22 33 44 55 66 11 22 33 44 55 66 77 11 22 33 44 50 55 66 77 10 11 22 33 44 50 55 66 77 */
0 0
- 二叉排序树的创建,插入和 删除操作
- C++二叉排序树的创建和插入
- 二叉排序树的创建,插入,遍历
- 二叉排序树的创建、插入和遍历(代码)
- 二叉排序树的插入和删除
- 七、二叉排序树--(2)二叉排序树的创建/插入/查找/删除
- C语言二叉排序树的创建
- 二叉排序树的创建,查找,插入,删除
- 数据结构 二叉排序树的创建,查找,插入,删除
- 二叉排序树的插入创建与查找
- 二叉排序树的创建和查找
- 二叉排序树的查询、插入和删除操作
- 二叉排序树的查找、插入和删除
- 二叉排序树的查找、插入和删除
- 二叉排序树的插入、遍历和删除
- 二叉排序树的创建(结点的插入,删除等操作)
- 二叉排序树创建、插入节点、查找节点的实现
- 树形结构的查找(二叉排序树-创建、查找、插入、删除)
- sql查询----巧用别名
- 汇编语言 王爽(第三版)实验十六
- 贪心算法——活动安排问题
- 总结38
- 【hadoop学习之一】Hadoop集群 安装配置
- C++二叉排序树的创建和插入
- python list 共享引用
- listener之内存泄漏
- 找鞍点
- 打开和关闭 Oralce PDB数据库
- linux学习--tar命令
- OpenCV摄像头标定(Calibration) 使用源代码
- 当Android遇见工厂模式
- kafka三个节点集群环境搭建-详细步骤