数据结构实验二解析.docxVIP

  • 8
  • 0
  • 约1.84万字
  • 约 26页
  • 2017-06-05 发布于湖北
  • 举报
二叉树的基本运算:问题描述设计递归算法,实现二叉树的运算:删除一棵二叉树,求一棵二叉树的高度,求一棵二叉树中叶子节点数,复制一棵二叉树,交换一棵二叉树的左右子树设计算法,自上而下,自左向右即按层次遍历一棵二叉树设计main函数,测试上述每个运算二、系统分析和概要设计首先用maketree构造一棵二叉树,然后遍历二叉树,然后交换每个结点的左右子树,接着算出输得高度和叶子节点,最后删除。三、详细设计T1. 类和类的层次结构BinaryTree#BTNodeT * root-static int number+BinaryTree()+~BinaryTree()+void Copy(BinaryTreeT r) const+bool IsEmpty()const+void Clear()+void Exchange()+bool Root(T x)const;+int GetHeight()+voidMakeTree(const T x,BinaryTreeT left,BinaryTreeT right)+void BreakTree(Tx,BinaryTreeT left,BinaryTreeT right)+void PreOrder(void (*Visit)(T x))+void LevelOrder(void (*Visit)(T x))+int Size()+BinaryTreeT(BinaryTreeT t)+BTNodeT* Copy(BTNodeT* t)-void Clear(BTNodeT* t)-void Exchange(BTNodeT* t)-int GetHeight(BTNodeT* t)-int Size(BTNodeT* t)-void PreOrder(void (*Visit)(T x),BTNodeT* t)-void LevelOrder(void (*Visit)(T x),BTNodeT* t)2. 核心算法建立二叉树的void MakeTree(const T x,BinaryTreeT left,BinaryTreeT right)和计算叶子节点的int Size();3. 算法分析删除一棵二叉树,求一棵二叉树的高度,求一棵二叉树中叶子节点数,复制一棵二叉树等都是用递归的方法实现。程序代码建立二叉树树遍历二叉树叶子节点数量树的高度左右交换复制二叉树删除二叉树 流程图 #includeiostream.htemplateclass Tstruct BTNode{BTNode(){lChild=rChild=NULL;}BTNode(const T x){element=x;lChild=rChild=NULL;}BTNode(const T x,BTNodeT* l,BTNodeT* r){element=x;lChild=l;rChild=r;}T element;BTNodeT* lChild,* rChild;};templateclass Tclass BinaryTree{public:BinaryTree(){root=NULL;}~BinaryTree(){Clear();}void Copy(BinaryTreeT r) const;bool IsEmpty()const{return root == NULL;}void Clear();void Exchange();bool Root(T x)const;int GetHeight();void MakeTree(const T x,BinaryTreeT left,BinaryTreeT right);void BreakTree(T x,BinaryTreeT left,BinaryTreeT right);void PreOrder(void (*Visit)(T x));void LevelOrder(void (*Visit)(T x));int Size();BinaryTreeT(BinaryTreeT t){root=Copy(t.root);}//void InOrder(void (*Visit)(T x));//void PostOrder(void (*Visit)(T x));BTNodeT* Copy(BTNodeT* t);protected:BTNodeT * root;private:static int number;void Clear(BTNodeT* t);void Exchange(BTNodeT* t);int GetHeight(BTNodeT* t);int Size(BTNod

文档评论(0)

1亿VIP精品文档

相关文档