基于二叉链表的二叉树中所有结点个数的统计

基于二叉链表的二叉树中所有结点个数的统计
描述

统计二叉树中所有结点的个数。

输入

多组数据。每组数据一行,为二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。

输出

每组数据输出一行,每行为二叉树中所有结点的个数。

输入样例 1 

abcd00e00f00ig00h00
abd00e00cf00g00
0

输出样例 1

9
7

示例一

#include<iostream>
using namespace std;
int m;
typedef struct BiTNode
{
 char data;
 struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char S[],int &i)
{
 if(S[i]=='0')
  T=NULL;
 else
 {
  T=new BiTNode;
  T->data=S[i];
  CreateBiTree(T->lchild,S,++i);
  CreateBiTree(T->rchild,S,++i);
 }
}
void Count(BiTree T)
{
 if(T)
 {
       if(T->lchild&&T->rchild)
   m++;
  else if(T->lchild||T->rchild)
   m++;
       else
   m++;
  Count(T->lchild);
  Count(T->rchild);
 }
}
int main()
{
 char S[100];
 while(cin>>S&&S[0]!='0')
 {
  m=0;
       int i=-1;
    BiTree T;
  CreateBiTree(T,S,++i);
  Count(T);
  cout<<m<<endl;
 }
 return 0;
}

示例二、

#include <iostream>
using namespace std;
 
int n0=0,n1=0,n2=0;
 
typedef struct BiTNode
{
	char data;
	struct BiTNode *lchild,*rchild;	
}BiTNode,*BiTree;BiTree temp;
 
void CreateBiTree(BiTree &T)
{
	char ch;
	cin>>ch;
	if(ch=='0')T=NULL;
	else
	{
		T=new BiTNode;
		T->data=ch;
		CreateBiTree(T->lchild);
		CreateBiTree(T->rchild); 
	}
}
  
void CreateBiTree(BiTree &T,char ch)
{
	if(ch=='0')T=NULL;
	else
	{
		T=new BiTNode;
		T->data=ch;
		CreateBiTree(T->lchild);
		CreateBiTree(T->rchild); 
	}
}
 
int Traverse(BiTree T)
{
	if(T)
	{
		if(T->lchild!=NULL&&T->rchild!=NULL) n2++;
		if(T->lchild!=NULL&&T->rchild==NULL||T->lchild==NULL&&T->rchild!=NULL) n1++;
		if(T->lchild==NULL&&T->rchild==NULL) n0++;
		Traverse(T->lchild);
		Traverse(T->rchild);	
	}
}
 
 
int main()
{
	while(1)
	{
		char ch;
		cin>>ch;
		if(ch=='0')
		{break;} 
		BiTree T;
		CreateBiTree(T,ch);
		Traverse(T);
		cout<<n0+n1+n2<<endl;
		n0=0;n1=0;n2=0; 
	}
	return 0;
}

示例三、

#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,string s,int &i)
{
    if(s[i]=='0')
        T=NULL;
    else
    {
        T=new BiTNode;
        T->data=s[i];
        CreateBiTree(T->lchild,s,++i);
        CreateBiTree(T->rchild,s,++i);
    }
}
int Root(BiTree T)
{
    if(T==NULL) return 0;
    return Root(T->lchild)+Root(T->rchild)+1;
}
int main()
{
    string s;
    cin>>s;
    while(s!="0")
    {
        BiTree T;
        int i=-1;
        CreateBiTree(T, s, ++i);
        cout<<Root(T)<<endl;;
        cin>>s;
    }
    return 0;
}

示例四、

#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,string s,int &i)
{
    if(s[i]=='0')
        T=NULL;
    else
    {
        T=new BiTNode;
        T->data=s[i];
        CreateBiTree(T->lchild,s,++i);
        CreateBiTree(T->rchild,s,++i);
    }
}
int Root(BiTree T)
{
    if(T==NULL) return 0;
    return Root(T->lchild)+Root(T->rchild)+1;
}
int main()
{
    string s;
    cin>>s;
    while(s!="0")
    {
        BiTree T;
        int i=-1;
        CreateBiTree(T, s, ++i);
        cout<<Root(T)<<endl;;
        cin>>s;
    }
    return 0;
}

五、

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int a, b, c;
typedef struct BiTNode
{
	char data;
	struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
void CreateBiTree(BiTree &T, char S[], int &i)
{
	if (S[i] == '0')
		T = NULL;
	else
	{
		T = new BiTNode;
		T->data = S[i];
		CreateBiTree(T->lchild, S, ++i);
		CreateBiTree(T->rchild, S, ++i);
	}
}
void Count(BiTree T)
{
	if (T)
	{
		if (T->lchild&&T->rchild)
			c++;
		else if (T->lchild || T->rchild)
			b++;
		else
			a++;
		Count(T->lchild);
		Count(T->rchild);
	}
}
int main()
{
	char S[100];
	while (cin >> S && S[0] != '0')
	{
		a = b = c = 0;
		int i = -1;
		int d = 0;
		BiTree T;
		CreateBiTree(T, S, ++i);
		Count(T);
        d=a+b+c;
		cout << d << endl;
	}
	return 0;
}

六、

	#include <iostream>
using namespace std;
 
 
typedef struct BiNode
{
	char data;
	struct BiNode *lchild,*rchlid;
}*BiTree;
 
void Creat(BiTree &T)
{
	char ch;
	cin>>ch;
	if(ch=='0') T=NULL;
	else
	{
		T = new BiNode;
		T->data=ch;
		Creat(T->lchild);
		Creat(T->rchlid);
	}
}
 
int count(BiTree T)
{
	if(T==NULL) return 0;
	else return count(T->lchild)+count(T->rchlid)+1;
}
 
int main()
{
	while(1)
	{
		BiTree T;
		Creat(T);
		if(T==NULL) break;
		cout<<count(T)<<endl;
	}
	return 0;
}

七、

#include<iostream>
using namespace std;
typedef struct BiNode{
	char data;
	struct BiNode *lchild,*rchild;
}BiNode,*BiTree; 
 
void CreateBiTree(BiTree &T,char s[],int &i)
{
	if(s[i]=='0') T=NULL;
	else{
		T=new BiNode;
		T->data=s[i];
		CreateBiTree(T->lchild,s,++i);
		CreateBiTree(T->rchild,s,++i);
	}
}
void Count(BiTree T,int &a,int &b,int &c)
{
	if(T==NULL) return;
	if(T->lchild&&T->rchild) a++;
	else if(T->lchild||T->rchild) b++;
	else c++;
	Count(T->lchild,a,b,c);
	Count(T->rchild,a,b,c);
}
 
int main()
{
	char s[100];
	int num;
	while(cin>>s&&s[0]!='0')
	{
		int i=-1;
		int a,b,c;
		a=b=c=0;
		BiTree T;
		CreateBiTree(T,s,++i);
		Count(T,a,b,c);
		num=a+b+c;
		cout<<num<<endl;	
	}
	return 0;
}

八、

#include<iostream>
using namespace std;
 
typedef struct BiTNode{
	char data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
 
void Creat(BiTree &T){
	char ch;
	cin>>ch;
	if(ch=='0')
		T=NULL;
	else{
		T=new BiTNode;
		T->data=ch;
		Creat(T->lchild);
		Creat(T->rchild);
	}
}
int Count(BiTree &T){
	if(T==NULL)
		return 0;
	else{
		return Count(T->lchild)+Count(T->rchild)+1;
	}
} 
 
int main(){
	BiTree T;
	
	while(1){
		Creat(T);
		
		if(T==NULL)
			break;
		else
			cout<<Count(T)<<endl;
	}
}

九、

#include <iostream>
using namespace std;
 
int n=0;
 
typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;BiTree temp;
 
void CreateBiTree(BiTree &T)
{
    char ch;
    cin>>ch;
    if(ch=='0')T=NULL;
    else
    {
        T=new BiTNode;
        T->data=ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
}
 
void CreateBiTree(BiTree &T,char ch)
{
    if(ch=='0')T=NULL;
    else
    {
        T=new BiTNode;
        T->data=ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
}
 
 
void Traverse(BiTree T){
    
    if(T){
        n++;
        Traverse(T->lchild);
        Traverse(T->rchild);
    }
}
 
int main(){
    while(1){
        char ch;
        cin>>ch;
        if('0'==ch) break;
        BiTree T;
        CreateBiTree(T,ch);
        Traverse(T);
        cout<<n<<endl;
        n=0;
    }
    return 0;
}

	//所有节点个数的统计 
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct BiNode
{
	char data;
	struct BiNode *lchild,*rchild;
 } BiTNode,*BiTree;
 
void Create(BiTree &T)  //先序创建 
{
	char ch;
	cin>>ch;
	if(ch=='0')
		T=NULL;
	else
	{
		T=new BiTNode;
		T->data=ch;
		Create(T->lchild);
		Create(T->rchild);
	 } 
}
 
int NodeCount(BiTree T)
{
	if(T==NULL)return 0;
	else return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
 } 
 
 
 int main()
 {
 	BiTree T;
 	while(1)
	 {
	 	
		Create(T);
 		if(T==NULL)
 		break;
 	cout<<NodeCount(T)<<endl;
 		
	 }
	 return 0; 
  }

十一、

#include <iostream>
using namespace std;
 
int n0 = 0, n1 = 0, n2 = 0;
 
typedef struct BiTNode
{
	char data;
	struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree; BiTree temp;
 
void CreateBiTree(BiTree& T)
{
	char ch;
	cin >> ch;
	if (ch == '0')T = NULL;
	else
	{
		T = new BiTNode;
		T->data = ch;
		CreateBiTree(T->lchild);
		CreateBiTree(T->rchild);
	}
}
 
void CreateBiTree(BiTree& T, char ch)
{
 
	T = new BiTNode;
	T->data = ch;
	CreateBiTree(T->lchild);
	CreateBiTree(T->rchild);
}
 
void Traverse(BiTree T)
{
	if (T)
	{
		if (T->lchild != NULL && T->rchild != NULL) n2++;
		if (T->lchild != NULL && T->rchild == NULL || T->lchild == NULL && T->rchild != NULL) n1++;
		if (T->lchild == NULL && T->rchild == NULL) n0++;
		Traverse(T->lchild);
		Traverse(T->rchild);
	}
}
 
 
int main()
{
	while (1)
	{
		char ch;
		cin >> ch;
		if (ch == '0')break;
		BiTree T;
		CreateBiTree(T, ch);
		Traverse(T);
		cout << n0 + n1 +n2<< endl;
		n0 = 0; n1 = 0; n2 = 0;
	}
	return 0;
}

十二、

#include<iostream>
using namespace std;
 
typedef struct BiTNode
{
	char data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
 
void CreatBiTree(BiTree& T)
{
	char ch;
	cin>>ch;
	if(ch=='0')
		T=NULL;
	else
	{
		T=new BiTNode;
		T->data=ch;
		CreatBiTree(T->lchild);
		CreatBiTree(T->rchild);
	}
}
 
int count(BiTree T)
{
	if(T==NULL)
		return 0;
	else
	{
		int m,n;
		m=count(T->lchild);
		n=count(T->rchild);
		return (1+m+n);
	}
}
 
int main()
{
	BiTree T;
	while(1)
	{
		CreatBiTree(T);
		if(T==NULL)
			break;
		cout<<count(T)<<endl;
	}
	return 0;
}

十三、(Liu建淳)

#include<iostream>
#include<string>
#include<cstdio>
 
using namespace std;
 
#define OK 1
#define ERROR 0
 
int num;
 
typedef struct BiTNode
{
	char data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
 
 
 
 
 
void Count(BiTree &T)
{
	if(T)
	{
    	num++;
		Count(T->lchild);
		Count(T->rchild);
	}
}
 
 
 
 
void Input(BiTree &T,char c[],int &i)
{
	if(c[i]=='0')
		T=NULL;
	else
	{
		T=new BiTNode;
		T->data=c[i];
		Input(T->lchild,c,++i);
		Input(T->rchild,c,++i);
	}
}
 
 
 
 
int main()
{
	char c[1024];
	while(cin>>c&&c[0]!='0')
	{
		num=0;
		int i=-1;
		BiTree T;
		Input(T,c,++i);
		Count(T);
		cout<<num<<endl;
	}
	return 0;
}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个完整的程序,实现了开始时用先序/序/后序遍历的顺序建立二叉链表的递归算法,并且包含了您要求的所有功能: ```c++ #include <iostream> #include <vector> #include <queue> #include <stack> #include <climits> using namespace std; // 二叉树结点的定义 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; // 先序遍历建立二叉树 TreeNode* buildTreeByPreOrder(vector<int>& preOrder, int& index) { if (index >= preOrder.size() || preOrder[index] == -1) { index++; return nullptr; } TreeNode* root = new TreeNode(preOrder[index++]); root->left = buildTreeByPreOrder(preOrder, index); root->right = buildTreeByPreOrder(preOrder, index); return root; } // 序遍历建立二叉树 TreeNode* buildTreeByInOrder(vector<int>& inOrder, int& index, int start, int end) { if (index >= inOrder.size() || start > end) { return nullptr; } int i = start; for (; i <= end; i++) { if (inOrder[i] == inOrder[index]) { break; } } TreeNode* root = new TreeNode(inOrder[index++]); root->left = buildTreeByInOrder(inOrder, index, start, i - 1); root->right = buildTreeByInOrder(inOrder, index, i + 1, end); return root; } // 后序遍历建立二叉树 TreeNode* buildTreeByPostOrder(vector<int>& postOrder, int& index) { if (index < 0 || postOrder[index] == -1) { index--; return nullptr; } TreeNode* root = new TreeNode(postOrder[index--]); root->right = buildTreeByPostOrder(postOrder, index); root->left = buildTreeByPostOrder(postOrder, index); return root; } // 先序遍历二叉树的递归算法 void preOrder(TreeNode* root) { if (root == nullptr) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } // 序遍历二叉树的递归算法 void inOrder(TreeNode* root) { if (root == nullptr) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } // 后序遍历二叉树的递归算法 void postOrder(TreeNode* root) { if (root == nullptr) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } // 计算二叉树的深度的递归算法 int depth(TreeNode* root) { if (root == nullptr) { return 0; } int leftDepth = depth(root->left); int rightDepth = depth(root->right); return max(leftDepth, rightDepth) + 1; } // 统计二叉树结点个数的递归算法 int countNodes(TreeNode* root) { if (root == nullptr) { return 0; } int leftCount = countNodes(root->left); int rightCount = countNodes(root->right); return leftCount + rightCount + 1; } // 统计二叉树的叶子结点个数的递归算法 int countLeaves(TreeNode* root) { if (root == nullptr) { return 0; } if (root->left == nullptr && root->right == nullptr) { return 1; } int leftLeaves = countLeaves(root->left); int rightLeaves = countLeaves(root->right); return leftLeaves + rightLeaves; } // 设计该二叉树第K层的结点个数 int countNodesKthLevel(TreeNode* root, int k) { if (root == nullptr) { return 0; } if (k == 1) { return 1; } int leftCount = countNodesKthLevel(root->left, k - 1); int rightCount = countNodesKthLevel(root->right, k - 1); return leftCount + rightCount; } // 求该二叉树所有结点值最大的元素 int findMax(TreeNode* root) { if (root == nullptr) { return INT_MIN; } int leftMax = findMax(root->left); int rightMax = findMax(root->right); return max(max(leftMax, rightMax), root->val); } // 打印二叉树的叶子结点数的递归算法 void printLeaves(TreeNode* root) { if (root == nullptr) { return; } if (root->left == nullptr && root->right == nullptr) { cout << root->val << " "; return; } printLeaves(root->left); printLeaves(root->right); } int main() { // 建立二叉树 vector<int> preOrder = {1, 2, 4, -1, -1, 5, -1, -1, 3, 6, -1, -1, 7, -1, -1}; int index = 0; TreeNode* root = buildTreeByPreOrder(preOrder, index); // 先序遍历 cout << "PreOrder: "; preOrder(root); cout << endl; // 序遍历 cout << "InOrder: "; vector<int> inOrder; stack<TreeNode*> s; TreeNode* p = root; while (p != nullptr || !s.empty()) { while (p != nullptr) { s.push(p); p = p->left; } p = s.top(); s.pop(); inOrder.push_back(p->val); p = p->right; } index = 0; TreeNode* root2 = buildTreeByInOrder(inOrder, index, 0, inOrder.size() - 1); inOrder(root2); cout << endl; // 后序遍历 cout << "PostOrder: "; vector<int> postOrder; stack<TreeNode*> s2; p = root; while (p != nullptr || !s2.empty()) { while (p != nullptr) { s2.push(p); postOrder.insert(postOrder.begin(), p->val); p = p->right; } p = s2.top(); s2.pop(); p = p->left; } index = postOrder.size() - 1; TreeNode* root3 = buildTreeByPostOrder(postOrder, index); postOrder(root3); cout << endl; // 计算二叉树深度 cout << "Depth: " << depth(root) << endl; // 统计二叉树结点个数 cout << "Count Nodes: " << countNodes(root) << endl; // 统计二叉树叶子结点个数 cout << "Count Leaves: " << countLeaves(root) << endl; // 设计该二叉树第K层的结点个数 int k = 3; cout << "Count Nodes in " << k << "th Level: " << countNodesKthLevel(root, k) << endl; // 求该二叉树所有结点值最大的元素 cout << "Max Value: " << findMax(root) << endl; // 打印二叉树的叶子结点数 cout << "Leaves: "; printLeaves(root); cout << endl; return 0; } ``` 程序,先序遍历、序遍历、后序遍历建立二叉树的方法分别为buildTreeByPreOrder、buildTreeByInOrder、buildTreeByPostOrder。其他功能实现方法与前面给出的递归算法一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值