设为首页 加入收藏

TOP

数据结构二叉树构造及遍历详解(二)
2018-11-07 00:08:15 】 浏览:255
Tags:数据结构二 构造 详解
cout<<root->data<<' '; 57 } 58 } 59 60 int NodeNumber(Node root) 61 { 62 if(root == NULL) 63 return 0; 64 else 65 return 1+NodeNumber(root->lchild) + NodeNumber(root->rchild); 66 } 67 68 int DepthTree(Node root) 69 { 70 if(root) 71 return DepthTree(root->lchild) > DepthTree(root->rchild) ? DepthTree(root->lchild) + 1 : DepthTree(root->rchild) + 1; 72 else 73 return 0; 74 } 75 76 int LeafNumber(Node root) 77 { 78 if(!root) 79 return 0; 80 else if( (root->lchild == NULL) && (root->rchild == NULL) ) 81 return 1; 82 else 83 return ( LeafNumber(root->lchild) + LeafNumber(root->rchild) ); 84 } 85 86 87 88 int main() 89 { 90 Node root=NULL; 91 cout<<"请输入数据:"<<endl; 92 root=CreatTree(); 93 cout<<"二叉树建立成功!"<<endl; 94 95 cout<<"二叉树节点数为:"<<NodeNumber(root)<<endl; 96 97 cout<<"二叉树深度为:"<<DepthTree(root)<<endl; 98 99 cout<<"二叉树叶子结点数为:"<<LeafNumber(root)<<endl; 100 101 cout<<"前序遍历:"<<endl; 102 PreorderTraverse(root); 103 cout<<endl; 104 105 cout<<"中序遍历:"<<endl; 106 InorderTraverse(root); 107 cout<<endl; 108 109 cout<<"后序遍历:"<<endl; 110 LastorderTraverse(root); 111 cout<<endl; 112 113 return 0; 114 }

?

  首发在CSDN,有兴趣可以访问https://blog.csdn.net/qq_41785863

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇算法竞赛中c++一些需要注意的错误 下一篇洛谷P1600 天天爱跑步(差分 LCA ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目