本文目錄一覽:
請問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-hant/n/146747.html