2021-10-15【数据结构/严蔚敏】【二叉树基本实现】

线性表

栈&队列

二叉树

持续更新中,尽请期待!!!

#include <bits/stdc++.h>
using namespace std;
#define TElemType char
#define Status int
//------------二叉树的二叉链表存储表示-------------
typedef struct BiTNode
{
    TElemType data;
    struct BiTNode *lchild, *rchild; //左右孩子指针
} BiTNode, *BiTree;

//------------基本操作的函数原型说明---------------
Status InitBiTree(BiTree &T);
Status DestroyBiTree(BiTree &T);
Status CreateBiTree(BiTree &T);
void ClearBiTree(BiTree &T);
Status BiTreeEmpty(BiTree T);
int BiTreeDepth(BiTree T);
Status Root(BiTree T);
Status Value(BiTree T, TElemType e);
Status Assign(BiTree &T, TElemType e, TElemType value);
Status Parent(BiTree T, TElemType e);
Status LeftChild(BiTree T, TElemType e);
Status RightChild(BiTree T, TElemType e);
Status LeftSibling(BiTree T, TElemType e);
Status RightSibling(BiTree T, TElemType e);
Status InsertChild(BiTree T, BiTree p, int LR, BiTree c);
Status DeleteChild(BiTree T, BiTree p, int LR);
//-----------------具体实现-----------------------
Status InitBiTree(BiTree &T){
    T = NULL;
    return 1;
}

Status DestroyBiTree(BiTree &T){
    if ((T)->lchild)             
        DestroyBiTree(T->lchild); 
    if ((T)->rchild)                 
        DestroyBiTree(T->rchild); 
    free(T);                        
    T = NULL;
    return 1;
}

Status CreateBiTree(BiTree &T){ 
    //算法6.4
    TElemType ch;
    scanf("%c", &ch);
    if (ch == ' ') 
        T = NULL;
    else
    {
        T = (BiTree)malloc(sizeof(BiTNode));
        if (!T)
            exit(_OVERFLOW);
        T->data = ch;             
        CreateBiTree(T->lchild); 
        CreateBiTree(T->rchild);
    }
    return 1;
}

void ClearBiTree(BiTree &T){
    if (T){
        if ((T)->lchild)             
            ClearBiTree(T->lchild); 
        if ((T)->rchild)                 
            ClearBiTree(T->rchild); 
        free(T);                        
        T = NULL;                
    }
}

Status BiTreeEmpty(BiTree T){
    if (T)
        return 0;
    else
        return 1;
}

int BiTreeDepth(BiTree T){
    int i, j;
    if (!T)
        return 0;
    if (T->lchild)
        i = BiTreeDepth(T->lchild);
    else
        i = 0;
    if (T->rchild)
        j = BiTreeDepth(T->rchild);
    else
        j = 0;
    return i > j ? i + 1 : j + 1;
}

Status Root(BiTree T){
    cout << T->data << endl;
    return 1;
}

Status Value(BiTree T,TElemType e){
    if(T->data==e)
        return 1;
    else{
        int i = 0, j = 0;
        if(T->lchild)
            i = Value(T->lchild, e);
        if(T->rchild)
            j = Value(T->rchild, e);
        if(i||j)
            return 1;
        else
            return 0;
    }
    return 0;
}

Status Assign(BiTree &T,TElemType e,TElemType value){
	if(T->data==e){
        T->data = value;
        return 1;
	}
	else{
		int i=0,j=0;
		if(T->lchild){
            i = Assign(T->lchild, e, value);
        }
		if(T->rchild){
			j=Assign(T->rchild,e,value);
		}
		if(i||j) return 1;
		else return 0;
	}
	return 0;
}

Status Parent(BiTree T,TElemType e){
	if(T->lchild&&T->lchild->data==e){
		printf("双亲节点为:%c\n",T->data);
	    return 1;
	}
	else if(T->rchild&&T->rchild->data==e){
		printf("双亲节点为:%c\n",T->data);
	    return 1;
	}
	else{
		if(T->lchild){
			Parent(T->lchild,e);
		}
		if(T->rchild){
			Parent(T->rchild,e);
		}
	}
	return 0;
}

Status LeftChild(BiTree T,TElemType e){
	if(T->data==e&&T->lchild){
		printf("左孩子节点为:%c\n",T->lchild->data);
		return 1;
	}
	else{
		if(T->lchild){
			LeftChild(T->lchild,e);
		}
		if(T->rchild){
			LeftChild(T->rchild,e);
		}
	}
	return 0;
}

Status RightChild(BiTree T,TElemType e){
	if(T->data==e&&T->rchild){
		printf("右孩子节点为:%c\n",T->rchild->data);
		return 1;
	}
	else{
		if(T->lchild){
			RightChild(T->lchild,e);
		}
		if(T->rchild){
			RightChild(T->rchild,e);
		}
	}
	return 0;
}

Status LeftSibling(BiTree T,TElemType e){
	if(T->lchild&&T->rchild->data==e){
		printf("左兄弟节点为:%c\n",T->lchild->data);
		return 1;
	}
	else{
		if(T->lchild){
			LeftSibling(T->lchild,e);
		}
		if(T->rchild){
			LeftSibling(T->rchild,e);
		}
	}
	return 0;
}

Status RightSibling(BiTree T,TElemType e){
	if(T->rchild&&T->lchild->data==e){
		printf("左兄弟节点为:%c\n",T->rchild->data);
		return 1;
	}
	else{
		if(T->lchild){
			RightSibling(T->lchild,e);
		}
		if(T->rchild){
			RightSibling(T->rchild,e);
		}
	}
	return 0;
}

Status InsertChild(BiTree T,BiTree p,int LR,BiTree c){
	if(LR==0){//c的右子树为空,使p的左子树成为c的右子树,并使增加了右子树的c成为p的左子树 
		c->rchild=p->lchild;
		p->lchild=c;
	}
	else{//c的右子树为空,使p的右子树成为c的右子树,并使增加了右子树的c成为p的右子树 
		c->rchild=p->rchild;
		p->rchild=c;
	}
	return 1; 
}

Status DeleteChild(BiTree T,BiTree p,int LR){
	if(LR==0){//删除p的左子树 
		DestroyBiTree(p->lchild); 
	}
	else{//删除p的右子树 
		DestroyBiTree(p->rchild);
	}
	return 1; 
}
  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
清华大学出版社出版的《数据结构(C语言版)》是清华大学计算机科学与技术系教材严蔚敏编写的经典教材。全书包含了数据结构基本概念和常用算法的详细介绍,并提供了相应的代码实现。 《数据结构(C语言版)》的代码实现主要示例采用了C语言。全书共有12章内容,包括线性表、栈和队列、串、树与二叉树、图、查找、排序等。每一章节都配备了大量的算法示例和相应的C语言代码实现。下面以线性表为例简要介绍一下书中代码实现: 在第2章“线性表”的内容中,书中详细介绍了线性表的定义、基本操作以及线性表的顺序存储结构和链式存储结构的实现方法。 在顺序存储结构部分,书中给出了线性表的初始化、插入、删除、查找等基本操作的代码实现。例如,线性表的初始化操作可以通过以下C语言代码实现: ```c #define MAXSIZE 100 // 定义线性表的最大长度 typedef struct { int data[MAXSIZE]; // 存储数据元素的数组 int length; // 线性表的当前长度 } SqList; void InitList(SqList *L) { L->length = 0; // 初始化线性表长度为0 } ``` 在链式存储结构部分,书中介绍了线性表的链式存储结构以及常见的链表类型,如单链表、静态链表和循环链表。对于单链表,书中给出了插入、删除、查找等操作的代码实现。例如,线性表的插入操作可以通过以下C语言代码实现: ```c typedef struct Node { int data; // 数据域 struct Node *next; // 指针域 } Node; // 在第i个位置插入元素 int InsertList(Node *head, int i, int x) { Node *pre = head; // pre指向头结点 int j = 0; while (pre != NULL && j < i - 1) { // 遍历找到第i-1个结点 pre = pre->next; j++; } if (pre == NULL || j > i - 1) { return 0; // 位置错误返回0 } Node *new_node = (Node *)malloc(sizeof(Node)); new_node->data = x; new_node->next = pre->next; pre->next = new_node; return 1; } ``` 以上只是《数据结构(C语言版)》中部分代码实现的简单例子,全书还包含很多其他章节的代码实现。读者可以通过阅读这本教材更全面地了解数据结构的概念和常用算法,并借助书中提供的代码实现进行实际操作和学习。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eternity_GQM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值