数据结构----二叉树链式存储结构的算法

1.在二叉树中搜索给定结点的父结点
算法 father (t,p,q)
指针t指向二叉树T之根;father使用递归在T中搜索给定结点p之父结点;q指向p之父结点。

伪代码:
father 1.(test whether t is null)
if t=null then( q<---  l.return )
Father2.(t is the answer)
if left(t)=p or right(t)=p then (q<---t.return.)
father 3.(递归)
father (left(t),p,ql);
if ql  !=null then (q<----ql.return.)
father (right(t),p,qR) .q<---qR;

2.搜索二叉树中符合数据域条件的结点
算法 Find (t ,item,q)
指针t指向二叉树之根,Find在中搜值为item的结点,指针q指向该结点。

Find1.[ t=null?]
if t= null hen (q<---null);
if data(t)=item then( q<----t.return).
Find2.[递归]
Find(Left(t) ,item ,p).
if  p!=null then (q<---p.return).
Find (Right(t) ,item ,q);

3.从二叉树中删除给定结点及其左右子树

DST (t)
root 指向二叉树T之根,DST从T中删除给定结点t及其左右子树。


DST1.[t=null?]
if t=null then return .
DST 2.[t=root ?]
if t=root then (del(t).root <---null.return )
DST 3.[找t的父结点q】
p<----t.father(root,p,q).
DST 4 [修改q的指针域)
if left (q) = p  then left (q) <----null  ;
if right (q) = p then right (q)< ----null.
DST5.[删除p及其子树】
del(p);

算法 Del (p)
删除结点 p 及左右子树
Del1. 【p= null ?】
if p=null then return .
del 2 . [递归删除】
Del ( left §).
Del (right §).
AVAIL <= P;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
二叉树是一种常见的数据结构,它由一个根节点和每个节点最多有两个子节点组成。这两个子节点分别称为左子节点和右子节点。二叉树存储结构一般有两种:链式存储和顺序存储。 链式存储是指每个节点都保存了指向它的左子节点和右子节点的指针。这种存储方式适合于动态结构,因为它可以动态地分配内存。链式存储二叉树可以用如下的 C 语言代码来实现: ``` typedef struct TreeNode { int value; struct TreeNode *left; struct TreeNode *right; } TreeNode; ``` 顺序存储是指将二叉树的节点按照某种顺序依次存储下来,从而形成一个数组。对于一个节点的索引为 i,它的左子节点的索引为 2*i,右子节点的索引为 2*i+1。这种存储方式适合于静态结构,因为它不需要动态地分配内存。顺序存储二叉树可以用如下的 C 语言代码来实现: ``` #define MAX_SIZE 100 int tree[MAX_SIZE]; void set_node(int index, int value) { tree[index] = value; } int get_node(int index) { return tree[index]; } int get_left_child(int index) { return tree[2*index]; } int get_right_child(int index) { return tree[2*index+1]; } ``` 二叉树在计算机科学中广泛应用,它是很多算法数据结构的基础。例如,二叉搜索树是一种特殊的二叉树,它的每个节点的左子树中的所有节点都小于它,右子树中的所有节点都大于它。二叉搜索树可以用于实现快速查找和排序。还有其他类型的二叉树,例如红黑树、AVL 树、堆等等,它们都有各自的特点和应用场景。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不停---

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

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

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

打赏作者

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

抵扣说明:

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

余额充值