① 用c语言创建一棵二叉树并使其显现出来
#include <stdio.h>//头文件
#include <stdlib.h>
#include <malloc.h>
typedef struct BiTNode//定义节点
{
    char data;//元素类型为字符
    struct BiTNode *lchild,*rchild;//左孩子右孩子
} 
BiTNode,*BiTree;
BiTree CreateBiTree()//用先序递归建树
{
    char p;BiTree T;
    scanf("%c",&p);
    if(p==' ') T=NULL;
    else
    {
        T=(BiTNode *)malloc(sizeof(BiTNode));//开辟空间
        T->data=p;
        T->lchild=CreateBiTree(T->lchild);
        T->rchild=CreateBiTree(T->rchild);
    }
    return (T);
}
void PreOrder(BiTree T)先序遍历
{
     if(T!=NULL)
   {
       printf("%c",T->data);
      PreOrder(T->lchild);
      PreOrder(T->rchild);
       
    }
}
void main()//主函数
{
   BiTree Ta;
    Ta=CreateBiTree();
    printf("树:");
    printf("\n");
   PreOrder(Ta);
    }
就这个吧,挺简单的
② 看了半天用c语言实现二叉树的建立与遍历,居然没看懂。
如何建立二叉树,为了方便理解中递推法
案例1:
#Include <stdio.h>
#include <stdlib.h>
struct Data{
     int num;
     struct Data* left;    // 左节点指针
     struct Data* right;   // 右节点指针
};
typedef struct Data Data;
// 创建节点
Data* create_node(int data){
     Data *node = (Data*)malloc(sizeof(Data));
     node->left = NULL;
     node->right = NULL;
     node->num = data;
     return node;
};
// 给二叉树有序地插入节点
bool insert_node(Data *root, int data){
     while(1){
          if(data == root->num){
                return false;
          }
          else if(data > root->num){  // 如果输入的数值 大于 当前根节点的数值,就插入当前根节点的右边
               if(root->right == NULL){   // 如果当前根的右节点指针为空就插入
                         root->right = create_node(data);
                         return true;
               }else{    // 如果当前根的右节点指针不为空就继续遍历下一个根
                        root = root->right;
               }  
          }
          esle if(data < root->num){
               // 如果输入的数值 小于 当前根节点的数值, 就插入当前根节点的左端
               if(root->left == NULL){ //如果当前根的左节点指针为空就插入
                       root->left = create_node(data);
                       return true;
               }else{   // 如果当前根的左节点不为空,就继续变量下一个根
                       root = root->left; 
               }
          }
     }
}
int main(){
      Data *root = NULL;
      while(1){
           int num;
           printf("请输入数值:");
           scanf("%d",&num);
           if(num < 0)break;    // 如果输入的是负数就退出循环
          
           if(root == NULL){
                  root = create_node(num);     // 创建父树根
           }else{ 
                  if(!insert_node(root)){       // 向二叉树添加节点
                         printf("插入失败\n");
                  }    
           }
      }
}
③ 判断完全二叉树用C语言编写
用一个线性表和一个队列,表存放的是边集,队列用于按层次遍历。程序流程如下
1初始化空表、空队;
2输入结点数、指定根结点,输入边到表中;
3根结点进队;
4将队首出队到p;
5若表为空,返回1(真)。不空则在表中查找第一项等于p的边i。若找到,将边i的第二项进队,从表中删除边i。若没有找到,则返回0(假)。
6若表为空,返回1(真)。不空则在表中查找第二项等于p的边i。若找到,将边i的第一项进队,从表中删除边i。若没有找到,则返回0(假)。
7跳到4。
补充提供一个相应的程序代码如下,你可以试试
#include <stdio.h>
#define N 1024
void main( )
{
 short list[N][2], queue[N], listLength = 0, front = 0, rear = 0, r, n, i, p;/*1 初始化空表,空队*/
 char flag; /*flag是判断结果标识*/
 scanf("%d%d", &n, &r); /*2 输入结点数、指定根结点,输入边到表中*/
 for(i = 0; i < n - 1; i++)
 scanf("%d%d", &list[i][0], &list[i][1]);
 listLength = n - 1;
 queue[rear++] = r;/*3 根结点进队*/
 while(1) {
 p = queue[front++];/*4 将队首出队到p*/
 if(listLength == 0) { /*5 如果表为空,则返回1(真)*/
 flag = 1; 
 break;
 }
 for(i = 0; i < listLength && list[i][0] != p; i++); /*寻找第一项等于p的边i*/
 if(i == listLength) {/*如果没有找到,返回0(假)*/
 flag = 0;
 break;
 }
 queue[rear++] = list[i][1];/*将边i的第二项进队*/
 for(; i < listLength - 1; i++)/*删除边i*/
 list[i][0] = list[i + 1][0], list[i][1] = list[i + 1][1];
 listLength--;
 if(listLength == 0) { /*6 若表为空,返回1(真)*/
 flag = 1;
 break;
 }
 for(i = 0; i < listLength && list[i][1] != p; i++);/*在表中查找第二项等于p的边i*/
 if(i == listLength) { /*如果没有找到,返回0(假)*/
 flag = 0;
 break;
 }
 queue[rear++] = list[i][0]; /*将边i的第一项进队*/
 for(; i < listLength - 1; i++)/*删除边i*/
 list[i][0] = list[i + 1][0], list[i][1] = list[i + 1][1];
 listLength--;
 }/*7 跳到4*/
 if(flag)
 printf("yes
");
 else
 printf("no
");
}
运行结果

④ 二叉树 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 <stdlib.h>
typedef struct BitNode
{
	int data;
	struct BitNode *lchile,*rchild;
}*BiTree;
BiTree CreateBiTree(BiTree &T)
{
	int d;
	scanf("%d",&d);
	if (d<0)
		return NULL;
	else
	{
		if (!(T=(BiTree)malloc(sizeof(BiTree))))
		{
			exit(1);
		}
		T->data=d;//先根序创建二叉树
		printf("%d  ",T->data);
		T->lchile=CreateBiTree(T->lchile);//创建左子树
		T->rchild=CreateBiTree(T->rchild);//创建右子树
		return T;
	}
}
void InOrderView(BiTree &T)//中序遍历二叉树
{
	if(T)
	{
		InOrderView(T->lchile);
		printf("%d  ",T->data);
		InOrderView(T->rchild);
	}
}
void main()
{
	BiTree T;
	T=CreateBiTree(T);//递归创建二叉树
	InOrderView(T);
}
⑥ 数据结构二叉树的创建(c语言版)
//输入的格式为:abd##e##c##
#include "stdlib.h"
typedef int Element;
struct Tree
{
    Element data;
    struct Tree *left;
    struct Tree *right;
};
void CreateBinSortTree(struct Tree **t);
void InsertNode2Tree(struct Tree **root, Element num);
void PrintTree(struct Tree *r, int order);
int main(int argc, char* argv[])
{
    printf("请输入一组字母(#表示子树为空)\n");
    struct Tree *t=NULL;
    CreateBinSortTree(&t);
    
    printf("\n根左右:");
    PrintTree(t,1);
    printf("\n左根右:");
    PrintTree(t,2);
    printf("\n左右根:");
    PrintTree(t,3);
    printf("\n");
    return 0;
}
void CreateBinSortTree(struct Tree **t)
{
    char ch;
    ch = getchar();
    if (ch == '#')
    {
        *t = NULL;
        return ;
    }
    *t = (struct Tree *)malloc(sizeof(struct Tree));
    (*t)->data = ch;
    CreateBinSortTree( &((*t)->left));
    CreateBinSortTree( &((*t)->right));
}
void PrintTree(struct Tree *r, int order)
{
    if (r == NULL)
    {
        return ;
    }
    switch(order)
    {
    
    case 1:
        printf("%c ",r->data);
        PrintTree(r->left,order);
        PrintTree(r->right,order);
        break;
    case 2:
        PrintTree(r->left,order);
        printf("%c ",r->data);
        PrintTree(r->right,order);
        break;
    case 3:
        PrintTree(r->left,order);
        PrintTree(r->right,order);
        printf("%c ",r->data);
        break;
    }
    
}
⑦ 数据结构,完全二叉树问题(用C语言)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
	int elem;
	struct node *lch,*rch;
}Bnode;
Bnode *creat()
{
	Bnode *root =NULL;
	Bnode *s[20];
	int i,x;
	int j;
	printf("input i and x:");
	scanf("%d,%d",&i,&x);
	while(i != 0)
	{
		Bnode *p = (Bnode *)malloc(sizeof(Bnode));
		p->elem = x;
		p->lch = NULL;
		p->rch = NULL;
		s[i] = p;
		if(i == 1)root=p;
		else
		{
			j = i/2;
			if(i%2 == 0)  //编号为偶数
				s[j]->lch = p;
			else
				s[j]->rch = p;
				
		}
		printf("input i and x:");
		scanf("%d,%d",&i,&x);
	}
	return root;
}
void anceng(Bnode *p)    //按层遍历 
{
	Bnode *q = p;
	Bnode *s[20];
	int front = 0;
	int rear = 0;
	if(q != NULL)
	{
		rear++;
		s[rear] = q;
	}
	while(rear != front)
	{
		front++;
		p = s[front];
		printf("%d\n",p->elem);
		if(p->lch != NULL)
		{
			rear++;
			s[rear] = p->lch;
		}
		if(p->rch != NULL)
		{
			rear++;
			s[rear] = p->rch;
		}
	}
}
void zhonggen(Bnode *p)     //非递归中根遍历 
{
	Bnode *q = p;
	Bnode *s[20];
	int top = 0;
	int flag = 1;
	do
	{
		while(q != NULL)
		{
			top++;
			s[top] = q;
			q = q->lch;
		}
		if(top == 0)flag = 0;
		else
		{
			q = s[top];
			top--;
			printf("%d\n",q->elem);
			q = q->rch;
		}
	}
	while(flag);
}
void postorder(Bnode *p)      //递归后根遍历 
{
	if(p!= NULL)
	{
		postorder(p->lch);
		postorder(p->rch);
		printf("%d\n",p->elem);
	}
}
void inorder(Bnode *p)        //递归中根遍历 
{
	if(p!= NULL)
	{
		inorder(p->lch);
		printf("%d\n",p->elem);
		inorder(p->rch);
	}
}
void preorder(Bnode *p)        //递归先根遍历 
{
	if(p!= NULL)
	{
		printf("%d\n",p->elem);
		preorder(p->lch);
		preorder(p->rch);
	}
}
int main(void)
{
	int tmp;
	int i = 0;
	int choice;
	Bnode *root;
	do
	{
		printf("\n");
		printf("1.creat\n");
		printf("2.先序遍历\n");
		printf("3.中序遍历\n");
		printf("4.后序遍历\n");
		printf("5.中根遍历\n");
		printf("6.按层遍历\n");
		printf("0.exit\n");
		printf("input your choice:");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:
				root = creat();
				break;
			case 2:
				preorder(root);
				break;
			case 3:
				inorder(root);
				break; 
			case 4:
				postorder(root);
				break;
			case 5:
				zhonggen(root);
				break;
			case 6:
				anceng(root);
				break; 
			case 0:
				break;
		}
		
	}while(choice);
	return 0;
}
此代码有创建和遍历的各种算法,遍历有按层遍历,各种递归遍历和非递归遍历。希望对楼主有帮助。
望采纳!!
⑧ 二叉树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 <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;
}

(9)完全二叉树创建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);
}
}
⑩ C语言二叉树的建立完整程序谢谢!!!
完整程序自己写吧,我提供个思路:
二叉树的节点包含三个指针:左指针,右指针,字符串链表的头指针;
构造一个通过两个字符串的头指针比较字符窜大小的函数;(先比较串长,再从头对位开始比较);
构造一个插入函数(递归)
最后查找函数(递归)返回查找元素距根的距离;
