c語言單鏈表以及二叉樹中創建時,編寫函數,輸入字元序列,建立二叉樹的二叉鏈表

本文目錄一覽:

請問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;

}

擴展資料:

簡單二叉樹定義範例:此樹的順序結構為: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);

}

}

數據結構 c語言版二叉樹(1) 建立一棵含有n個結點的二叉樹,採用二叉鏈表存儲;

#includestdio.h

#includestdlib.h

typedef struct node *tree_pointer;

struct node{

char ch;

tree_pointer left_child,right_child;

};

tree_pointer root=NULL;

tree_pointer create(tree_pointer ptr)

{

char ch;

scanf(“%c”,ch);

if(ch==’ ‘)

ptr=NULL;

else{

ptr=(tree_pointer)malloc(sizeof(node));

ptr-ch=ch;

ptr-left_child=create(ptr-left_child);

ptr-right_child=create(ptr-right_child);

}

return ptr;

}

void preorder(tree_pointer ptr)

{

if(ptr){

printf(“%c”,ptr-ch);

preorder(ptr-left_child);

preorder(ptr-right_child);

}

}

void inorder(tree_pointer ptr)

{

if(ptr){

inorder(ptr-left_child);

printf(“%c”,ptr-ch);

inorder(ptr-right_child);

}

}

void postorder(tree_pointer ptr)

{

if(ptr){

postorder(ptr-left_child);

postorder(ptr-right_child);

printf(“%c”,ptr-ch);

}

}

void main()

{

printf(“構建一個二叉樹(結點數為n):\n”);

root=create(root);

printf(“前序遍歷二叉樹:\n”);

preorder(root);

printf(“\n”);

printf(“中序遍歷二叉樹:\n”);

inorder(root);

printf(“\n”);

printf(“後序遍歷二叉樹:\n”);

postorder(root);

printf(“\n”);

}

如何用C語言用順序表和鏈表分別建一個二叉樹

#includestdio.h

typedef struct node

{

char data;

struct node *lchild;

struct node *rchild;

}BitTree;

BitTree *creat_bt(int n);

BitTree *p[10];

int main()

{

BitTree *h;

int n;

printf(“請輸入你節點數\n”);

scanf(“%d”,n);

h=creat_bt(n);

return 0;

}

BitTree *creat_bt(int n)

{

BitTree *s,*t;

int j,i;

while(n–)

{

s=new(BitTree);

scanf(“%d%c”,i,s-data);

s-lchild=s-rchild=NULL;

p[i]=s;

if(i==1)

t=s;

else

{

j=i/2;

if(i%2==0)

p[j]-lchild=s;

else

p[j]-rchild=s;

}

}

return t;

}

這個是我用鏈表創建的二叉樹

原創文章,作者:BEUW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/146747.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
BEUW的頭像BEUW
上一篇 2024-10-31 15:32
下一篇 2024-10-31 15:32

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python中capitalize函數的使用

    在Python的字元串操作中,capitalize函數常常被用到,這個函數可以使字元串中的第一個單詞首字母大寫,其餘字母小寫。在本文中,我們將從以下幾個方面對capitalize函…

    編程 2025-04-29
  • 利用Python實現兩個鏈表合併為一個有序鏈表

    對於開發工程師來說,實現兩個鏈表合併為一個有序鏈表是必須掌握的技能之一。Python語言在鏈表處理上非常便利,本文將從多個方面詳細闡述如何利用Python實現兩個鏈表合併為一個有序…

    編程 2025-04-29
  • Python中set函數的作用

    Python中set函數是一個有用的數據類型,可以被用於許多編程場景中。在這篇文章中,我們將學習Python中set函數的多個方面,從而深入了解這個函數在Python中的用途。 一…

    編程 2025-04-29
  • 三角函數用英語怎麼說

    三角函數,即三角比函數,是指在一個銳角三角形中某一角的對邊、鄰邊之比。在數學中,三角函數包括正弦、餘弦、正切等,它們在數學、物理、工程和計算機等領域都得到了廣泛的應用。 一、正弦函…

    編程 2025-04-29
  • 單片機列印函數

    單片機列印是指通過串口或並口將一些數據列印到終端設備上。在單片機應用中,列印非常重要。正確的列印數據可以讓我們知道單片機運行的狀態,方便我們進行調試;錯誤的列印數據可以幫助我們快速…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變數時顯示的指定變數類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29
  • Python實現計算階乘的函數

    本文將介紹如何使用Python定義函數fact(n),計算n的階乘。 一、什麼是階乘 階乘指從1乘到指定數之間所有整數的乘積。如:5! = 5 * 4 * 3 * 2 * 1 = …

    編程 2025-04-29
  • Python定義函數判斷奇偶數

    本文將從多個方面詳細闡述Python定義函數判斷奇偶數的方法,並提供完整的代碼示例。 一、初步了解Python函數 在介紹Python如何定義函數判斷奇偶數之前,我們先來了解一下P…

    編程 2025-04-29
  • 分段函數Python

    本文將從以下幾個方面詳細闡述Python中的分段函數,包括函數基本定義、調用示例、圖像繪製、函數優化和應用實例。 一、函數基本定義 分段函數又稱為條件函數,指一條直線段或曲線段,由…

    編程 2025-04-29

發表回復

登錄後才能評論