运用C++如何使用二叉链表存储二叉树,遍历输出叶子节点路径,递归输出叶子节点值,输出树的深度?

作者&投稿:骆胞 (若有异议请与网页底部的电邮联系)
急求,用递归算法编写一个实现在以二叉链表存储的二叉树中遍历叶子结点的函数~

public void visit(Node n){ if(null != n){ // 递归访问其左子树 visit(n.lchild); if(null == n.lchild && null == n.rchild){ // 当前节点是叶子,则访问它 System.out.println(n); } // 递归访问其右子树 visit(n.rchild); }}

#include
using namespace std;

typedef struct TNode//二叉树结构
{
char nodeValue;//结点的值
TNode* left;//左子树
TNode* right;//右子树
}*BiTree;
void CreateBiTree(BiTree &T)//中序遍历方式创建二叉树 ,输入#代表该结点为空
{
char nodeValue;
cin>> nodeValue;
if(nodeValue!='#')//结点非空
{
T=new TNode;
T->nodeValue=nodeValue;
CreateBiTree(T->left);
CreateBiTree(T->right);
}
else T=NULL;

}
int CountLeaf(BiTree T)
{
static int LeafNum=0;//叶子初始数目为0,使用静态变量
if(T)//树非空
{
if(T->left==NULL&&T->right==NULL)//为叶子结点
LeafNum++;//叶子数目加1
else//不为叶子结点
{
CountLeaf(T->left);//递归统计左子树叶子数目
CountLeaf(T->right);//递归统计右子树叶子数目
}
}
return LeafNum;
}
//用来测试的main函数,
int main()
{
BiTree T;
int leafNum;
cout<<"请输入中序遍历的二叉树序列(#号代表该结点为空):如(ABC##DE#G##F###)"<<endl;
CreateBiTree(T);
leafNum=CountLeaf(T);
cout<<"该二叉树中叶子结点数为:"<<leafNum<<endl;
return 0;
}

构造的二叉树结构如下:

运行结果如下:


代码如下:

#include <iostream>
#include <vector>
using namespace std;

typedef struct tnode //定义树节点结构
{
int val;
tnode* left;
tnode* right;
tnode(int x=0):val(x),left(NULL),right(NULL){}//默认构造函数
}TreeNode,*pTreeNode;



void getPath(TreeNode* cur,vector<vector<TreeNode*> >&resAllPath,vector<TreeNode*> path,vector<TreeNode*>& leafNode)//深度优先遍历dfs
{
 
    path.push_back(cur);
    if(cur->left==NULL && cur->right==NULL)
    {
leafNode.push_back(cur);
        resAllPath.push_back(path);
        return;
    }
    if(cur->left)
    {
        getPath(cur->left,resAllPath,path,leafNode);
    }
    if(cur->right)
    {
        getPath(cur->right,resAllPath,path,leafNode);
    }
        
}

vector<vector<TreeNode*> > getLeafPathAndNode(TreeNode *root,vector<TreeNode*>& leafNode) //获取叶节点及其路径
{
     
    vector<vector<TreeNode*> > resAllPath;
    vector<TreeNode*> path;

        
    if(root==NULL)
        return resAllPath;
        
    getPath(root,resAllPath,path,leafNode);
    return resAllPath;
      
}

void printAllPath(vector<vector<TreeNode*> >& leafPath)//打印所有叶子节点路径
{
vector<vector<TreeNode*> >::iterator it1;
vector<TreeNode*>::iterator it2;
cout<<"The leaf node path is as follows:"<<endl;
for(it1=leafPath.begin();it1!=leafPath.end();it1++)
{
for(it2=(*it1).begin();it2!=(*it1).end();it2++)
{
cout<<(*it2)->val<<" ";
}
cout<<endl;
}
}
    
void printAllNode(vector<TreeNode*>& leafNode) //打印叶节点
{
vector<TreeNode*>::iterator it;
cout<<"The leaf node is as follows:"<<endl;
for(it=leafNode.begin();it!=leafNode.end();it++)
{
cout<<(*it)->val<<" ";
}
cout<<endl;
}
    
int getDepth(TreeNode* root) //获取树的深度
{
if(root==NULL)
return 0;
int l=getDepth(root->left);
int r=getDepth(root->right);
return l<r?r+1:l+1;
}


int main(void)
{
TreeNode* node1=new TreeNode(1);  //构造一颗二叉树
TreeNode* node2=new TreeNode(2);
TreeNode* node3=new TreeNode(3);
TreeNode* node4=new TreeNode(4);
TreeNode* node5=new TreeNode(5);
TreeNode* node7=new TreeNode(7);
TreeNode* node8=new TreeNode(8);
TreeNode* node9=new TreeNode(9);
TreeNode* node11=new TreeNode(11);

TreeNode* root=node1;

node1->left=node2;
node1->right=node3;
node2->left=node4;
node2->right=node5;
node3->right=node7;
node4->left=node8;
node4->right=node9;
node5->right=node11;

vector<TreeNode*> leafNode;

vector<vector<TreeNode*> > leafPath=getLeafPathAndNode(root,leafNode);

printAllNode(leafNode);

printAllPath(leafPath);


int depth = getDepth(root);
cout<<"The tree depth is "<<depth<<endl;


system("PAUSE");
return 0;
}





如何在C语言中使用二维数组?
如何在一维存储器中存放二维数组,可有两种方式:一种是按行排列, 即放完一行之后顺次放入第二行。另一种是按列排列, 即放完一列之后再顺次放入第二列。在C语言中,二维数组是按行排列的。即,先存放a[0]行,再存放a[1]行,最后存放a[2]行。每行中有四个元素也是依次存放。由于数组a说明为...

如何用c语言实现二维数组?
使用 C 语言实现二维数组可以通过以下步骤:1. 声明一个二维数组变量:首先,需要声明一个二维数组变量来存储数据。声明二维数组需要指定数组的行数和列数,并可以给数组命名。2. 初始化二维数组:可以选择在声明二维数组时初始化,或者在后续的代码中初始化数组。可以使用循环结构来遍历数组的每个元素,并...

如何使用C语言编写二进制转换为十进制的程序
if(n=0) {term=a[i]*power(n); sum=sum+term; } return sum; } int power(int b) { int i=2,j=1; if(b==0) i=1; for(;jb;j++) i=2*i; return i; }在这里我修改了一下代码,换成了使用库函数pow,不想自己写求2的n次方的函数的可以用这里的代码。“\/\/”后面的内容...

c语言如何使用二维数组存储中文?
用char就可以储存了 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include<stdio.h> int main() { char a[2][10]={"百度一下","你就知道"}; int i; for(i=0;i<8;i++) printf("%c",a[0][i]); printf("\\n"); for(i=0;i<2;i++) printf("%c",a[0][i]); prin...

c语言如何使用二维数组空心菱形*跳出
\/\/核心思想就是首先把二维字符数组元素全设为空格符,再将特定位置元素设为*,再输出#include "stdio.h"#define N 9 \/\/数组的行列数int main(){ char str[N][N]={0}; \/\/例子,行列数应该是奇数 int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)str[i][j]=' ';\/\/先全部...

c语言c++语言如何用二维数组做形参?
C\/C++中,二维数组的第一维的每一个元素都是一维数组。所以,用指向一维数组的指针或用第一维维数空缺的二维数组作为函数的形式参数都能达到目的。设处理数组为int型,举例代码如下:\/\/#include "stdafx.h"\/\/If the vc++6.0, with this line.#include "stdio.h"void myprint(int (*p)[5]){...

c语言的二进制数值如何直接输出?
1.C标准没有输出二进制的,不过用itoa()可以实现到二进的转换 2.可以使用itoa函数把变量的数值转换成2进制字符串,再用输出函数输出。3.用 法:char itoa(int value,char string,int radix);4.详细解释:itoa是英文integer to array(将int整型数转化为一个字符串,并将值保存在数组string中)的缩写....

c语言把二进制数转换成十进制数的程序怎么写。
按照如下步骤即可用C语言把二进制数转换成十进制数的程序:1、首先在主函数中设置成函数Sum,另外定义了一个数组array[8],用于存放输入的八位二进制数。2、然后使用了一个for循环语句,用于输入八位二进制数。在scanf函数里,在%d之间加一个1,然后使用printf函数输出,并且调用Sum函数,数组名作为实参...

二 数据类型是什么?C语言有哪些数据类型,分别作用是什么?(
数据类型是指在程序中可以使用的不同种类的数据,例如整数、浮点数、字符等。数据类型决定了变量的存储空间和表示方式。C语言中有以下几种基本数据类型:char:用于存储单个字符,占用1个字节。int:用于存储整数,占用4个字节。float:用于存储单精度浮点数,占用4个字节。double:用于存储双精度浮点数,...

C语言中如何给一个变量赋一个二进制数
C系列语言中,通常想要以二进制操作的时候,我们叫它位操作,所以使用移位运算符“<<” x<<1 = x *2 x<<2 = x *4 x<<3 = x *8 移位就是这个意思,想进行二进制赋值的时候,需要一位一位进行赋值 比如你想赋值"11001" int a; a = (1<<4) + (1<<3) + (1<<0) 这样赋值之...

乳源瑶族自治县18451961801: 运用C++如何使用二叉链表存储二叉树,遍历输出叶子节点路径,递归输出叶子节点值,输出树的深度? -
皮刘燕德: 构造的二叉树结构如下:运行结果如下:代码如下:#include <iostream>#include <vector> using namespace std; typedef struct tnode //定义树节点结构 { int val; tnode* left; tnode* right; tnode(int x=0):val(x),left(NULL),right(NULL){}//默认构造函数 }...

乳源瑶族自治县18451961801: c++ 采用二叉链表作存储结构,实现二叉树非递归后序遍历算法 -
皮刘燕德: 链接存储的二叉树类型和结构定义如下:typedef struct bnode { ElemType data; struct bnode *lchild, *rchild; } btree; 后序遍历 void postorder(btree *bt) { btree *p=bt, *stack[MAX];//p表示当前结点,栈stack[]用来存储结点int tag[MAX]; int ...

乳源瑶族自治县18451961801: 用C++程序语言:以二叉链表作存储结构,编写一个算法将二叉树左、右子树进行交换的程序 -
皮刘燕德: template t search(bstnode* tree, const t& elemt) { stack*> travstack; bstnode *p = root; //遍历指针 bstnode *q = root; //层数看守 int num = 1; if(p != null) travstack.push(p); while(!travstack.empty()) { p = travstack.pop(); if(p->data != elemt) if(p == q) ...

乳源瑶族自治县18451961801: 以二叉链表作存储结构,编写二叉树深度的递归算法(c++语言)
皮刘燕德: 求2叉树深度输入:123$$4$$56$$7$$输出:1234567depth:3#include<stdio.h>#include <stdlib.h>typedef struct btree { btree* lhs; btree* rhs; char val;} *node, btree;node btree_create(){ char c; nodet = (node)malloc(sizeof(btree)); t->lhs = t->rhs = ...

乳源瑶族自治县18451961801: 用C++语言编写一个采用二叉链式结构做存储结构的二叉排序树建立和查找算法.关于数据结构的,高手救命!! -
皮刘燕德: //自己好好看看,不理解的即时问 #include <stdio.h> #include <malloc.h> typedef int KeyType; typedef char InfoType[10]; typedef struct node //记录类型 {KeyType key; //关键字项InfoType data; //其他数据域struct node *lchild,*rchild; //左右孩...

乳源瑶族自治县18451961801: 利用链式存储创建二叉树,写出完整程序,(C\C++) -
皮刘燕德: 线性表的链式存储结构: typedef int elemtype; typedef struct lnode {elemtype data;struct lnode *next; }lnode,*linklist; (被封装好的每个节点,都有一个数据域data和一个指针域*next用于指向下一个节点) 二叉树的二叉链表: typedef int ...

乳源瑶族自治县18451961801: C++: 设计算法将一棵以二叉链表形式存储的二叉树转换为顺序存储形式存储到数组A[n]中,并将其中 -
皮刘燕德: 参考以下代码: #include <stdio.h> //定义二叉树的存储结构 struct BTNode {char data;BTNode* lchild;BTNode* rchild; }BTNode; void Ctree(struct BTNode* t,char A[],int i) {if(t!=NULL){A[i]=t->data;Ctree(t->lchild,A,i*2);Ctree(t->rchild,A,i*2+1);...

乳源瑶族自治县18451961801: (1)以二叉链表作为二叉树的存储结构,写出二叉树的存储结构定义(C或C++表示). (2)编程实现以二叉链 -
皮刘燕德: 思想上,可以构造“节点”这种结构体,该结构体包含自身的值,还有左孩子指针,右孩子指针.朝这个方向自己构思,希望能帮到你!欢迎追问

乳源瑶族自治县18451961801: 你答过的用顺序和二叉链表作存储结构 -
皮刘燕德: /*以下是用c++ 实现的二叉排序树的源代码*/ #include typedef struct TreeNode { int key; struct TreeNode *left; struct TreeNode *right; }treeNode; class BiSortTree { public: BiSortTree(void); void desplayTree(void);//显示这个树 void insertTree(int key...

乳源瑶族自治县18451961801: 二叉树的建立与遍历(C++) -
皮刘燕德: //先定义数据类型 #typedef struct BiTNode{char data;struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;//data你想用什么类型自己变就行了//建树也用递归 void createTree(char data,BiTree &T)//用引用 {char c; 输入c; if(c!=NULL){T=(BiTree)malloc(...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网