题目(好像以前没加)
二叉树与哈希表
作业
1.二叉树前序遍历结果
-
二叉树结构为
-
代码实现中序+后序推理前序表达式
#include <iostream> #include <stack> #include <string> #include <vector> #include <deque> // BDCEAFHG std::deque<char> mid_order = {'B','D','C','E','A','F','H','G'}; // DECBHGFA std::deque<char> back_order = {'D','E','C','B','H','G','F','A'}; std::string mid = "BDCEAFHG"; std::string back = "DECBHGFA"; void get_res(std::string mid, std::string back) { if(mid.empty()) { return; } char root = back.back(); std::cout <<root; int root_index = mid.find(root),length = mid.length(); // mid , left is 0-ind, right is ind-end // back, left is 0-ind, right is ind-end-1 // left sub tree get_res(mid.substr(0,root_index),back.substr(0,root_index)); // right subtree // get_res(mid.substr(root_index+1,length-1),back.substr(root_index,length-1)); get_res(mid.substr(root_index+1,length-1),back.substr(root_index,length-root_index-1)); } int main() { get_res(mid,back); return 0; }
-
运行结果
ABCDEFGH(符合推导的二叉树结构)
2.将多叉树转二叉树
3.线性哈希表
H(k)=INT(k/13),序列为(08, 14,23,30,68,92,42,55,76,10)
目录