C语言算法实现查找二叉树最短路径的问题 2C

1个回答

C语言求二叉树上的节点路径 求源程序，不胜感激

C语言二叉树的节点查找问题（递归方法）

C语言求二叉树结点个数？

![图片说明](https://img-ask.csdn.net/upload/201908/29/1567008482_442997.jpg) 黑笔部分代码不是用来求二叉树的高度吗，怎么又能算二叉树的结点个数了，求解答

c语言 判断二叉树是否为完全二叉树

C语言构造二叉树的问题，二叉树从键盘的输入

Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / \ / \ B E / \ \ / \ \ A C G / / F To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG. She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it). Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree. However, doing the reconstruction by hand, soon turned out to be tedious. So now she asks you to write a program that does the job for her! Input The input will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) Input is terminated by end of file. Output For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root). Sample Input DBACEGF ABCDEFG BCAD CBAD Sample Output ACBFGED CDAB

java编写查找二叉树某个结点的父节点的算法

【c语言数据结构】遍历二叉树

C语言如何输入两个二叉树的二进制判断二叉树是否是相等的

Problem Description Everyone konws how to traveling a tree using the DFS strategy. But we also know that there are many ways to do so. For example, giving a tree as the following picture, we may get three ways: 010011, 001101, 01010011. 0 stands for the down operation while 1 means the up operation. Now we make a constraint: if one node has k direct childs, you can visit a node at most 2*k times, if k == 0, you can visit it only once, in the example, the root has two direct child. Like the example, you can only get two ways: 010011, 001101. Because the way 01010011 will visit the node in yellow four times. Here is the problem: ACboy drawed a tree, but is not very nice, so he won't show you the picture. Instread he will give you two strings indicating that the ways to travel the tree. Of cource, the strings will only contain 0 and 1. And your mission is to tell whether ACboy is telling the truth. For example, he drawed a picture as the following, if he give you 010011 and 001101, then he is telling the truth, but if he give you 010011 and 01010011, you konw that he is telling a lie. Input On the first line of input is a single positive integer n, telling the number of test scenarios to follow.Each test case consists of two lines, each containing a string of the characters '0' and '1' of length at most 3000, both describing a way to travel the tree. Output For each test case output a line containing the word "True" or the word "False", depending on whether ACboy is telling the truth. Sample Input 2 010011 001101 010011 01010011 Sample Output True False

C语言二叉树遍历的问题

``` #include<stdio.h> #include<stdlib.h> typedef struct node { int num; struct node *l; struct node *r; }LN; void Creat(LN *L); void Change(LN *L); int Print(int n); void main() { LN *L; int n; ii: printf("Press 0 to end: \n"); printf("Press 1 to Creat a Linklist: \n"); printf("Press 2 to Change the child: \n"); scanf("%d",&n); switch(n) { case 0: system("CLS"); goto cc; case 1: system("CLS"); printf("Input the root number: \n"); Creat(L); system("CLS"); break; case 2: Change(L); break; } goto ii; cc:; } void Creat(LN *L) { int n; scanf("%d",&n); if(n==0) { L=NULL; } else { L=(LN*)malloc(sizeof(LN)); L->num=n; printf("Creat %d's leftchlid: \n",n); Creat(L->l); printf("Creat %d's rightchild: \n",n); Creat(L->r); } } void Change(LN *L) { if (L!=NULL) { printf("%d",L->num); Change(L->l); Change(L->r); } } ``` 我的Change函数是先序遍历，小弟刚学实在不知道错在哪里。怎么从先序遍历算法中交换左右孩子，谢谢大神门！！！！！ 还有怎么按层次遍历，感觉按层次没有办法用递归算法 如果我要想从大到小的顺序输出二叉排序树的各结点的算法，怎么实现，跪求大神门的指教，感谢！

c语言中数据结构二叉树的遍历问题

I use a data structrue to description a tree. struct Node{ char *name; int value; //标志位，1说明该节点在数组中无效了，0则有效 int isNull; struct Node *leftNode; struct Node *rightNode; struct Node *parentNode; int isLastNode; }. I want to reckon a Haffman code. this is my code: # include <stdio.h> # include <string.h> # include <stdlib.h> struct Node{ char *name; int value; //标志位，1说明该节点在数组中无效了，0则有效 int isNull; struct Node *leftNode; struct Node *rightNode; struct Node *parentNode; int isLastNode; } nodes[5],sigleNode; //声明定义的初始化节点的方法 void initNodes(); //声明获取节点数组的长度的方法 int getNodesSize(); //声明获得哈夫曼树节点的方法 Node getHaffTree(); //声明获取值最小的节点的方法 Node getMinValueNode(); //声明获取非空节点数量的方法 int getNotNullNodesSize(); //声明判断是否是空节点的方法 int isNullNode(Node n); //声明获取第几个有效节点的方法,i从0开始 Node getValidNode(int i); //声明将节点插入到数组节点中无效节点的方法 void addNodetoList(Node n); //声明检索树的方法 void viewTree(Node *n); //主方法 int main(){ //初始化节点 initNodes(); Node mainNode = getHaffTree(); //printf("根节点值 %d \n",mainNode.value); viewTree(&mainNode); return 0; } //检索树结构 void viewTree(Node *mainNode){ Node *leftNodeP = mainNode->leftNode; if( leftNodeP->isLastNode == 1){ //计算哈夫曼码.to do printf("权值为[%d]的节点[%s]哈夫曼码是: \n",leftNodeP->value,leftNodeP->name); }else{ viewTree(leftNodeP->leftNode); } Node *rightNodeP = mainNode->rightNode; if( rightNodeP->isLastNode == 1){ //计算哈夫曼码.to do printf("权值为[%d]的节点[%s]哈夫曼码是: \n",rightNodeP->value,rightNodeP->name); }else{ viewTree(rightNodeP->rightNode); } /* if(NULL != mainNode.rightNode){ Node rightNode = *mainNode.rightNode; Node rlNode = *rightNode.leftNode; Node rrNode = *rightNode.rightNode; char *rlName = rlNode.name; char *rrName = rrNode.name; if(strcmp(empName,rlName) ==0 && strcmp(empName,rrName) == 0){ printf("权值为[%d]的节点[%s]哈夫曼码是: \n",rightNode.value,rightNode.name); }else{ viewTree(rightNode); } } */ //viewTree(leftNode); /* if(!leftNode==NULL){ if(leftNode.leftNode == NULL && leftNode.rightNode == NULL){ //String haffCode = getHaffCode(leftNode,""); printf("权值为[%d]的节点[%s]哈夫曼码是: \n",leftNode.value,leftNode.name); }else{ viewTree(leftNode); } }*/ //Node rightNode = *mainNode.rightNode; //printf("右 value %d \n",rightNode.value); //viewTree(rightNode); /* if(!rightNode==NULL){ if(leftNode.leftNode == NULL && leftNode.rightNode == NULL){ //String haffCode = getHaffCode(rightNode,""); printf("权值为[%d]的节点[%s]哈夫曼码是:\n",rightNode.value,rightNode.name); }else{ viewTree(rightNode); } } */ } //递归方法组件树形数据结构 Node getHaffTree(){ int size = getNotNullNodesSize(); printf("length = %d \n",size); if(size > 2){ printf("before size %d \n",getNotNullNodesSize()); Node minNode1 = getMinValueNode(); Node minNode2 = getMinValueNode(); printf("value1 %d \n",minNode1.value); printf("value2 %d \n",minNode2.value); printf("after size %d \n",getNotNullNodesSize()); //声明2个节点生产的父节点对象 Node mainNode; //将子节点的值相加赋给父节点 mainNode.value = minNode1.value+minNode2.value; mainNode.isNull = 0; mainNode.isLastNode = 0; //将小值节点地址赋给左节点的指针变量 mainNode.leftNode = &minNode1; mainNode.rightNode = &minNode2; minNode1.parentNode = &mainNode; minNode2.parentNode = &mainNode; //将父节点插入到数组中的无效节点位置 addNodetoList(mainNode); return getHaffTree(); }else if(size = 2){ Node minNode1 = getMinValueNode(); Node minNode2 = getMinValueNode(); //声明2个节点生产的父节点对象 Node mainNode; //将子节点的值相加赋给父节点 mainNode.value = minNode1.value+minNode2.value; mainNode.isNull = 0; mainNode.isLastNode = 0; //将小值节点地址赋给左节点的指针变量 mainNode.leftNode = &minNode1; mainNode.rightNode = &minNode2; minNode1.parentNode = &mainNode; minNode2.parentNode = &mainNode; return mainNode; } Node n; return n; } //定义声明的初始化节点方法 void initNodes(){ nodes[0].name="A"; nodes[0].value=5; nodes[0].isNull=0; nodes[0].isLastNode = 1; nodes[1].name="B"; nodes[1].value=4; nodes[1].isNull=0; nodes[1].isLastNode = 1; nodes[2].name="C"; nodes[2].value=3; nodes[2].isNull=0; nodes[2].isLastNode = 1; nodes[3].name="D"; nodes[3].value=2; nodes[3].isNull=0; nodes[3].isLastNode = 1; nodes[4].name="E"; nodes[4].value=1; nodes[4].isNull=0; nodes[4].isLastNode = 1; } //定义计算节点数量的方法 int getNodesSize(){ return sizeof(nodes)/sizeof(sigleNode); } int getNotNullNodesSize(){ int size = getNodesSize(); int reallSize=0; for(int i = 0;i<size;i++){ if(isNullNode(nodes[i]) ==0){ reallSize++; } } return reallSize; } //判断是否是空节点 int isNullNode(Node n){ if(n.isNull == 1){ return 1; }else{ return 0; } } //定义获取值最小的节点方法 Node getMinValueNode(){ int size = getNodesSize(); Node minNode = getValidNode(0); int index = 0; for(int i=0;i<size;i++){ Node n = nodes[i]; //空值节点不判断 if(isNullNode(n)==0 && n.value <= minNode.value){ minNode = n; index = i; } } Node empNode; empNode.isNull = 1; nodes[index] = empNode; return minNode; } //定义获取第几个有效节点的方法 Node getValidNode(int index){ int size = getNodesSize(); int continueNum = index; Node rs; for(int i=0;i<size;i++){ Node n = nodes[i]; //空值节点不判断 if(isNullNode(n)==0 && continueNum ==0){ rs = n; break; } if(isNullNode(n) == 0){ continueNum--; } } return rs; } //定义将节点插入到数组节点中无效节点的方法 void addNodetoList(Node insertN){ int size = getNodesSize(); for(int i=0;i<size;i++){ Node n = nodes[i]; //如果是空白节点则，更新该位置为要插入的节点 if(isNullNode(n)==1){ nodes[i] = insertN; break; } } } I debug for the main method, before into the viewTree(Node *N) method,the mainNode like pic1: ![进入遍历树节点方法前的mainNode](https://img-ask.csdn.net/upload/201608/03/1470204966_754823.png) but when I debug into the viewTree(Node *n) method. thie mainNode like pic2?! ![进入遍历树节点方法时候的mainNode](https://img-ask.csdn.net/upload/201608/03/1470204999_439482.png) who can help me to explain such problem? Thanks !

c语言中的先序插入二叉树怎么写？

#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 100 typedef struct node {char name[10] ; struct node *lchild, *rchild; }BTree; BTree *q[MAX]; /*存储队列元素的数组*/ BTree *CreatBtree(); /* 创建二叉家族树 */ void ScanLevel(BTree *bt); /* 按层次顺序遍历二叉家族树 */ int FindAncestry(BTree *bt,char *ch,int flag); void Precreat(BTree *bt); void InorderTraverse(BTree *bt); int main() { BTree *head; int ch; char cname[10]; while(1) { printf("请选择以下操作：\n"); printf ("一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一\n"); printf("1……创建一个二叉家族树\n"); printf("2……按层次输出二叉家族树\n"); printf("3……输出某个成员的所有祖先成员\n"); printf("4……结束程序\n"); printf ("一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一\n"); printf("请选择(1--4)："); scanf("%d",&ch); getchar(); switch(ch) { case 1: /* 创建二叉家族树 */ Precreat(head); printf("\n二叉家族树创建成功！\n"); printf("\n请按任意键返回主菜单..."); getchar(); break; case 2:printf("按层次输出二叉家族树：\n");/* 按层次输出家族树 */ ScanLevel(head); printf("\n中序遍历输出二叉树\n"); InorderTraverse(head);//中序遍历整个数组 printf("\n请按任意键返回主菜单..."); break; case 3:printf("\n请输入要查找哪个成员的祖先：\n"); scanf("%s",cname); getchar(); FindAncestry(head,cname,0); printf("\n请按任意键返回主菜单..."); break; case 4: exit(0); } getchar(); } } void ScanLevel(BTree *bt) /*按层次顺序遍历二叉家族树 */ { int front=0,rear=0; /* 初始化队列的头、尾指针 */ if(bt!=NULL) { rear++; q[rear]=bt; /* 将bt所指向的结点入队 */ } while(front!=rear) /* 队列非空 */ { front++; bt=q[front]; /* 队头结点出队 */ printf (" %s",bt->name); /* 输出该结点的数据 */ if(bt->lchild!=NULL) { rear++; q[rear]=bt->lchild; /* 将左孩子结点入队 */ } if(bt->rchild!=NULL) {rear++; q[rear]=bt->rchild; /* 将右孩子结点入队 */ } } } int FindAncestry(BTree *bt,char *ch,int flag) { int flag1=0,flag2=0; int cond; if(flag==0&&strcmp(bt->name,ch)==0) { printf("这个人是最大的祖先!!!\n"); return 0; } flag=1; if(bt==NULL||(bt->lchild==NULL&&bt->rchild==NULL)) return 0; cond=(bt->lchild!=NULL)&&(strcmp(bt->lchild->name,ch)==0); cond=cond||((bt->rchild!=NULL)&&(strcmp(bt->rchild->name,ch)==0)); if(cond) { printf("%s，",bt->name); return 1; } else { flag1=FindAncestry(bt->lchild,ch,flag); flag2=FindAncestry(bt->rchild,ch,flag); if(flag1||flag2) { printf("%s，",bt->name); return 1; } else return 0; } } void Precreat(BTree *bt) { char ch1[10]; scanf("%s",ch1); if(strcmp(ch1,"\$")) { bt=NULL; } else { bt=(BTree*)malloc(sizeof(BTree)); strcpy(bt->name,ch1); Precreat(bt->lchild); Precreat(bt->rchild); } printf("建立完成"); return; } void InorderTraverse(BTree *bt) //中序遍历整个数组 { if(bt) { InorderTraverse(bt->lchild); printf(" %s",bt->name); InorderTraverse(bt->rchild); } } 写precreat函数，要求\$为结束符@为空

c语言建立二叉树怎么输入多组数据

#include <stdio.h> #include <stdlib.h> typedef struct BiTNode { char data; struct BiTNode *lchild,*rchild; } BiTNode,*BiTree;//定义结点类型 BiTree CreateBiTree()//创建树 { char p;BiTree T; scanf("%c",&p); if(p == '\$') T=NULL; else { T=(BiTNode *)malloc(sizeof(BiTNode));//为结点开辟空间 T->data=p; T->lchild=CreateBiTree(); T->rchild=CreateBiTree(); } return (T); } void PostOrder(BiTree T)//后序 { if(T!=NULL) { PostOrder(T->lchild); PostOrder(T->rchild); printf("%c",T->data); } } void main()//主函数 { BiTree Ta; Ta=CreateBiTree(); PostOrder(Ta); }

C语言 如何将二叉树中序遍历的结果存入数组

C语言 如何将二叉树中序遍历的结果存入一个数组，我的代码这段是用递归写的，但不知道如何将值传到数组中？ ``` void treeprint(struct tnode *p) { if(p!=NULL){ treeprint(p->left); printf("%s %d\n",p->word,p->count); treeprint(p->right); } } ``` 我的想法是将每个p->word （指的是单词）存入二维字符型数组中 将每个p->count（指的是单词的个数）存入整型数组中，但不知道如何将值传到数组中？

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation. He's given Rikka many math tasks to practice but she hasn't solved any of them. So, today he comes up with a simple problem to help her build up confidence: Here is a tree with m nodes, you can delete some of the nodes on the tree and there mustn't be any edges connecting two remained nodes. You need to maximize the number of the points remained. Rikka thinks this task is too simple, so she comes up with a new problem: At first there is a tree with only one node. And then each time she links a new node to the tree. After each operation, you need to tell her the maximum number of the points remained (as described above). This problem is too difficult for Rikka to solve. Can you help her? Input There are no more than 100 testcases and there are no more than 3 testcases with n>103. For each testcase, the first line contains a number n (1≤n≤105). Then n−1 lines follow. The ith line contains a single number fi (0≤fi<i), which means that after the ith operation there is a new node numbered i and there is an edge between node i and node fi. Output For each operation you need to print a single line with a single number - the answer after this operation. Sample Input 4 0 0 1 Sample Output 1 2 2

“亚马逊丛林里的蝴蝶扇动几下翅膀就可能引起两周后美国德州的一次飓风……” 这句人人皆知的话最初用来描述非线性系统中微小参数的变化所引起的系统极大变化。 而在更长的时间尺度内，我们所生活的这个世界就是这样一个异常复杂的非线性系统…… 水泥、穹顶、透视——关于时间与技艺的蝴蝶效应 公元前3000年，古埃及人将尼罗河中挖出的泥浆与纳特龙盐湖中的矿物盐混合，再掺入煅烧石灰石制成的石灰，由此得来了人...

loonggg读完需要3分钟速读仅需 1 分钟大家好，我是你们的校长。我之前讲过，这年头，只要肯动脑，肯行动，程序员凭借自己的技术，赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

MySQL数据库面试题（2020最新版）

HashMap底层实现原理，红黑树，B+树，B树的结构原理 Spring的AOP和IOC是什么？它们常见的使用场景有哪些？Spring事务，事务的属性，传播行为，数据库隔离级别 Spring和SpringMVC，MyBatis以及SpringBoot的注解分别有哪些？SpringMVC的工作原理，SpringBoot框架的优点，MyBatis框架的优点 SpringCould组件有哪些，他们...

《经典算法案例》01-08：如何使用质数设计扫雷（Minesweeper）游戏

《Oracle Java SE编程自学与面试指南》最佳学习路线图（2020最新版）