南邮数据结构实验报告
实验报告 课程名称 实验名称数据结构二叉树基本操作以及哈夫曼编码译码系统 实验时间 指导单位指导教师年月日 学生姓名 学院(系) 班级学号专业 二叉树的基本运算: 一、问题描述 1.设计递归算法,实现二叉树的运算:删除一棵二叉树,求一棵二叉树的高度,求一棵二叉树中叶子节点数,复制一棵二叉树,交换一棵二叉树的左右子树 2.设计算法,自上而下,自左向右即按层次遍历一棵二叉树 3.设计main函数,测试上述每个运算 二、系统分析和概要设计 首先用maketree构造一棵二叉树,然后遍历二叉树,然后交换每个结点的左右子树,接着算出输得高度和叶子节点,最后删除。 三、详细设计 2.核心算法 建立二叉树的voidMakeTree(constT&x,BinaryTree&left,BinaryTree&right)和计算叶子节点的intSize(); 3.算法分析 删除一棵二叉树,求一棵二叉树的高度,求一棵二叉树中叶子节点数,复制一棵二叉树等都是用递归的方法实现。 四、程序代码 流程图 #include template structBTNode { }; template classBinaryTree { public: BTNode(){}BTNode(constT&x){}BTNode(constT&x,BTNode*l,BTNode*r){}Telement;BTNode*lChild,*rChild;element=x;lChild=l;rChild=r;element=x;lChild=rChild=NULL;lChild=rChild=NULL;BinaryTree(){root=NULL;}~BinaryTree(){Clear();}voidCopy(BinaryTree&r)const;boolIsEmpty()const{returnroot==NULL;}voidClear();voidExchange();boolRoot(T&x)const;intGetHeight();voidMakeTree(constT&x,BinaryTree&left,BinaryTree&right);voidBreakTree(T&x,BinaryTree&left,BinaryTree&right);voidPreOrder(void(*Visit)(T&x));voidLevelOrder(void(*Visit)(T&x));intSize();BinaryTree(BinaryTree&t) {}root=Copy(); //voidInOrder(void(*Visit)(T&x)); //voidPostOrder(void(*Visit)(T&x)); BTNode*Copy(BTNode*t);BTNode*root;staticintnumber;voidClear(BTNode*&t);voidExchange(BTNode*t);intGetHeight(BTNode*t);intSize(BTNode*t);voidPreOrder(void(*Visit)(T&x),BTNode*t);voidLevelOrder(void(*Visit)(T&x),BTNode*t);protected:private://voidInOrder(void(*Visit)(T&x),BTNode*t);//voidPostOrder(void(*Visit)(T&x),BTNode*t);}; template boolBinaryTree::Root(T&x)const { } template voidBinaryTree::Clear() { } template voidBinaryTree::Clear(BTNode*&t) { if(root){}elsereturnfalse;x=root->element;returntrue;Clear(root);if(t) 数据结构实验代码南邮实验课实验十各种算法性能比较 #include #include #include template voidswap(T&a,T&b) { Ttemp; temp=a; a=b; b=temp; } template//选择排序 voidSelectSort(TA[],intn) { intsmall; for(inti=0;i//直接插入排