c语言二叉树以二叉链表存储,对于树中每个元素值为x的结点,删除以它为根的子树,并释放相应空间

1.删除时采用后序遍历即先删除左右结点再删除根结点

void dele_x(Node *p){
	if(p){
		printf("%d ",p->data);
		dele_x(p->lchild);
		dele_x(p->rchild);
		free(p);
	}
}

2.要删除结点通过层次遍历进行查找,依次判断p,p->lchild,p->rchild是否等于值x,若等于则删除,跳到删除函数,若不等于则入队列

void search(Node *p,int x){
	Queue Q;
	initQueue(&Q);
	if(p!=NULL){
		if(p->data==x){
			dele_x(p);
			return;
		}
		EnQueue(&Q,p);
		while(!emptyQueue(Q)){
			p=DeQueue(&Q);
			if(p->lchild!=NULL){
				if(p->lchild->data==x)
					dele_x(p->lchild);
				
				else
					EnQueue(&Q,p->lchild);
				
			}
			if(p->rchild!=NULL){
				if(p->rchild->data==x)
					dele_x(p->rchild);
				else
					EnQueue(&Q,p->rchild);
				
			}
		}
	}
}

3.所有代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 10
typedef struct Node{
	int data;
	struct Node *lchild,*rchild;
}Node; 
typedef struct{
    Node *data[MaxSize];
    int front,rear;
}Queue;
void initQueue(Queue *Q){
    Q->front=Q->rear=0;
}

bool emptyQueue(Queue Q){
    if(Q.front==Q.rear){
        return true; 
    }
    else{
        return false;
    }
}
bool EnQueue(Queue *Q,Node *p){
    if((Q->rear+1)%MaxSize==Q->front){
        return false;
    }
    Q->data[Q->rear]=p;
    Q->rear=(Q->rear+1)%MaxSize;
    return true;
}
int DeQueue(Queue *Q){
    if(Q->rear==Q->front){
        return false; 
    }
    Q->front=(Q->front+1)%MaxSize;
    return Q->data[Q->front-1];
}
void dele_x(Node *p){
	if(p){
		printf("%d ",p->data);
		dele_x(p->lchild);
		dele_x(p->rchild);
		free(p);
	}
}
void search(Node *p,int x){
	Queue Q;
	initQueue(&Q);
	if(p!=NULL){
		if(p->data==x){
			dele_x(p);
			return;
		}
		EnQueue(&Q,p);
		while(!emptyQueue(Q)){
			p=DeQueue(&Q);
			if(p->lchild!=NULL){
				if(p->lchild->data==x)
					dele_x(p->lchild);
				
				else
					EnQueue(&Q,p->lchild);
				
			}
			if(p->rchild!=NULL){
				if(p->rchild->data==x)
					dele_x(p->rchild);
				else
					EnQueue(&Q,p->rchild);
				
			}
		}
	}
}
int main() {
	Node *p=(Node *)malloc(sizeof(Node));
    p->data=1;
    p->lchild=NULL;
    p->rchild=NULL;
    Node *m=(Node *)malloc(sizeof(Node));
    m->data=2;
    m->lchild=NULL;
    m->rchild=NULL;
    p->lchild=m;
    Node *n=(Node *)malloc(sizeof(Node));
    n->data=3;
    n->lchild=NULL;
    n->rchild=NULL;
    p->rchild=n;
    Node *a=(Node *)malloc(sizeof(Node));
    a->data=4;
    a->lchild=NULL;
    a->rchild=NULL;
    m->lchild=a;
    Node *b=(Node *)malloc(sizeof(Node));
    b->data=5;
    b->lchild=NULL;
    b->rchild=NULL;
    m->rchild=b;
    Node *c=(Node *)malloc(sizeof(Node));
    c->data=6;
    c->lchild=NULL;
    c->rchild=NULL;
    n->lchild=c;
    search(p,2);
	return 0;
}

4.输出所删除的结点的值  2  4  5

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言实现二叉链表存储结构的代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 创建新结点 TreeNode* createNode(int val) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } // 层次遍历二叉树 void levelOrderTraversal(TreeNode* root) { if (root == NULL) return; TreeNode* queue[1000]; int front = 0, rear = 0; queue[rear++] = root; while (front < rear) { TreeNode* node = queue[front++]; printf("%d ", node->val); if (node->left != NULL) queue[rear++] = node->left; if (node->right != NULL) queue[rear++] = node->right; } } // 统计度为1的结点个数 int countDegreeOneNodes(TreeNode* root) { if (root == NULL) return 0; int count = 0; if ((root->left == NULL && root->right != NULL) || (root->left != NULL && root->right == NULL)) { count++; } return count + countDegreeOneNodes(root->left) + countDegreeOneNodes(root->right); } int main() { TreeNode* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); root->right->left = createNode(6); root->right->right = createNode(7); printf("层次遍历二叉树:"); levelOrderTraversal(root); printf("\n"); printf("度为1的结点个数:%d\n", countDegreeOneNodes(root)); return 0; } ``` 代码中的`TreeNode`结构体表示二叉树结点,包含一个`val`字段表示结点值,以及左右子结点的指针。`createNode`方法用于创建新结点。`levelOrderTraversal`方法实现了二叉树的层次遍历,使用了一个队列来辅助。`countDegreeOneNodes`方法用于统计二叉树中度为1的结点个数,递归实现。在`main`函数中,我们创建了一个二叉树并测试了以上方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值