二叉树ADT

本文介绍了二叉查找树,一种结合二分查找的链接结构。每个节点包含一个项,指针指向左右子节点,且左节点项小于父节点,右节点项大于父节点。文章详细讨论了二叉树的抽象数据类型(ADT)、接口定义及其实现,包括在tree.h中的接口函数和在tree.c中的具体实现,以及在主程序petclub.c中的应用。
摘要由CSDN通过智能技术生成

二叉查找树

       二叉查找树是一种结合了二分查找策略的链接结构。二叉树的每个节点都包含一个项和两个指向其他节点(称为子节点:左节点/右节点)的指针。其顺序按照按照如下规定:左节点的项在父节点的项的前面;右节点的项在父节点的项的后面

二叉树

二叉树ADT

树定义

二叉查找树接口(tree.h)

接口中的函数是使用二叉树ADT的程序员可以操作的:

#ifndef _TREE_H_
#define _TREE_H_
#include<stdbool.h>

#define SLEN 20
typedef struct item
{
    char petname[SLEN];
    char petkind[SLEN];
} Item;

#define MAXITEMS 10

typedef struct trnode
{
    Item item;
    struct trnode * left;  //指向左分支的指针 
    struct trnode * right; //指向右分支的指针 
} Trnode;

typedef struct tree
{
    Trnode * root;  //指向根结点的指针 
    int size;  //树的项数 
} Tree;

//函数原型

//初始化树

void InitializeTree(Tree * ptree);

//确定树是否为空

bool TreeIsEmpty(const Tree * ptree);

//确定树是否为满

bool TreeIsFull(const Tree * ptree);

//确定树的项数

int TreeItemCount(const Tree * ptree);

//在树中添加一个项

bool AddItem(const Item * pi, Tree * ptree);

//在树中查找一个项

bool InTree(const Item * pi, Tree * ptree);

//在树中删除一个项

bool DeleteItem(const Item * pi, Tree * ptree);

//把函数应用于树中每一项

void Traverse(const Tree * ptree, void (*pfun) (Item item));

//删除树中所有内容

void DeteleAll(Tree * ptree);

#endif

二叉树的实现(tree.c)

//树的支持函数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"tree.h"

//局部数据类型
typedef struct pair {
    Trnode * parent;
    Trnode * child;
} Pair;

//局部函数原型,这些函数是底层进行的操作
static Trnode * MakeNode(const Item *pi);
static bool ToLeft(const Item *i1, const Item *i2);
static bool ToRight(const Item *i1, const Item *i2);
static void AddNode(Trnode * new_node, Trnode * root);
static void InOrder(const Trnode * root, void (*pfun) (Item item));
static Pair SeekItem(const Item * pi, const Tree * ptree);
static void DeleteNode(Trnode **ptr);
static void DeleteAllNodes(Trnode * root);

//函数定义
void InitializeTree(Tree * ptree)
{
    ptree->root = NULL;
    ptree->size = 0;
}

bool TreeIsEmpty(const Tree * ptree)
{
    if (ptree->root == NULL)
        return true;
    else
        return false;
}

bool Tre
/* * 二叉树节点ADT接口 */ package dsa; public interface BinTreePosition extends Position { //判断是否有父亲(为使代码描述简洁) public boolean hasParent(); //返回当前节点的父节点 public BinTreePosition getParent(); //设置当前节点的父节点 public void setParent(BinTreePosition p); //判断是否为叶子 public boolean isLeaf(); //判断是否为左孩子(为使代码描述简洁) public boolean isLChild(); //判断是否有左孩子(为使代码描述简洁) public boolean hasLChild(); //返回当前节点的左孩子 public BinTreePosition getLChild(); //设置当前节点的左孩子(注意:this.lChild和c.parent都不一定为空) public void setLChild(BinTreePosition c); //判断是否为右孩子(为使代码描述简洁) public boolean isRChild(); //判断是否有右孩子(为使代码描述简洁) public boolean hasRChild(); //返回当前节点的右孩子 public BinTreePosition getRChild(); //设置当前节点的右孩子(注意:this.rChild和c.parent都不一定为空) public void setRChild(BinTreePosition c); //返回当前节点后代元素的数目 public int getSize(); //在孩子发生变化后,更新当前节点及其祖先的规模 public void updateSize(); //返回当前节点的高度 public int getHeight(); //在孩子发生变化后,更新当前节点及其祖先的高度 public void updateHeight(); //返回当前节点的深度 public int getDepth(); //在父亲发生变化后,更新当前节点及其后代的深度 public void updateDepth(); //按照中序遍历的次序,找到当前节点的直接前驱 public BinTreePosition getPrev(); //按照中序遍历的次序,找到当前节点的直接后继 public BinTreePosition getSucc(); //断绝当前节点与其父亲的父子关系 //返回当前节点 public BinTreePosition secede(); //将节点c作为当前节点的左孩子 public BinTreePosition attachL(BinTreePosition c); //将节点c作为当前节点的右孩子 public BinTreePosition attachR(BinTreePosition c); //前序遍历 public Iterator elementsPreorder(); //中序遍历 public Iterator elementsInorder(); //后序遍历 public Iterator elementsPostorder(); //层次遍历 public Iterator elementsLevelorder(); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值