當前位置:首頁 » 編程語言 » 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;
}

我都解釋的這么明白了,給分啊!!!!!!!!!!!!!!!!!!

熱點內容
dirt5需要什麼配置 發布:2024-05-20 06:02:58 瀏覽:542
怎麼把電腦鎖上密碼 發布:2024-05-20 05:19:09 瀏覽:985
安卓為什麼連上wifi後沒有網路 發布:2024-05-20 05:17:50 瀏覽:419
安卓usb在設置哪裡 發布:2024-05-20 05:03:03 瀏覽:187
綏化編程 發布:2024-05-20 04:59:44 瀏覽:991
基本原理和從頭計演算法 發布:2024-05-20 04:50:32 瀏覽:30
配置情況指的是什麼 發布:2024-05-20 04:48:14 瀏覽:497
那個程序用來編譯源文件 發布:2024-05-20 04:46:45 瀏覽:551
小程序需要資料庫嗎 發布:2024-05-20 04:35:14 瀏覽:338
鏈接sqlserver 發布:2024-05-20 04:27:53 瀏覽:210