根据二叉树的前序数组和中序数组生成二叉树
来源:互联网 发布:聂卫平与高层关系 知乎 编辑:程序博客网 时间:2024/04/29 07:24
typedef struct NodeType{ int data; struct NodeType* left; struct NodeType* right; NodeType(){left = NULL;right = NULL;};}Node;/** parameter arr_pre, preorder array of tree* parameter size_pre, size of preorder array* parameter arr, order array of tree* parameter size, size of order array** Traverse order array of binary tree to construct a binary tree(遍历中序数组来构建二叉树)*/NodeType* BuildTree(NodeType* arr_pre, int size_pre, NodeType* arr, int size){ if ( 0 == size) return NULL; Node* sub_root = NULL; int root_index_in_arr = 0; bool is_found = false; for (int i = 0;(i < size_pre) && (!is_found);i++) { int data = (arr_pre + i)->data; for (int j = 0;j < size;j++) { if (data == (arr + j)->data) { sub_root = arr + j; root_index_in_arr = j; is_found = true; break; } } } sub_root->left = BuildTree(arr_pre, size_pre, arr, root_index_in_arr); //Left subtree sub_root->right = BuildTree(arr_pre, size_pre, arr + root_index_in_arr + 1, size - root_index_in_arr - 1); //Right subtree return sub_root;}//Traverse in postorder(后续遍历输出)void PrintPostorder(NodeType* root){ if (NULL != root) { PrintPostorder(root->left); PrintPostorder(root->right); cout << (char) root->data << endl; }}void SearchRelatedNodes(NodeType* root, int value, NodeType* parent){ if (NULL != root) { if (root->data == value) { if (NULL != parent) { cout << "Parent is : " << (char) parent->data << endl; } else { cout << "Parent is : NULL" << endl; } if (NULL != root->left) { cout << "left son is : " << (char) root->left->data << endl; } else { cout << "left son is : NULL" << endl; } if (NULL != root->right) { cout << "right son is : " << (char) root->right->data << endl; } else { cout << "right son is : NULL" << endl; } } parent = root; SearchRelatedNodes(root->left, value, parent); SearchRelatedNodes(root->right, value, parent); }}int _tmain(int argc, _TCHAR* argv[]){ NodeType tmp; tmp.data = 'a'; NodeType tmp1; tmp1.data = 'b'; NodeType tmp2; tmp2.data = 'd'; NodeType tmp3; tmp3.data = 'e'; NodeType tmp4; tmp4.data = 'h'; NodeType tmp5; tmp5.data = 'c'; NodeType tmp6; tmp6.data = 'f'; NodeType tmp7; tmp7.data = 'g'; //Preorder: abdehcfg //Order: dbehafcg NodeType pre[8] = {tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7}; //Preorder array(前序遍历数组) NodeType order[8] = {tmp2,tmp1,tmp3,tmp4,tmp,tmp6,tmp5,tmp7}; //Order array(中序遍历数组) NodeType* root = BuildTree(pre, 8, order, 8); PrintPostorder(root); SearchRelatedNodes(root, 'c', NULL);}
0 0
- 根据二叉树的前序数组和中序数组生成二叉树
- 由前序和中序数组重建二叉树
- 二叉树问题---通过先序和中序数组生成后序数组
- 根据前序和中序数组构造二叉树(递归方法)
- 根据后序数组重建搜索二叉树
- 二叉树问题---根据后序数组重建搜索二叉树
- 二叉树问题---先序,中序,和后序数组两两结合重构二叉树
- 通过先序和中序数组生成后序数组
- 通过先序和中序数组生成后序数组
- 先序、中序和后序数组两两结合重构二叉树
- 通过先序、中序和后序数组两两结合重构二叉树
- HDU1710(根据前序和中序生成二叉树)
- 【二叉树】根据二叉树的中序遍历和前序遍历,还原二叉树
- 根据二叉树的前序和中序建树
- 乱序数组和随机数
- 乱序数组的算法
- 根据二叉树的前序遍历和中序遍历的结果,重建二叉树
- POJ 2255 根据二叉树的前序和中序序列来重建二叉树
- 扩展SpringMVC以支持更精准的数据绑定1
- lcx用法之心得总结
- 根据经纬度计算两地的距离
- Linux进程间通信(四)---信号通信之信号发送捕捉kill()、raise()、alarm()、pause()及其基础实验
- java 发送邮件测试例子
- 根据二叉树的前序数组和中序数组生成二叉树
- 特征脸(Eigenface)理论基础-PCA(主成分分析法)
- 应届生白手起家当老板
- JavaScript可折叠弹出式菜单
- 占鳌头。当雪花沁透着
- 3. 函数升级(上)
- Android之SurfaceView学习
- 有关PIM的一点想法
- linux启动tomcat异常:The BASEDIR environment variable is not defined correctly