c語言排序樹,c++排序庫函數

本文目錄一覽:

c語言 二叉排序樹 100分

#include stdio.h #include stdlib.h typedef struct _BiTNode { int val; struct _BiTNode* lhs; struct _BiTNode* rhs; }BiTNode; //建立二叉排序樹 void inserttree(BiTNode** Tree, int val) //插入函數 { BiTNode* t, *p, *n; t = (BiTNode*)malloc(sizeof(BiTNode)); t-val = val; t-lhs = t-rhs = NULL; p = NULL, n = *Tree; while(n) { p = n; n = val n-val ? n-lhs : n-rhs; } (p ? val p-val ? p-lhs : p-rhs : *Tree) = t; } void buildBST(BiTNode** Tree) { int val; char c; *Tree = NULL; while(1) { scanf(“%d”, val); inserttree(Tree, val); if((c = getchar()) == ‘\n’) break; else ungetc(c, stdin); } } //二叉排序樹查找 BiTNode* search(BiTNode* Tree, int val) { while(Tree) { if(val Tree-val) Tree = Tree-lhs; else if(val Tree-val) Tree = Tree-rhs; else return Tree; } return NULL; } //二叉排序樹刪除 void del(BiTNode* Tree) { if(Tree) { del(Tree-lhs); del(Tree-rhs); free(Tree); } } int main() { return 0; }

用C語言實現二叉排序樹的查找、插入和刪除

#includestdio.h

//二分查找法或折半查找法

void main(){

int a[10]={1,3,5,9,13,16,17,26,38},count=0;//記錄查找了多少次

//必須是有次的數組

int key,mid;//要查找的數字和折半後的下標

int pos=-1;//查找到的位置

int i=0,j=8;

printf(“請輸入要查找的數據:”);

scanf(“%d”,key);

while(i=j){

count++;

mid=(i+j)/2;

if(key==a[mid]){

pos=mid+1;

break;

}

if(keya[mid])i=mid+1;

else j=mid-1;

}

if(pos==-1)

printf(“對不起,沒有找到你想要的數據!\n”);

else

printf(“該數據位於數組的第%d位\n共查找了%d次\n”,pos,count);

}

刪除

#includestdio.h

void main(){

int i,a[10]={1,3,4,5,7};

//下面看如何刪除數組元素

//其實跟插入數據相反的道理

//比如刪除a[1]這個元素,我們可以通過移動依次覆蓋相應的位置

/*a[1]=a[2];

a[2]=a[3];

a[3]=a[4];*/

//這時候a[1]就已經被刪除了

//把這段寫成循環

for(i=1;i=3;++i)

a[i] = a[i+1];

for(i=0;i4;++i)

printf(“%d “,a[i]);

}

C語言2叉排序樹的問題,急

struct tree

{

int num;

struct tree *left;

struct tree *right;

};

int a[10]={4,9,0,1,8,6,3,5,2,7}

main()

{

int i,j,k;

struct tree *head,*p1,*p2,*p3;

p1-num=a[0];

p1-left=null;

p1-right=null;

head=p1;

for(i=0;i10;i++)

{

p2-num=a[0];

p2-left=null;

p2-right=null;

p1=head;

while(1)

{

p3=p1

if(p2-num=p1-num) p1=p1-right;

else p1=p1-left;

if(p1==null)

{

p3-left=p2;

breal;

}

}

}

//以上為建立樹

inorder(head);

//編歷

}

void inorder(struct tree *p)

{

if(p!=null)

{

inorder(p-left);

printf(“%d”,p-num);

inorder(p-right);

}

return;

}

本程序未經過調試,大體思路應該無誤!

二叉排序樹的實現(c語言)

/*二叉樹的基本運算與實現*/

#include stdio.h

#include malloc.h

#define MAXNODE 256

typedef int datatype;

typedef struct BiTNode

{

datatype data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

typedef struct

{

BiTree link;

int flag;

}stacktype;void menu();

int Initiate(BiTree *bt,datatype x);

BiTree InsertL(BiTree bt,datatype x,BiTree parent);

BiTree InsertR(BiTree bt,datatype x,BiTree parent);

BiTree DeleteL(BiTree bt,BiTree parent);

BiTree DeleteR(BiTree bt,BiTree parent);

void PreOrder(BiTree bt);

void InOrder(BiTree bt);

void PostOrder(BiTree bt);

void LevelOrder(BiTree bt);

BiTree Find(BiTree parent,datatype a);

void NRPreOrder(BiTree bt);

void NRInOrder(BiTree bt);

void NRPostOrder(BiTree bt);void main()

{

int n,m=1;

BiTree t; /*clrscr();*/

while(m)

{

menu();

scanf(“%d”,n);

switch(n)

{

case 1:{/*初始化*/

int flag;

datatype x;

printf(“please input head point x:\n”);

scanf(“%d”,x);

flag=Initiate(t,x);

if(flag==1)

printf(“\nInitiate success!”);

else

printf(“\nInitiate fail!”);

break;

}

case 2:{/*建樹*/

break;

}

case 3:{/*插入結點x作為a的左孩子*/

datatype a,x;/*x作為a的左孩子*/

BiTree parent=t;

printf(“please input a and x:\n”);

scanf(“%d%d”,a,x);

parent=Find(parent,a);

parent=InsertL(t,x,parent);

if(parent!=NULL)

t=parent;

break;

}

case 4:{/*插入結點x作為a的右孩子*/

datatype a,x;/*x作為a的右孩子*/

BiTree parent=t;

printf(“please input a and x:\n”);

scanf(“%d%d”,a,x);

parent=Find(parent,a);

parent=InsertR(t,x,parent);

if(parent!=NULL)

t=parent;

break;

}

case 5:{/*刪除結點a的左孩子*/

datatype a;

BiTree parent=t;

printf(“please input a:\n”);

scanf(“%d”,a);

parent=Find(parent,a);

parent=DeleteL(t,parent);

if(parent!=NULL)

t=parent;

break;

}

case 6:{/*刪除結點a的左孩子*/

datatype a;

BiTree parent=t;

printf(“please input a:\n”);

scanf(“%d”,a);

parent=Find(parent,a);

parent=DeleteR(t,parent);

if(parent!=NULL)

t=parent;

break;

}

case 7:{/*遞歸先序遍歷*/

PreOrder(t);

break;

}

case 8:{/*遞歸中序遍歷*/

InOrder(t);

break;

}

case 9:{/*遞歸後序遍歷*/

PostOrder(t);

break;

}

case 10:{/*層次遍歷*/

LevelOrder(t);

break;

}

case 11:{/*先序遍歷的非遞歸實現*/

NRPreOrder(t);

break;

}

case 12:{/*中序遍歷的非遞歸實現*/

NRInOrder(t);

break;

}

case 13:{/*後序遍歷的非遞歸實現*/

NRPostOrder(t);

break;

}

case 0:m=0;

}

}

}

void menu()

{

/*clrscr();*/

printf(“\n”);

printf(“\t\t1.initiate\n\n”);

printf(“\t\t2.create thread\n\n”);

printf(“\t\t3.insert Left\n\n”);

printf(“\t\t4.insert Right\n\n”);

printf(“\t\t5.delete Left\n\n”);

printf(“\t\t6.delete Right\n\n”);

printf(“\t\t7.preorder\n\n”);

printf(“\t\t8.inorder\n\n”);

printf(“\t\t9.postorder\n\n”);

printf(“\t\t10.levelorder\n\n”);

printf(“\t\t11.nrpreorder\n\n”);

printf(“\t\t12.nrinorder\n\n”);

printf(“\t\t13.nrpostorder\n\n”);

printf(“\t\t0.exit\n\n”);

printf(“\n\n\n\tplease select:”);

}

int Initiate(BiTree *bt,datatype x)

{

if((*bt=(BiTNode*)malloc(sizeof(BiTNode)))==NULL)

return 0;

(*bt)-data=x;

(*bt)-lchild=NULL;

(*bt)-rchild=NULL;

return 1;

}

BiTree InsertL(BiTree bt,datatype x,BiTree parent)

{

BiTree p;

if(parent==NULL)

{

printf(“\nerror!\n”);

return NULL;

}

if((p=(BiTNode*)malloc(sizeof(BiTNode)))==NULL)

return NULL;

p-data=x;

p-lchild=NULL;

p-rchild=NULL;

if(parent-lchild==NULL)

parent-lchild=p;

else

{

p-lchild=parent-lchild;

parent-lchild=p;

}

return bt;

}

BiTree InsertR(BiTree bt,datatype x,BiTree parent)

{

BiTree p;

if(parent==NULL)

{

printf(“\nerror!\n”);

return NULL;

}

if((p=(BiTNode*)malloc(sizeof(BiTNode)))==NULL)

return NULL;

p-data=x;

p-lchild=NULL;

p-rchild=NULL;

if(parent-rchild==NULL)

parent-rchild=p;

else

{

p-rchild=parent-rchild;

parent-rchild=p;

}

return bt;

}

BiTree DeleteL(BiTree bt,BiTree parent)

{

BiTree p;

if(parent==NULL||parent-lchild==NULL)

{

printf(“\ndelete error!”);

return NULL;

}

p=parent-lchild;

parent-lchild=NULL;

free(p);

return bt;

}

BiTree DeleteR(BiTree bt,BiTree parent)

{

BiTree p;

if(parent==NULL||parent-rchild==NULL)

{

printf(“\ndelete error!”);

return NULL;

}

p=parent-rchild;

parent-rchild=NULL;

free(p);

return bt;

}

void PreOrder(BiTree bt)

{

if(bt==NULL)

return;

printf(“%5d”,bt-data);

PreOrder(bt-lchild);

PreOrder(bt-rchild);

}

void InOrder(BiTree bt)

{

if(bt==NULL)

return;

InOrder(bt-lchild);

printf(“%5d”,bt-data);

InOrder(bt-rchild);

}

void PostOrder(BiTree bt)

{

if(bt==NULL)

return;

PostOrder(bt-lchild);

PostOrder(bt-rchild);

printf(“%5d”,bt-data);

}

void LevelOrder(BiTree bt)

{

BiTree Queue[MAXNODE];

int front,rear;

if(bt==NULL)

{

return;

}

front = -1;

rear = 0;

Queue[rear] = bt;

while(front!=rear)

{

front++;

printf(“%5d”,Queue[front]-data);

if(Queue[front]-lchild!=NULL)

{

rear++;

Queue[rear]=Queue[front]-lchild;

}

if(Queue[front]-rchild!=NULL)

{

rear++;

Queue[rear]=Queue[front]-rchild;

}

}//end while

}

BiTree Find(BiTree parent,datatype a)

{

BiTree p;

if(parent==NULL)

p=NULL;

else if(parent-data==a)

p=parent;

else

{

p=Find(parent-lchild,a);

if(p==NULL)

p=Find(parent-rchild,a);

}

return p;

}

void NRPreOrder(BiTree bt)

{

BiTree stack[MAXNODE],p;

int top;

if(bt==NULL)

{

printf(“Tree is empty!\n”);

return;

}

top=-1;

p=bt;

while((p!=NULL)||(top!=-1))

{

while(p!=NULL)

{

printf(“%5d”,p-data);

if(top==MAXNODE-1)

{

printf(“Stack overflow!\n”);

return;

} /* end if */

else

{

top++;

stack[top]=p;

} /* end if-else */

p=p-lchild;

} /* end while p */

p=stack[top];

top–;

p=p-rchild;

} /* end while p top */

}

void NRInOrder(BiTree bt)

{

BiTree stack[MAXNODE],p;

int top;

if(bt==NULL)

{

printf(“Tree is empty!\n”);

return;

}

top=-1;

p=bt;

while((p!=NULL)||(top!=-1))

{

while(p!=NULL)

{

if(top==MAXNODE-1)

{

printf(“Stack overflow!\n”);

return;

} /* end if */

else

{

top++;

stack[top]=p;

} /* end if-else */

p=p-lchild;

} /* end while p */

p=stack[top];

top–;

printf(“%5d”,p-data);

p=p-rchild;

} /* end while p top */

}

void NRPostOrder(BiTree bt)

{

stacktype stack[MAXNODE];

BiTree p;

int top,sign;

if(bt==NULL)

{

printf(“Tree is empty!\n”);

return;

}

top=-1;

p=bt;

while((p!=NULL)||(top!=-1))

{

if(p!=NULL) /*結點第一次入棧*/

{

top++;

stack[top].link=p;

stack[top].flag=1; /*標記第一次入棧*/

p=p-lchild;

} /* end if */

else

{

p=stack[top].link;

sign=stack[top].flag;

top–;

if(sign==1) /*結點第二次入棧*/

{

top++;

stack[top].link=p;

stack[top].flag=2; /*標記第二次入棧*/

p=p-rchild;

} /* end if */

else

{

printf(“%5d”,p-data);

p=NULL;

} /* end if-else */

} /* end if-else */

} /* end while */

}

C語言實現:二叉排序樹結點的刪除

else

if((*t)-lchild==NULL)

(*t)=(*t)-rchild;

else

if((*t)-rchild==NULL)

(*t)=(*t)-lchild;

你確定你這個沒有寫錯???左右孩子節點,都為空了你怎麼還進入想進入進去?【估計你是這樣想的】。。。但是安裝你這個代碼,只要左(右)孩子節點為空,你就直接把這個節點該刪了,能不出錯?【把一個空的孩子節點指向該節點】

C語言 關於二叉樹排序樹的建立及中序遍歷程序的調試

看這個吧,跟你的設計一摸一樣,有注釋,你的沒有注釋,有些不怎麼好理解。

#include

stdio.h

#include

stdlib.h

struct

tree//聲明樹的結構

{

struct

tree

*left,*right;

int

data;

};

typedef

struct

tree

treenode;//聲明新類型樹結構

typedef

treenode

*b_tree;//聲明二叉樹鏈表

//插入二叉樹節點

b_tree

insert_node(b_tree

root,

int

node)

{

b_tree

newnode

;//聲明樹根指針

b_tree

currentnode;//聲明目前節點指針

b_tree

parentnode;//聲明父節點指針

//申請新節點的內存空間

newnode=(b_tree)malloc(sizeof(treenode));

newnode-data=node;//存入節點內容

newnode-right=NULL;//初始化右指針

newnode-left=NULL;

if(root==NULL)return

newnode;

else

{

currentnode=root;//存儲目前節點指針

while(currentnode!=NULL)

{

parentnode=currentnode;//存儲父節點指針

if(currentnode-datanode)//比較節點的數值大小

currentnode=currentnode-left;//左子樹

else

currentnode=currentnode-right;//右子樹

}

if(parentnode-datanode)//將父節點和子節點做鏈接

parentnode-left=newnode;//子節點為父節點的左子樹

else

parentnode-right=newnode;//子節點為父節點的右子樹

}

return

root;//返回根節點的指針

}

//建立二叉樹

b_tree

create_btree(int

*data,int

len)

{

b_tree

root=NULL;

for(int

i=0;ilen;i++)

root=insert_node(root,data[i]);

return

root;

}

//二叉樹中序遍歷

void

in_order(b_tree

point)

{

if(point!=NULL)//遍歷的終止條件

{

in_order(point-left);//遍歷左子樹

coutpoint-data”

“;//列印節點的內容

in_order(point-right);//遍歷右子樹

}

}

//主程序

void

main()

{

b_tree

root=NULL;//樹根指針

int

value;//暫存讀入將要遍歷的數值

int

nodelist[20];

int

index=0;

printf(“請輸入將要參與遍歷的數值:\n”);

//讀數值到數組中,

//這是一種不知道數組元素到底有幾個的情況下的輸入方法

scanf(“%d”,value);

while(value!=0)

{

nodelist[index]=value;

index++;

scanf(“%d”,value);

}

//建立二叉樹

root=create_btree(nodelist,index);

//中序遍歷

printf(“中序遍歷二叉樹結果如下\n”);

in_order(root);

printf(“\n”);

}

/*

輸入:6

3

1

9

5

7

4

8

0(退出)

結果:1

3

4

5

6

7

8

9

*/

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
KBKE的頭像KBKE
上一篇 2024-10-04 00:24
下一篇 2024-10-04 00:24

相關推薦

  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28
  • Python基礎語言

    Python作為一種高級編程語言擁有簡潔優雅的語法。在本文中,我們將從多個方面探究Python基礎語言的特點以及使用技巧。 一、數據類型 Python基礎數據類型包括整數、浮點數、…

    編程 2025-04-28

發表回復

登錄後才能評論