㈠ 數據結構二叉樹的程序,用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);} //從鍵盤讀入建樹
};