当前位置: 首页 > news >正文

海南网站设计公司音频网站建设

海南网站设计公司,音频网站建设,云南网络推广公司,wordpress autop0x01 实验目的 掌握二叉树的基本概念#xff0c;二叉树的存储结构使用链表。 0x02 实验内容 输入一个完全二叉树的层次遍历字符串#xff0c;创建这个二叉树#xff0c;输出这个二叉树的前序遍历字符串、中序遍历字符串、后序遍历字符串、结点数目、二叉树高度(上述每一个…0x01 实验目的 掌握二叉树的基本概念二叉树的存储结构使用链表。 0x02 实验内容 输入一个完全二叉树的层次遍历字符串创建这个二叉树输出这个二叉树的前序遍历字符串、中序遍历字符串、后序遍历字符串、结点数目、二叉树高度(上述每一个结果独立一行显示)。输入二叉树前序序列和中序序列(各元素各不相同)创建这个二叉树输出该二叉树的后序序列、层次遍历。 0x03 实验过程 层次遍历 判断队列是否为空的条件必须加上因为队列为空时有可能会通过q.front()获取到一些非NULL的奇怪的东西然后获取其element时会报错。 void levelOrder(binaryTreeNodeE* node) {queuebinaryTreeNodeE* q;while (node ! NULL) {if(Level 0) {Level;coutnode-element;if(node-leftChild ! NULL) q.push(node-leftChild);if(node-rightChild ! NULL) q.push(node-rightChild);if(!q.empty()) {node q.front();q.pop();}else node NULL; } else {cout,node-element;if(node-leftChild ! NULL) q.push(node-leftChild);if(node-rightChild ! NULL) q.push(node-rightChild);if(!q.empty()) {node q.front();q.pop();}else node NULL; }}}层次遍历构建二叉树 先将第一个节点放入队列之后树上每添加一个节点就要将该节点入队列。先进先出的进行添加节点操作直到node数组空了。 template class E binaryTreeNodeE* constructTreeByLevelOrder(binaryTreeNodeE node[],int num) {queuebinaryTreeNodeE* q;q.push(node[0]);int i 1;while(i num) {binaryTreeNodeE* current q.front();q.pop();if(i num) {current-leftChild node[i];q.push(node[i]);}i;if(i num) {current-rightChild node[i];q.push(node[i]);}i;}return node[0]; }前序遍历和中序遍历构建二叉树 采用递归地形式依次构建左子树和右子树。利用前序遍历的的第一个节点是头节点以及在中序遍历中头结点的两侧分别为左子树和右子树这两个性质。 templateclass E binaryTreeNodeE* create(binaryTreeNodeE preorder[], int p, int q, binaryTreeNodeE inorder[], int i, int j) {if (p q) return nullptr;if (p q) return preorder[p];int k i;// 找到根节点在中序遍历序列中的位置while (preorder[p].element ! inorder[k].element) k;preorder[p].leftChild create(preorder, p1, pk, inorder, i, k-1);preorder[p].rightChild create(preorder, pk1, q, inorder, k1, j);return preorder[p]; }改BUG 0x04 实验源码 #includebits/stdc.h using namespace std; int in 0; int post 0; int Level 0; templateclass T class binaryTreeNode {public:T element;binaryTreeNodeT *leftChild,*rightChild;binaryTreeNode() {leftChild rightChild NULL;}binaryTreeNode(const T theElement) : element(theElement) {leftChild rightChild NULL;}binaryTreeNode(const T theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild):element(theElement) {leftChild theLeftChild;rightChild theRightChild;} }; templateclass E class linkedBinaryTree {public:linkedBinaryTree() {root NULL;treeSize 0;}bool empty() const {return treeSize 0;}int size() const {return treeSize;}void setSize(int size) {treeSize size;}void preOrder(binaryTreeNodeE* node,int level) {if(node ! NULL) {if(level 0) {coutnode-element;preOrder(node-leftChild, level);preOrder(node-rightChild, level);} else {cout,node-element;preOrder(node-leftChild, level);preOrder(node-rightChild, level);}}}void inOrder(binaryTreeNodeE* node) {if(node ! NULL) {if(node-leftChild NULL in 0) {coutnode-element;in;} else {inOrder(node-leftChild);cout,node-element;inOrder(node-rightChild);}}}void postOrder(binaryTreeNodeE* node) {if(node ! NULL) {if(node-leftChild NULL post 0) {coutnode-element;post;} else {postOrder(node-leftChild);postOrder(node-rightChild);cout,node-element;}}}void levelOrder(binaryTreeNodeE* node) {queuebinaryTreeNodeE* q;while (node ! NULL) {if(Level 0) {Level;coutnode-element;if(node-leftChild ! NULL) q.push(node-leftChild);if(node-rightChild ! NULL) q.push(node-rightChild);if(!q.empty()) {node q.front();q.pop();}else node NULL; } else {cout,node-element;if(node-leftChild ! NULL) q.push(node-leftChild);if(node-rightChild ! NULL) q.push(node-rightChild);if(!q.empty()) {node q.front();q.pop();}else node NULL; }}}int getHeight(binaryTreeNodeE* node) {if(node NULL) return 0;int h1 getHeight(node-leftChild);int h2 getHeight(node-rightChild);if(h1 h2) {return h1;} else {return h2;}}private:binaryTreeNodeE *root;int treeSize; }; template class E binaryTreeNodeE* constructTreeByLevelOrder(binaryTreeNodeE node[],int num) {queuebinaryTreeNodeE* q;q.push(node[0]);int i 1;while(i num) { // coutq.front()q.front()endl; // coutq.front()q.front()endl; // coutnode[0]node[0]endl; // coutnode[0]nodeendl;binaryTreeNodeE* current q.front();q.pop();if(i num) {current-leftChild node[i]; // coutcurrent-leftChild-elementendl; // coutnode[0].leftChild-elementendl;q.push(node[i]);}i;if(i num) {current-rightChild node[i];q.push(node[i]);}i;}return node[0]; } templateclass E binaryTreeNodeE* create(binaryTreeNodeE preorder[], int p, int q, binaryTreeNodeE inorder[], int i, int j) {if (p q) return nullptr;if (p q) return preorder[p];int k i;// 找到根节点在中序遍历序列中的位置while (preorder[p].element ! inorder[k].element) k;preorder[p].leftChild create(preorder, p1, pk, inorder, i, k-1);preorder[p].rightChild create(preorder, pk1, q, inorder, k1, j);return preorder[p]; } int main() {//两种写法都可以。//binaryTreeNodechar *node new binaryTreeNodechar[100];binaryTreeNodechar node[100];string s;coutInput1endl;cins;for(int i 0; i s.length(); i) {binaryTreeNodechar n(s.at(i));node[i] n;}linkedBinaryTreechar tree;constructTreeByLevelOrder(node,s.length());tree.setSize(s.length()); // node[0].leftChild node[1]; // node[0].rightChild node[2]; // coutpreOrderendl; // 记录层数方便输出逗号int level 0;coutOutput1endl; // coutnode[0].elementendl; // coutnode[0].leftChildendl;tree.preOrder(node[0],level);coutendl;tree.inOrder(node[0]);in 0;coutendl;tree.postOrder(node[0]);post 0;coutendl;couttree.size()endl;couttree.getHeight(node[0])endl;// tree.constructTreeByLevelOrder(node,length); // tree.inOrder(); // cout2node[0].elementnode[0].leftChild-element;coutInput2endl;string s1,s2;cins1s2;binaryTreeNodechar node1[100];binaryTreeNodechar node2[100];for(int i 0; i s1.length(); i) {binaryTreeNodechar n(s1.at(i));node1[i] n;}int noting;for(int i 0; i s2.length(); i) {binaryTreeNodechar n(s2.at(i));node2[i] n;if(s2.at(i) s1.at(0)) noting i;}//constructTreeByPreOrderAndInOrder(node1, node2, s1.length(), s2.length(), noting);coutOutput2endl;create(node1, 0, s1.length()-1, node2, 0, s2.length()-1); // tree.preOrder(node1[0],level); // coutendl; // tree.inOrder(node1[0]); // coutendl;tree.postOrder(node1[0]);coutendl;tree.levelOrder(node1[0]);coutendl;coutEndendl;return 0; }0x05 错误代码 #includebits/stdc.h using namespace std; int in 0; int post 0; int Level 0; templateclass T class binaryTreeNode {public:T element;binaryTreeNodeT *leftChild,*rightChild;binaryTreeNode() {leftChild rightChild NULL;}binaryTreeNode(const T theElement) : element(theElement) {leftChild rightChild NULL;}binaryTreeNode(const T theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild):element(theElement) {leftChild theLeftChild;rightChild theRightChild;} }; templateclass E class linkedBinaryTree {public:linkedBinaryTree() {root NULL;treeSize 0;}bool empty() const {return treeSize 0;}int size() const {return treeSize;}void setSize(int size) {treeSize size;}void preOrder(binaryTreeNodeE* node,int level) {if(node ! NULL) {if(level 0) {coutnode-element;preOrder(node-leftChild, level);preOrder(node-rightChild, level);} else {cout,node-element;preOrder(node-leftChild, level);preOrder(node-rightChild, level);}}}void inOrder(binaryTreeNodeE* node) {if(node ! NULL) {if(node-leftChild NULL in 0) {coutnode-element;in;} else {inOrder(node-leftChild);cout,node-element;inOrder(node-rightChild);}}}void postOrder(binaryTreeNodeE* node) {if(node ! NULL) {if(node-leftChild NULL post 0) {coutnode-element;post;} else {postOrder(node-leftChild);postOrder(node-rightChild);cout,node-element;}}}void levelOrder(binaryTreeNodeE* node) {queuebinaryTreeNodeE* q;while (node ! NULL) {if(Level 0) {Level;coutnode-element;if(node-leftChild ! NULL) q.push(node-leftChild);if(node-rightChild ! NULL) q.push(node-rightChild);node q.front();q.pop();} else {cout,node-element;if(node-leftChild ! NULL) q.push(node-leftChild);if(node-rightChild ! NULL) q.push(node-rightChild);node q.front();q.pop();}}}int getHeight(binaryTreeNodeE* node) {if(node NULL) return 0;int h1 getHeight(node-leftChild);int h2 getHeight(node-rightChild);if(h1 h2) {return h1;} else {return h2;}}private:binaryTreeNodeE *root;int treeSize; }; template class E binaryTreeNodeE* constructTreeByLevelOrder(binaryTreeNodeE node[],int num) {queuebinaryTreeNodeE* q;q.push(node[0]);int i 1;while(i num) { // coutq.front()q.front()endl; // coutq.front()q.front()endl; // coutnode[0]node[0]endl; // coutnode[0]nodeendl;binaryTreeNodeE* current q.front();q.pop();if(i num) {current-leftChild node[i]; // coutcurrent-leftChild-elementendl; // coutnode[0].leftChild-elementendl;q.push(node[i]);}i;if(i num) {current-rightChild node[i];q.push(node[i]);}i;}return node[0]; } template class E //左子树的实现 binaryTreeNodeE* soul(int noting, binaryTreeNodeE node1[], binaryTreeNodeE node2[]) {int index1 100;int index2 100;for(int i 1; i noting; i) {for(int j noting; j 0; j--) {if(node1[i].element node2[j].element) {index1 index2;index2 j;coutindex1index1endl;coutindex2index2endl;}}if(index1 index2) {node1[i-1].leftChild node1[i];} else { // coutYesendl;for(int j noting; j 0; j--) {if(node1[i].element node2[j].element) {for(int k noting; k 0; k--) {if(node2[j-1].element node1[k].element) {node1[k].rightChild node1[i];}}}}}}return node1[0]; } template class E binaryTreeNodeE* constructTreeByPreOrderAndInOrder(binaryTreeNodeE node1[], binaryTreeNodeE node2[], int num1, int num2, int noting) {soul(noting, node1, node2);int temp;//获取下一个左子树区间 for(int i noting 1; i num1; i) {if(node2[i].element node1[noting1].element) {temp noting;noting i;}}do {//重复操作直到剩下最后一个元素或者不剩下元素退出循环 int num 0;binaryTreeNodechar node3[100];binaryTreeNodechar node4[100];for(int i temp 1; i noting; i) {node3[num] node1[i];node4[num] node2[i];num;}soul(noting, node3, node4);for(int i noting 1; i num1; i) {if(node2[i].element node1[noting1].element) {temp noting;noting i;}}} while(noting 1 ! num1 - 1 noting 1 ! num1);//如果剩一个元素 if(noting 1 num1 - 1) node2[num1 - 1].rightChild node2[num1];return node1[0]; } int main() {//binaryTreeNodechar *node new binaryTreeNodechar[100];binaryTreeNodechar node[100];string s;coutInputendl;cins;for(int i 0; i s.length(); i) {binaryTreeNodechar n(s.at(i));node[i] n;}linkedBinaryTreechar tree;constructTreeByLevelOrder(node,s.length());tree.setSize(s.length()); // node[0].leftChild node[1]; // node[0].rightChild node[2]; // coutpreOrderendl; // 记录层数方便输出逗号int level 0;coutOutputendl; // coutnode[0].elementendl; // coutnode[0].leftChildendl;tree.preOrder(node[0],level);coutendl;tree.inOrder(node[0]);in 0;coutendl;tree.postOrder(node[0]);post 0;coutendl;couttree.size()endl;couttree.getHeight(node[0])endl;// tree.constructTreeByLevelOrder(node,length); // tree.inOrder(); // cout2node[0].elementnode[0].leftChild-element;coutInput2endl;string s1,s2;cins1s2;binaryTreeNodechar node1[100];binaryTreeNodechar node2[100];for(int i 0; i s1.length(); i) {binaryTreeNodechar n(s1.at(i));node1[i] n;}int noting;for(int i 0; i s2.length(); i) {binaryTreeNodechar n(s2.at(i));node2[i] n;if(s2.at(i) s1.at(0)) noting i;}constructTreeByPreOrderAndInOrder(node1, node2, s1.length(), s2.length(), noting);coutOutput2endl;tree.preOrder(node1[0],level);coutendl;tree.inOrder(node1[0]);coutendl;tree.postOrder(node1[0]);coutendl;tree.levelOrder(node1[0]);coutendl;return 0; }
http://www.pierceye.com/news/516417/

相关文章:

  • 全球购物网站大全百度网盟推广官方网站
  • 计算机网站维护建设深圳外网站建设
  • 贵州公明建设投资咨询有限公司官方网站小说网站开发对影成三人小说
  • 软件分享网站不一样的婚恋网站怎么做
  • 如何维护给做网站的客户公司变更名称和经营范围
  • 网站建设维护php建站最好的公司排名
  • 济南1951年建站wordpress 描述
  • 政务网站建设信息嵊州网站制作
  • 我的网站突然找不到网页了seo是啥意思
  • 黑河做网站的公司平面设计现在怎么样
  • 银川网站建站中国建设银行人力资源网站
  • 建设部考试中心网站用自己的ip怎么查看dw8建设的网站
  • 九江网站建设九江商标设计网页
  • 网站建设资格预审公告附近广告设计与制作门店电话
  • 百度权重站长工具网页制作工具哪些好用
  • 关键词整站优化公司网站店招用什么软件做的
  • 租车网站模版广州市网站建设 骏域
  • 关闭网站怎么不保存我做的更改人工智能专业
  • ui中有哪些做的好看的网站简单logo设计
  • 深圳大型网站设计公司校园 网站建设 知乎
  • 西部数码网站流量怎么充优化网站界面的工具
  • 街区网站建设东阳市住房和城乡建设局网站
  • 怎样建设网站是什么网站代码制作软件
  • 成华区微信网站建设计公司加盟
  • 找个男做那个视频网站好高端网站建设公司
  • 做网站 毕业设计广东手机网页制作
  • 沈阳口碑最好的男科医院seo排名优化什么意思
  • 光谷做网站推广价格手机网站 教程
  • 泉州做网站多少钱关键词排名快照优化
  • 威海网站建设费用网站不能调用样式