当前位置:首页 » 编程语言 » 多期二叉树模型程序C语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

多期二叉树模型程序C语言

发布时间: 2023-08-22 08:01:17

㈠ 数据结构二叉树的程序,用c语言怎么实现

您好,想要实现一个二叉树,需要用到结构体来存储每个节点的信息,并使用指针来存储每个节点的左右子节点的地址。具体的实现方法可以参考下面的代码示例:

#include <stdio.h>

#include <stdlib.h>

struct TreeNode {

int val;

struct TreeNode *left;

struct TreeNode *right;

};

struct TreeNode* createNode(int val) {

struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));

node->val = val;

node->left = NULL;

node->right = NULL;

return node;

}

void insertNode(struct TreeNode* root, int val) {

if (root == NULL) {

return;

}

if (val < root->val) {

if (root->left == NULL) {

root->left = createNode(val);

} else {

insertNode(root->left, val);

}

} else {

if (root->right == NULL) {

root->right = createNode(val);

} else {

insertNode(root->right, val);

}

}

}

void printTree(struct TreeNode* root) {

if (root == NULL) {

return;

}

printf("%d ", root->val);

printTree(root->left);

printTree(root->right);

}

int main() {

struct TreeNode* root = createNode(5);

insertNode(root, 3);

insertNode(root, 2);

insertNode(root, 4);

insertNode(root, 7);

insertNode(root, 6);

insertNode(root, 8);

printTree(root);

return 0;

}

在这段代码中,我们定义了一个结构体 TreeNode 来表示二叉树的每个节点,结构体中包含了一个节点的数值 val,以及指向左子节点和右子节点的指针 left 和 right。

㈡ 完整正确的C语言二叉树程序

我有现成的,分享给大家了。

#include<stdio.h>
#include<stdlib.h>
#define maxsize 100
typedef struct btnode
{
int data ; //结点数据类型
struct btnode *lchild, *rchild; //定义左、右孩子为指针型
} bitree;
bitree *creat(bitree *t) //创建二叉树
{
bitree *s,*p,*q;
int x;
scanf("%d",&x);
while(x!=0)
{
s= ( bitree *)malloc(sizeof(bitree));
s->data=x;
s->lchild=s->rchild=NULL;
if(t==NULL)
t=s;
else
{
p=t;
while(p)
{
q=p;
if(s->data<p->data)
p=p->lchild;
else
p=p->rchild;
}
if(s->data<q->data)
q->lchild=s;
else
q->rchild=s;
}
scanf("%d",&x);
}
return(t);
}
bitree *insert(bitree *t) //插入结点
{
bitree *s,*p,*q;
int x;
scanf("%d",&x);
while(x!=0)
{
s= ( bitree *)malloc(sizeof(bitree));
s->data=x;
s->lchild=s->rchild=NULL;
if(t==NULL)
t=s;
else
{
p=t;
while(p)
{
q=p;
if(s->data<p->data)
p=p->lchild;
else
p=p->rchild;
}
if(s->data<q->data)
q->lchild=s;
else
q->rchild=s;
}
scanf("%d",&x);
}
return(t);
}
void search(bitree *t,int k) //查找数据
{
int flag=0;
while(t!=NULL)
{
if(t->data==k)
{
printf("已查到要找的数!\n");
flag=1;
break;
}
else
if(t->data<k)
t=t->rchild;
else
t=t->lchild;
}
if(flag!=1)
printf("没有找到要查找的数据!\n");
}
bitree *dele(bitree *t,int k) //删除数据
{
int flag;
bitree *p,*q,*s=NULL,*f;
f=t;
while(t!=NULL) //查找数据
{
if(t->data==k)
{
printf("已找到所查找的数!\n");
break;
}
else
if(t->data<k)
{
p=t;
t=t->rchild;
flag=0;
}
else
{
p=t;
t=t->lchild;
flag=1;
}
}
if(t->lchild==NULL&&t->rchild==NULL) //删除叶子结点
{
free(t);
if(flag==0)
p->rchild=NULL;
else
p->lchild=NULL;
}
else
{
if(t->lchild==NULL&&t->rchild!=NULL) //删除只有右子树的结点
{
if(flag==0)
p->rchild=t->rchild;
else
p->lchild=t->rchild;
free(t);
}
else
{
if(t->lchild!=NULL&&t->rchild==NULL) //删除只有左子树的结点
{
if(flag==0)
p->rchild=t->lchild;
else
p->lchild=t->lchild;
free(t);
}
else //删除左右子树都有的结点
{
p=t;
t=t->lchild;
q=t;
while(t->rchild)
{
q=t;
t=t->rchild;
}
if(t==q)
{
p->data=t->data;
p->lchild=t->lchild;
free(t);
}
else
{
p->data=t->data;
q->rchild=t->lchild;
free(t);
}
}
}
}
return(f);
}
void output(bitree * t) //实现二叉树的遍历
{
bitree *q[maxsize],*p;
int f,r;
q[0]=t;
f=r=0;
while (f<=r)
{
p=q[f];
f++;
printf("%d ",p->data);
if(p ->lchild!=NULL)
{
r++;
q[r]=p->lchild;
}
if (p->rchild!=NULL)
{
r++;
q[r]=p->rchild;
}
}
}
void main()
{

bitree *q=NULL,*r;
int m,n,x=1;
while(x==1)
{
system("cls");
printf(" ********************************\n");
printf(" 创建请按1\n");
printf(" 插入请按2\n");
printf(" 查找请按3\n");
printf(" 删除请按4\n");
printf(" 显示请按5\n");
printf(" 退出请按0\n");
printf(" ********************************\n");
scanf("%d",&m);
switch(m)
{
case 1:printf("请输入数据并以'0'结束\n");
r=creat(q);system("pause");break;
case 2:printf("请输入数据并以'0'结束\n");
r=insert(r);break;
case 3:printf("请输入要查找的数:");
scanf("%d",&n);
search(r,n);
system("pause");
break;
case 4:printf("请输入要删除的数:");
scanf("%d",&n);
r=dele(r,n);
printf("已删除输入数据!\n");
system("pause");
break;
case 5:output(r);system("pause");printf("\n");
break;
case 0:x=0;break;
}

}
}

㈢ 求数据结构(C语言版)建立二叉树的代码~~急~~谢谢了

BT.H文件
#include
<stdio.h>
#include
<malloc.h>
#include
<conio.h>
#define
TRUE
1
#define
FALSE
0
#define
ERROR
0
#define
OK
1
#define
Stack_Size
50
#define
NUM
50
#define
MAXSIZE
50
//队列的最大长度
//定义二叉树
typedef
char
DataType;
typedef
struct
Node
{
DataType
data;
struct
Node
*LChild;
struct
Node
*RChild;
}BiTNode,
*BiTree;
//定义stack
typedef
BiTree
StackElementType;
typedef
struct
{
StackElementType
elem[Stack_Size];
int
top;
}SeqStack;
//定义队列
typedef
BiTree
QueueElementType;
typedef
struct
{
QueueElementType
element[MAXSIZE];
int
front;
int
rear;
}SeqQueue;
//队列的抽象
void
InitQueue(SeqQueue
*Q)
{
Q->front=Q->rear=0;
}
int
EnterQueue(SeqQueue
*Q,
QueueElementType
x)
{
if((Q->rear+1)%MAXSIZE==Q->front)
return(FALSE);
Q->element[Q->rear]=x;
Q->rear=(Q->rear+1)%MAXSIZE;
return(TRUE);
}

㈣ 二叉树 C语言实现

编译通过
先序创建并输出
#include <stdio.h>
#include <stdlib.h>

typedef struct BTree{
char data;
struct BTree *lchild;
struct BTree *rchild;
}BinTree;
BinTree *pre_order()
{
BinTree *p;
char ch;
scanf("%c",&ch);
if(ch==' ')
return NULL;
p=(BinTree *)malloc(sizeof(BinTree));
p->data=ch;
p->lchild=pre_order();
p->rchild=pre_order();
return p;
}
BinTree *in_order()
{
BinTree *p;
char ch;
p->lchild=in_order();
scanf("%c",&ch);
if(ch==' ')
return NULL;
p->rchild=in_order();
}
void post_order(BinTree *p)
{
if(p!=NULL)
{
post_order(p->lchild);
post_order(p->rchild);
printf("%c ",p->data);
}
}
void main()
{
BinTree *tree;
printf("Please Enter the pre_order:\n");
tree=pre_order();
printf("\n");
//printf("Please Enter the in_order:\n");
//tree=in_order();
//printf("\n");
post_order(tree);
printf("\n");
}
先序和中序不能同时使用,要么就光先序,要么就再用另一个数做中序

㈤ 数据结构代码(用C语言) 二叉树的操作

# include <stdio.h>

# include <malloc.h>

struct BTNode

{

int data;

struct BTNode * pLchild;//p是指针,L是左,child是孩子

struct BTNode * pRchild;

};

//函数声明

struct BTNode * CreateBTree(void);//创建树

void PreTraverseBTree(struct BTNode * pT);//先序遍历

void InTraverseBTree(struct BTNode * pT);//中序遍历

void PostTraverseBTree(struct BTNode * pT);//后续遍历

int main(void)

{

struct BTNode * pT = CreateBTree();

PreTraverseBTree(pT);

printf("\n");

InTraverseBTree(pT);

printf("\n");

PostTraverseBTree(pT);

return 0;

}

//创建树

struct BTNode * CreateBTree(void)

{

struct BTNode * pA = (struct BTNode * )malloc(sizeof(BTNode));

struct BTNode * pB = (struct BTNode * )malloc(sizeof(BTNode));

struct BTNode * pC = (struct BTNode * )malloc(sizeof(BTNode));

struct BTNode * pD = (struct BTNode * )malloc(sizeof(BTNode));

struct BTNode * pE = (struct BTNode * )malloc(sizeof(BTNode));

pA->data = 'A';

pB->data = 'B';

pC->data = 'C';

pD->data = 'D';

pE->data = 'E';

pA->pLchild = pB;

pA->pRchild = pC;

pB->pLchild = NULL;

pB->pRchild = NULL;

pC->pLchild = pD;

pC->pRchild = NULL;

pD->pLchild = NULL;

pD->pRchild = pE;

pE->pLchild = NULL;

pE->pRchild = NULL;

return pA;

}

//先序遍历

void PreTraverseBTree(struct BTNode * pT)

{ //先访问根节点,再先序访问左子树,最后先序访问右子树

if ( pT != NULL)

{

printf("%c\n",pT->data);//访问根节点

//pT->pLchild可以代表整个左子树

PreTraverseBTree(pT->pLchild);

PreTraverseBTree(pT->pRchild);

}

return;

}

//中序遍历

void InTraverseBTree(struct BTNode * pT)

{

if(pT != NULL )

{

if (NULL != pT->pLchild)

{

InTraverseBTree(pT->pLchild);

}

printf("%c\n",pT->data);

if (NULL != pT->pRchild)

{

InTraverseBTree(pT->pRchild);

}

}

return;

}

//后续遍历

void PostTraverseBTree(struct BTNode * pT)

{

if(pT != NULL )

{

if (NULL != pT->pLchild)

{

PostTraverseBTree(pT->pLchild);

}

if (NULL != pT->pRchild)

{

PostTraverseBTree(pT->pRchild);

}

printf("%c\n",pT->data);

}

return;

}

㈥ 二叉树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);
}

㈦ 用程序完成 完整的二叉树结构的实现及测试(C语言)

源文件:

#include <iostream.h>
#include <stdlib.h>
#include "BinaryTree.h"

template <class T>
BinTreeNode<T> *BinaryTree<T>::Parent (BinTreeNode <T> *subTree, BinTreeNode <T> *t) {
//私有函数: 从结点 subTree 开始, 搜索结点 t 的双
//亲, 若找到则返回双亲结点地址, 否则返回NULL
if (subTree == NULL) return NULL;
if (subTree->leftChild == t || subTree->rightChild == t )
return subTree; //找到, 返回父结点地址
BinTreeNode <T> *p;
if ((p = Parent (subTree->leftChild, t)) != NULL)
return p; //递归在左子树中搜索
else return Parent (subTree->rightChild, t);
//递归在左子树中搜索
};

template<class T>
void BinaryTree<T>::Destroy (BinTreeNode<T> * subTree) {
//私有函数: 删除根为subTree的子树
if (subTree != NULL) {
Destroy (subTree->leftChild); //删除左子树
Destroy (subTree->rightChild); //删除右子树
delete subTree; //删除根结点
}
};

template <class T>
void BinaryTree<T>::InOrder (BinTreeNode<T> * subTree) {
if (subTree != NULL) {
InOrder (subTree->leftChild); //遍历左子树
cout<<subTree->data<<"|"; //访问根结点
InOrder (subTree->rightChild); //遍历右子树
}
};

template <class T>
void BinaryTree<T>::PreOrder (BinTreeNode<T> * subTree) {
if (subTree != NULL) {
cout<<subTree->data<<"|"; //访问根结点
PreOrder (subTree->leftChild); //遍历左子树
PreOrder (subTree->rightChild); //遍历右子树
}
};

template <class T>
void BinaryTree<T>::PostOrder (BinTreeNode<T> * subTree) {
if (subTree != NULL ) {
PostOrder (subTree->leftChild); //遍历左子树
PostOrder (subTree->rightChild); //遍历右子树
cout<<subTree->data<<"|"; //访问根结点
}
};

template <class T>
int BinaryTree<T>::Size (BinTreeNode<T> * subTree){
//私有函数:利用二叉树后序遍历算法计算二叉
//树的结点个数
if (subTree == NULL) return 0; //空树
else return 1+Size (subTree->leftChild) + Size (subTree->rightChild);
};

template <class T>
int BinaryTree<T>::Height ( BinTreeNode<T> * subTree){
//私有函数:利用二叉树后序遍历算法计算二叉
//树的高度或深度
if (subTree == NULL) return 0; //空树高度为0
else {
int i = Height (subTree->leftChild);
int j = Height (subTree->rightChild);
return (i < j) ? j+1 : i+1;
}
};

template<class T>
void BinaryTree<T>::CreateBinTree (T RefValue, BinTreeNode<T> * & subTree) {
//前序序列从键盘输入建立二叉树。
T item;
cin >> item; //读入根结点的值
if (item != RefValue) {
subTree = new BinTreeNode<T>(item); //建立根结点
if (subTree == NULL)
{cerr << "存储分配错!" << endl; exit (1);}
CreateBinTree (RefValue, subTree->leftChild); //递归建立左子树
CreateBinTree (RefValue, subTree->rightChild); //递归建立右子树
}
else subTree = NULL; //封闭指向空子树的指针
};

头文件:

#pragma once

template <class T>
struct BinTreeNode { //二叉树结点类定义
T data; //数据域
BinTreeNode<T> *leftChild, *rightChild; //左子女、右子女链域
BinTreeNode () //构造函数
{ leftChild = NULL; rightChild = NULL; }
BinTreeNode (T x, BinTreeNode<T> *l = NULL, BinTreeNode<T> *r = NULL)
{ data = x; leftChild = l; rightChild = r; }
};

template <class T>
class BinaryTree { //二叉树类定义
protected:
BinTreeNode<T> *root; //二叉树的根指针
T RefValue; //数据输入停止标志
void Destroy (BinTreeNode<T> * subTree); //删除
int Height (BinTreeNode<T> *subTree); //返回树高度
int Size (BinTreeNode<T> *subTree); //返回结点数
BinTreeNode<T> *Parent (BinTreeNode<T> * subTree, BinTreeNode<T> *t); //返回父结点
void PreOrder (BinTreeNode<T>* subTree); //前序遍历
void InOrder (BinTreeNode<T>* subTree); //中序遍历
void PostOrder (BinTreeNode<T>* subTree); //后序遍历
void CreateBinTree (T RefValue, BinTreeNode<T> * & subTree); //从键盘读入建树
public:
BinaryTree () : root (NULL) { } //构造函数
BinaryTree (T value) : RefValue(value), root(NULL) { } //构造函数
~BinaryTree () { Destroy(root); } //析构函数
bool IsEmpty () { return root == NULL;} //判二叉树空否
int Height () { return Height(root); } //求树高度
int Size () { return Size(root); } //求结点数
BinTreeNode<T> *Parent (BinTreeNode <T> *t)
{ return (root == NULL || root == t) ? NULL : Parent (root, t); } //返回双亲结点
BinTreeNode<T> *LeftChild (BinTreeNode<T> *t)
{ return (t != NULL)?t->leftChild : NULL; } //返回左子女
BinTreeNode<T> *RightChild (BinTreeNode<T> *t)
{ return (t != NULL)?t->rightChild : NULL; } //返回右子女
BinTreeNode<T> *GetRoot () const { return root; } //取根
void PreOrder ()
{ PreOrder (root); } //前序遍历
void InOrder ()
{ InOrder (root); } //中序遍历
void PostOrder ()
{ PostOrder (root); } //后序遍历
void CreateBinTree ()
{ CreateBinTree (RefValue, root);} //从键盘读入建树
};