当前位置:首页 » 编程语言 » c语言二叉树

c语言二叉树

发布时间: 2022-01-09 04:23:48

A. c语言二叉树

#include <stdlib.h>
#include<malloc.h>
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;

}tnode;
tnode *createtree()
{
tnode *t;
char ch;
ch=getchar();
if(ch=='0')
t=NULL;
else
{
t=(tnode *)malloc(sizeof(tnode));
t->data=ch;
t->lchild=createtree();
t->rchild=createtree();

}
return t;

}
void listtree(tnode *t)
{
if (t!=NULL)
{
printf("%c",t->data);
if(t->lchild!=NULL||t->rchild!=NULL)
{
printf("(");
listtree(t->lchild);
if(t->rchild!=NULL)
printf(",");
listtree(t->rchild);
printf(")");

}
}
}
void inorder(tnode *t)
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%c\t",t->data);
inorder(t->rchild);
}
}
void leve(tnode *t)
{
tnode *quee[100];
int front,rear;
front=-1;
rear=0;
quee[rear]=t;
while(front!=rear)
{
front++;
printf("%c\t",quee[front]->data);
if(quee[front]->lchild!=NULL)
{
rear++;
quee[rear]=quee[front]->lchild;

}
if(quee[front]->rchild!=NULL)
{
rear++;
quee[rear]=quee[front]->rchild;
}
}
}
main()
{
tnode *t=NULL;
printf("请输入二叉树元素:");
t=createtree();
printf("广义表的输出:");
listtree(t);
printf("\n");
printf("二叉树的中序遍历:");
inorder(t);
printf("\n");
printf("二叉树的层次遍历:");
leve(t);
printf("\n");
system("pause");
}
/*
输入:AB00CD00E00F000
输出:A(B,C((D,E))
中序遍历: B A D C E
层次遍历:A B C D E
*/
另外,团IDC网上有许多产品团购,便宜有口碑

B. C语言 二叉树

#include<stdio.h>

#include<stdlib.h>


typedef struct Node

{

int e;

struct Node *l, *r;

} Node;


Node *init() //先序遍历构造二叉树

{

char n;

Node *p;

scanf("%c", &n);

if (n=='0')

return NULL;

p = (Node*)malloc(sizeof(Node));

if (!p)

exit(0);

p->e = n-'0';

p->l = init();

p->r = init();

return p;

}


void DLR(Node *head) //先序遍历二叉树(递归算法

{

if (head)

{

printf("%d", head->e);

DLR(head->l);

DLR(head->r);

}

}

void destory(Node *head) //销毁二叉树

{

Node *l, *r;

if (!head)

return;

l = head->l;

r = head->r;

free(head);

destory(l);

destory(r);

}

int main()

{

Node *head = init();

DLR(head);

destory(head);

return 0;

}

C. 关于c语言二叉树

完全二叉树除了第一层和最后一层外,其余各层的结点数都是2的幂,所以都是偶数,因此,当最后一层的结点数为偶数时,树的总结点数才可能是奇数.
而完全二叉树只有最后一层的结点数为奇数时,树中才可能存在唯一的度为1的结点,即最后一个结点的父结点.但现在最后一层结点数为偶数,所以树中不存在度为1的结点,即n1=0.
所以n=n0+n2=2n0-1=699,n0=350

D. c语言 二叉树

等级考试不考树的语言方面啊。知道原理就行了。那里是很难你做做卷子啊,很快就明白了。插入删除不同吧,别都背下来,它考的时候有侧重,我记得我考试的时候就是做的历年的卷子,真的不难。语言都是简单的英语,没基础也没有关系的。考试刚考过一次了,3月第三那个周末,9月第3个周末吧。。。。上机我觉得简单,你要相信自己人品超好能抽到简单题,我那道编程真的不会啊,最后就靠前面的那60都写了刚好六十。建议你买套卷子来看把,比看书有效果多了。上机有题库的。06年以后的都能用教育出版社好像。是在不明白就背过来。我这大家都是那么过的。。。

E. 请问C语言如何创建二叉树

创建二叉树的源程序如下:

#include <cstdlib>

#include <stdio.h>

typedef struct node

{ //树的结点

int data;

struct node* left;

struct node* right;

} Node;

typedef struct

{ //树根

Node* root;

} Tree;

void insert(Tree* tree, int value)//创建树

{

Node* node=(Node*)malloc(sizeof(Node));//创建一个节点

node->data = value;

node->left = NULL;

node->right = NULL;

if (tree->root == NULL)//判断树是不是空树

{

tree->root = node;

}

else

{//不是空树

Node* temp = tree->root;//从树根开始

while (temp != NULL)

{

if (value < temp->data)//小于就进左儿子

{

if (temp->left == NULL)

{

temp->left = node;

return;

}

else

{//继续判断

temp = temp->left;

}

}

else {//否则进右儿子

if (temp->right == NULL)

{

temp->right = node;

return;

}

else {//继续判断

temp = temp->right;

}

}

}

}

return;

}

void inorder(Node* node)//树的中序遍历

{

if (node != NULL)

{

inorder(node->left);

printf("%d ",node->data);

inorder(node->right);

}

}

int main()

{

Tree tree;

tree.root = NULL;//创建一个空树

int n;

scanf("%d",&n);

for (int i = 0; i < n; i++)//输入n个数并创建这个树

{

int temp;

scanf("%d",&temp);

insert(&tree, temp);

}

inorder(tree.root);//中序遍历

getchar();

getchar();

return 0;

}


(5)c语言二叉树扩展阅读:

简单二叉树定义范例:此树的顺序结构为:ABCDE

#include <cstdlib>

#include <stdio.h>

#include <string>

int main()

{

node* p = newnode;

node* p = head;

head = p;

string str;

cin >> str;

creat(p, str, 0)//默认根结点在str下标0的位置

return 0;

}

//p为树的根结点(已开辟动态内存),str为二叉树的顺序存储数组ABCD##E或其他顺序存储数组,r当前结点所在顺序存储数组位置

void creat(node* p, string str, int r)

{

p->data = str[r];

if (str[r * 2 + 1] == '#' || r * 2 + 1 > str.size() - 1)p->lch = NULL;

else

{

p->lch = newnode;

creat(p->lch, str, r * 2 + 1);

}

if (str[r * 2 + 2] == '#' || r * 2 + 2 > str.size() - 1)p->rch = NULL;

else

{

p->rch = newnode;

creat(p->rch, str, r * 2 + 2);

}

}

F. 关于C语言二叉树!

对于你的这种情况我觉得比较适合用数组来实现。对于长度为t的输入,申请类型为Node、长度为t的数组nodeArray[t],然后进行两次遍历。
第一次,nodeArray[i].data对应输入的第i个字符,nodeArray[i].lchild和rchild都为空;(如果输入#则nodeArray[i]=null)
第二次,在[0, n-1]的范围内,令nodeArray[i].lchild = &(nodeArray[i * 2]),nodeArray[i].rchild = &(nodeArray[i * 2 + 1])。
完成后,nodeArray[0]即为所求二叉树。
应该有办法一次遍历就构造好这棵树,懒得想了。

G. 关于C语言二叉树

首先二叉树的结点是由做孩子指针*lchild 右孩子指针*rchild 以及数据成员data

L表示左孩子R表示右孩子T表示他们的父结点

后序遍历的访问顺序是LRT
中序遍历的访问顺序是LTR
前序遍历的访问顺序是TLR

其中说的前中后就是指访问父结点的次序;

拓扑图在这里没法给出啊。。。

--------------------------------------------

这是我用C++类写的二叉树的头文件,里面有几个函数你可能用不到,你主要看看那几个遍历函数

#include<iostream>

using namespace std;

typedef char elemType;

struct bnode
{
bnode *lchild,*rchild;
elemType data;
};

class BinaryTree
{
public:
BinaryTree();
void create(bnode* &tempR);
void visite(bnode *T);
void preorder(bnode *T);
void inorder(bnode *T);
void postorder(bnode *T);
int high(bnode *T);
void convert(bnode* &tempR,string &a,int i);
void (bnode *T,bnode *&T1);
void level(bnode *T,int i);
void swap(bnode *T);
bnode *root;
private:
int count;
};

BinaryTree::BinaryTree()
{
root = NULL;
count = 0;
}

void BinaryTree::create(bnode* &tempR)
{
elemType x;
cin>>x;
if(x == '.')
{
tempR = NULL;
}
else
{
tempR = new bnode;
count++;
tempR->data = x;
create(tempR->lchild);
create(tempR->rchild);
}
}

void BinaryTree::visite(bnode *T)
{
if(T!=NULL)
cout<<T->data<<' ';
}

void BinaryTree::preorder(bnode *T)
{
if(T!=NULL)
{
visite(T);
preorder(T->lchild);
preorder(T->rchild);
}
}

void BinaryTree::inorder(bnode *T)
{
if(T!=NULL)
{
inorder(T->lchild);
visite(T);
inorder(T->rchild);
}
}

void BinaryTree::postorder(bnode *T)
{
if(T!=NULL)
{
postorder(T->lchild);
postorder(T->rchild);
visite(T);
}
}

int BinaryTree::high(bnode *T)
{
if(T==NULL)
return 0;
else if(high(T->lchild)>high(T->rchild))
return high(T->lchild)+1;
else
return high(T->rchild)+1;
}

void BinaryTree::level(bnode *T,int i)
{
if(T!=NULL)
{
level(T->lchild,i+1);
visite(T);
cout<<i<<' ';
level(T->rchild,i+1);
}
}

void BinaryTree::convert(bnode *&T,string &a,int i)
{
elemType x;
if(i<=a.length())
{
x = a[i-1];
T = new bnode;
count++;
T->data = x;
convert(T->lchild,a,2*i);
convert(T->rchild,a,2*i+1);
}
else
{
T=NULL;
}
}

void BinaryTree::(bnode *T,bnode *&T1)
{
elemType x;
if(T!=NULL)
{
x=T->data;
if(x == '.')
{
T1 = NULL;
}
else
{
T1 = new bnode;
T1->data = x;
T1->lchild = NULL;
T1->rchild = NULL;
(T->lchild,T1->lchild);
(T->rchild,T1->rchild);
}

}
}

void BinaryTree::swap(bnode *T)
{
if(T!=NULL)
{
bnode *temp;
temp=T->lchild;
T->lchild=T->rchild;
T->rchild=temp;
swap(T->lchild);
swap(T->rchild);
}
}

H. c语言绘制二叉树

你那里是打印出的啥?不会是没有存下数据打印了乱码吧?:)

[修改]
比如,我把和你的二叉树相关的代码去掉,改了一下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <graphics.h>

int main()
{
char str[10];
int x = 100, y = 100;
int e = 9;

/* select a driver and mode that supports */
/* multiple drawing colors. */
int gdriver = DETECT, gmode = VGA, errorcode;

detectgraph(&gdriver, &gmode);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "d:\\bc\\bgi");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(x,y,9,9);
setcolor(WHITE);
circle(x,y,10);

sprintf(str,"%d",e);
outtextxy(x-3,y-2,str);

/* clean up */
getch();
/* colse */
closegraph();

return 0;
}
就能在圈圈里打印出"9"

I. 二叉树c语言实现

#include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char data;
struct node *lchild,*rchild;//
}BiTNode,*BiTree;
void CreatBiTree(BiTree &T)
{
char ch;
ch=getchar();
if (ch == ' ')
T = 0;
else {
T=(BiTNode*)malloc(sizeof(BiTNode));
T->data=ch;//生成根节点
CreatBiTree(T->lchild);//构造左子树
CreatBiTree(T->rchild);//构造右子树
}
}
void preorder(BiTree T)//前序遍历
{
if (T!=NULL){
printf ("%c",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
void inorder(BiTree T)//中序遍历
{
if (T!=NULL){
inorder(T->lchild);
printf ("%c",T->data);
inorder(T->rchild);
}
}
void postorder(BiTree T)//后序遍历
{
if (T!=NULL){
postorder(T->lchild);
postorder(T->rchild);
printf ("%c",T->data);
}
}
void main ()
{
cout<<"请输入要创建的二叉树包括空格:"<<endl ;
BiTree T;
CreatBiTree(T);//创建二叉树
cout<<"前序遍历的结果为:"<<endl;
preorder(T);
cout<<endl;
cout<<"中序遍历的结果为:"<<endl;
inorder(T);
cout<<endl;
cout<<"后序遍历的结果为:"<<endl;
postorder(T);
}

J. C语言二叉树

我试着来解答一下。这是一个递归函数。首先要理解T、L、R的含义。
假如L[i]=x1,R[i]=x2,那么节点i的左右孩子分别就是x1,x2.
那么T[x1]=i,T[x2]=i,就是指x1,x2的双亲节点就是i。
Status Dencend(Array1D L, Array1D R, int n, int u, int v, Array1D T)
/******************************************************************/
{
int i;
for(i=1;i<=n;i++)
{
T[i]=0;
}
for(i=1;i<=n;i++)
{
T[L[i]]=i; //你现在看看上边的话,你是否能看懂呢?
T[R[i]]=i; //这里说的意思就是让L[i] R[i]节点认祖归宗,让其知道自己的双亲节点是谁。
}
if(T[u]==v)return TRUE;//如果节点u的双亲节点是v,那么皆大欢喜,返回true
if(T[u])//如果u有父节点的话
return Dencend(L,R,n,T[u],v,T);//那么就看节点u的父节点是否是节点v的子节点。
return FALSE;
}

我都解释的这么明白了,给分啊!!!!!!!!!!!!!!!!!!

热点内容
单片机android 发布:2024-09-20 09:07:24 浏览:760
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:660
如何更换服务器网站 发布:2024-09-20 08:42:34 浏览:306
子弹算法 发布:2024-09-20 08:41:55 浏览:284
手机版网易我的世界服务器推荐 发布:2024-09-20 08:41:52 浏览:812
安卓x7怎么边打游戏边看视频 发布:2024-09-20 08:41:52 浏览:158
sql数据库安全 发布:2024-09-20 08:31:32 浏览:90
苹果连接id服务器出错是怎么回事 发布:2024-09-20 08:01:07 浏览:503
编程键是什么 发布:2024-09-20 07:52:47 浏览:655
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:479