【工程】二叉树已知前序/中序的顺序,构造树的递归等实现
来源:互联网 发布:java文件上传工具类 编辑:程序博客网 时间:2024/06/05 21:04
文章中的二叉树原型为 《Data Structures, Algorithms, & Applications in C++》, 1nd Edition中的树类。
要求在源文件的基础上 实现以下三个操作:
【1】给出前序遍历和中序遍历结果,构造树。
【2】给出计算叶子节点数目的函数。
【3】交换树中的左右所有节点。
需要用到的主要函数(异常抛出\链表队列之类与主题没啥关系的请查书):
遍历查找函数:
BinaryTree<char> MakeTree2(string InOrder, string PreOrder){BinaryTree<char> tree;if(InOrder.empty()){tree.root = 0;return tree;}char RootValue;RootValue = PreOrder[0];//根结点的值//计算左右子树中结点数size_t pos,LeftChildSize, RightChildSize;pos = InOrder.find(RootValue);LeftChildSize = pos;RightChildSize = InOrder.size() - pos - 1;string LeftInOrder, LeftPreOrder, RightInOrder, RightPreOrder;//原函数不变LeftInOrder = InOrder.substr(0, LeftChildSize);RightInOrder = InOrder.substr(pos + 1, RightChildSize);LeftPreOrder = PreOrder.substr(1,LeftChildSize+1);RightPreOrder =PreOrder.substr(LeftChildSize+1,RightChildSize+1); BinaryTree<char> left,right;left = MakeTree2(LeftInOrder,LeftPreOrder);right = MakeTree2(RightInOrder,RightPreOrder);tree.root = new BinaryTreeNode<char>(RootValue); tree.root->LeftChild = left.root; tree.root->RightChild = right.root; left.root = right.root = 0;return tree;}
BinaryTree.h
#include<iostream>#include "LinkedQueue.h"#include "binaryTreeNode.h"#include "myexception.h"#include <string>#ifndef _BinaryTree#define _BinaryTreeint _count;template<class T>class BinaryTree { public: BinaryTree() {root = 0;}; ~BinaryTree(){}; bool IsEmpty() const {return ((root) ? false : true);} bool Root(T& x) const; void MakeTree(const T& element, BinaryTree<T>& left, BinaryTree<T>& right); void BreakTree(T& element, BinaryTree<T>& left, BinaryTree<T>& right); void PreOrder(void(*Visit)(BinaryTreeNode<T> *u)) {PreOrder(Visit, root);} void InOrder(void(*Visit)(BinaryTreeNode<T> *u)) {InOrder(Visit, root);} void PostOrder(void(*Visit)(BinaryTreeNode<T> *u)) {PostOrder(Visit, root);} void LevelOrder(void(*Visit)(BinaryTreeNode<T> *u)); void ExChange(BinaryTreeNode<T> *u); void PreOutput() {PreOrder(Output, root); cout << endl;} void InOutput() {InOrder(Output, root); cout << endl;} void PostOutput() {PostOrder(Output, root); cout << endl;} void LevelOutput() {LevelOrder(Output); cout << endl;} void Delete() {PostOrder(Free, root); root = 0;} int Height() const {return Height(root);} int Size() {_count = 0; PreOrder(Add1, root); return _count-1;} //修改 叶子节点个数 int SizeLeaf(BinaryTreeNode<T> *root) { if(root==NULL)return 0; else if(root->LeftChild==NULL && root->RightChild==NULL)return 1; else return SizeLeaf(root->LeftChild)+SizeLeaf(root->RightChild); } //自己添加的函数,P31--1 bool Compare(BinaryTreeNode<T> *X,BinaryTreeNode<T> *Y) { if(!Y && !X)return true; if((!(!Y && !X)) &&((!Y)||(!X)) || Y->data != X->data) return false; if(Compare(X->LeftChild, Y->LeftChild) && Compare(X->RightChild, Y->RightChild)) return true; else return false; } BinaryTreeNode<T> *root; private: void PreOrder(void(*Visit) (BinaryTreeNode<T> *u), BinaryTreeNode<T> *t); void InOrder(void(*Visit) (BinaryTreeNode<T> *u), BinaryTreeNode<T> *t); void PostOrder(void(*Visit) (BinaryTreeNode<T> *u), BinaryTreeNode<T> *t); static void Free(BinaryTreeNode<T> *t) {delete t;} static void Output(BinaryTreeNode<T> *t) {cout << t->data << ' ';} static void Add1(BinaryTreeNode<T> *t) {_count++;} int Height(BinaryTreeNode<T> *t) const;};template<class T>bool BinaryTree<T>::Root(T& x) const{ if (root) {x = root->data; return true;} else return false; }template<class T>void BinaryTree<T>::MakeTree(const T& element, BinaryTree<T>& left, BinaryTree<T>& right){ root = new BinaryTreeNode<T> (element, left.root, right.root); left.root = right.root = 0;}template<class T>void BinaryTree<T>::BreakTree(T& element, BinaryTree<T>& left, BinaryTree<T>& right){ if (!root) throw BadInput(); element = root->data; left.root = root->LeftChild; right.root = root->RightChild; delete root; root = 0;}//交换树的左右子节点.template <class T> void BinaryTree<T>::ExChange(BinaryTreeNode<T> *u){ BinaryTreeNode<T> *temp; temp = root; if(u) { temp=u->LeftChild; (u)->LeftChild=(u)->RightChild; (u)->RightChild=temp; ExChange((*u).LeftChild); ExChange((*u).RightChild); }} template<class T> void BinaryTree<T>::PreOrder( void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t){ if (t) { Visit(t); PreOrder(Visit, t->LeftChild); PreOrder(Visit, t->RightChild); }} template <class T> void BinaryTree<T>::InOrder( void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t){ if (t) { InOrder(Visit, t->LeftChild); Visit(t); InOrder(Visit, t->RightChild); }} template <class T> void BinaryTree<T>::PostOrder( void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t){ if (t) { PostOrder(Visit, t->LeftChild); PostOrder(Visit, t->RightChild); Visit(t); }} template <class T> void BinaryTree<T>::LevelOrder( void(*Visit)(BinaryTreeNode<T> *u)){ LinkedQueue<BinaryTreeNode<T>*> Q; BinaryTreeNode<T> *t; t = root; while (t) { Visit(t); if (t->LeftChild) Q.Add(t->LeftChild); if (t->RightChild) Q.Add(t->RightChild); try {Q.Delete(t);} catch (OutOfBounds) {return;} }}template <class T>int BinaryTree<T>::Height(BinaryTreeNode<T> *t) const{ if (!t) return 0; int hl = Height(t->LeftChild); int hr = Height(t->RightChild); if (hl > hr) return ++hl; else return ++hr;}#endif
BinaryTreeNode.h,没改。。
#ifndef BinaryNode#define BinaryNodetemplate <class T> class BinaryTree;template <class T>class BinaryTreeNode { friend BinaryTree<T>; public: BinaryTreeNode() {LeftChild = RightChild = 0;} BinaryTreeNode(const T& e) {data = e; LeftChild = RightChild = 0;} BinaryTreeNode(const T& e, BinaryTreeNode *l, BinaryTreeNode *r) {data = e; LeftChild = l; RightChild = r;} T data; BinaryTreeNode<T> *LeftChild, *RightChild; };#endif
测试文件:
#include <iostream>#include <string>#include "BinaryTree.h"#include "myexception.h"#include "BinaryTreeNode.h"using namespace std;int count = 0;BinaryTree<int> a,x,y,z,q,r,s;int main(){y.MakeTree(3,a,a);x.MakeTree(4,a,a);z.MakeTree(5,y,x);cout<<"交换前:";z.PreOutput();//交换测试z.ExChange(z.root);cout<<"交换后:";z.PreOutput();//个数测试cout<<"叶子节点个数:"<<z.SizeLeaf(z.root)<<endl;//声明一个变量BinaryTree<char> p;//qianxu="111",zhongxu="111";/*string zhongxu="1234";string qianxu="4321";*//*string zhongxu="312";string qianxu="132";*/string zhongxu="32145";string qianxu="12345";p=MakeTree2(zhongxu,qianxu);cout<<"Add之后 前序的结果:";p.PreOutput();cout<<endl; system("pause"); return 0;}
- 【工程】二叉树已知前序/中序的顺序,构造树的递归等实现
- 二叉树的重建 已知前序 中序 求后序 递归的方法
- 已知二叉树的前序和中序,求后序
- 前序和中序构造树的递归实现
- 重建二叉树 的 非递归算法(已知前序和中序)输出 层续和后续
- 已知二叉树的前序遍历和中序遍历重建二叉树(二叉树)
- 简单二叉树的构造,前序遍历顺序为;中序遍历顺序为;后序遍历顺序为;
- 【面试算法系列】已知二叉树的前序和中序遍历重建二叉树 - C语言实现
- 已知二叉树的前序和中序序列,构建二叉树并求后序序列,java实现。
- 二叉树遍历,已知前序遍历与中序遍历,求后续遍历 使用java(递归方式)实现
- 二叉树的前序、中序、后序的实现(递归和非递归)
- 【二叉树】实现二叉树的前序、中序、后序的非递归遍历
- 二叉树:已知前序&&中序或中序&&后序构造树
- 已知二叉树的前序和中序遍历,构建该二叉树
- 已知二叉树的前序和中序遍历,重构该二叉树
- 已知二叉树的前序遍历和中序遍历求后序遍历(二叉树)
- 已知二叉树的前序、中序遍历,重建二叉树
- 已知二叉树的前序和中序遍历,构建该二叉树
- jQuery:用方向键控制层的移动
- maven根据系统指定过滤文件
- usb基础
- Android之Fragment(上)
- 457 - Linear Cellular Automata(就这样懒惰的转了吧)
- 【工程】二叉树已知前序/中序的顺序,构造树的递归等实现
- jquery 获取 DIV的width
- 剑指offer 面试题8 旋转数组的最小数字 (旋转数组就是把有序数组后面一部分挪到前面)
- 集合
- 通用Makefile模板
- 剑指offer 面试题9 递归 非波那且数列
- 32位ubuntu安装alfresco4.x
- 线程状态图
- 剑指offer面试题10 二进制中1的个数(位运算专题)