二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

时间:2023-03-09 08:41:15
二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作:

1、定义树的结构体:

 typedef struct TreeNode{
int data;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;

2、创建根节点:

 TreeNode *creatRoot(){
TreeNode * root =(TreeNode *)malloc(sizeof(TreeNode));
if(NULL==root){
printf("CreatRoot is failing!\n");
return NULL;
}
root->left=NULL;
root->right=NULL;
printf("Input the data of root!\n");
scanf("%d",&root->data);
printf("Root created!\n");
return root;
}

3、申请了空间一定要记得释放空间,否则后果不堪设想

 void clearTree(TreeNode * root){
if(root){
clearTree(root->left);
clearTree(root->right);
free(root);
}
}

4、选择插入的节点是根节点的左孩子还是右孩子

 void addSubTreeNode(TreeNode* root,TreeNode*node,int select){
if(NULL==root){
printf("Root is NULL\n");
return ;
}
switch(select){
case :
if(root->left){
printf("The root has left subtree!\n");
return ;
}else{
root->left=node;
break;
}
case :
if(root->right){
printf("The root has right subtree!\n");
return ;
}else{
root->right=node;
break;
}
default:
printf("The input of select is wrong!\n");
}
}

5、二叉树的前序、中序、后序遍历

 void DLR(TreeNode * root){
if(NULL==root)
return;
printf("The First root input %d\n",root->data);
DLR(root->left);
DLR(root->right);
} void LDR(TreeNode * root){
if(NULL==root)
return;
LDR(root->left);
printf("The Middle root input %d\n",root->data);
LDR(root->right);
} void LRD(TreeNode * root){
if(NULL==root)
return;
LRD(root->left);
LRD(root->right);
printf("The Last root input %d\n",root->data);
}

6、求二叉树的深度

 int lengthTree(TreeNode *root){
int lenLeft,lenRight;
if(NULL==root)
return ;
else{
lenLeft=lengthTree(root->left);
lenRight=lengthTree(root->right);
if(lenLeft<lenRight)
return lenRight+;
else
return lenLeft+;
}
}

7、查找节点,这里是为了初始化构建二叉树时选择的函数,目的是找出你要插入节点的父亲节点

 TreeNode *treeFind(TreeNode *node,int number){
TreeNode *p;
if(NULL==node)
return NULL;
else{
if(node->data==number){
return node;
}else{
if(p=treeFind(node->left,number)){
printf("Parent found!\n");
return p;
} else if(p=treeFind(node->right,number)){
printf("Parent found!\n");
return p;
}
else
return NULL;
}
}
}

8、找到父亲节点后,选择是插入做孩子还是右孩子

 void addNode(TreeNode *root){
TreeNode *node,*parent;
int number,select;
printf("Please input the parent data!\n");
scanf("%d",&number);
parent=treeFind(root,number);
if(NULL==parent){
printf("parent is not found!\n");
return ;
}
printf("Please choice the operation: 0 Exit; 1 insert left subtree; 2 insert right subtree!\n");
scanf("%d",&select);
if(select==)
return ;
if(node=(TreeNode*)malloc(sizeof(TreeNode))){
printf("Please input the data of BinTreeData that you want to insert!\n");
scanf("%d",&node->data);
node->left=NULL;
node->right=NULL;
switch(select){
case :
addSubTreeNode(parent,node,);
break;
case :
addSubTreeNode(parent,node,);
break;
}
}
}

9、二叉树的层次遍历

 void levelFind(TreeNode * root){
TreeNode *node;
TreeNode *Queue[MAX];
int head,tail;
head=tail=;
if(root==NULL){
printf("Queue is empty!\n");
return ;
}
if((tail+)%MAX!=head)
Queue[tail++]=root;
else
printf("Queue is full!\n");
if(head!=tail)
node=Queue[head++];
else
printf("Queue is empty!\n");
printf("The bintree level find is: \n");
while(NULL!=node){
printf("%d ",node->data);
if(node->left!=NULL){
Queue[tail++]=node->left;
}
if(node->right!=NULL){
Queue[tail++]=node->right;
}
if(head==tail){
printf("Queue is empty!\n");
return;
}
node=Queue[head++];
}
}

10、主函数

 int main(){
TreeNode *root=NULL;
int n;
do{
printf("1 set root, 2 addNode, 3 the First root input, 4 the Middle root input, 5 the last root input, 6 according to level find, 7 caculate the depth, 0 exit!\n");
scanf("%d",&n);
switch(n){
case :
break;
case :
if(NULL==root)
root=creatRoot();
else
printf("The root is exist!\n");
break;
case :
addNode(root);
break;
case :
DLR(root);
break;
case :
LDR(root);
break;
case :
LRD(root);
break;
case :
levelFind(root);
break;
case :
printf("the depth of tree is %d\n",lengthTree(root));
break;
}
}while(n!=);
clearTree(root);
printf("Tree are cleared all!\n");
getch();
return ; }