java用三叉链表构建二叉树_三叉链表实现二叉树的基本操作

本文介绍如何使用三叉链表来构建和操作二叉树,包括构造、遍历、高度、宽度、结点计数、叶子计数等功能。通过这种方式,可以更高效地访问和管理二叉树中的节点。
摘要由CSDN通过智能技术生成

三叉链表存储表示

改进于二叉链表,增加指向父节点的指针,能更好地实现结点间的访问。

4288d7f13249042776f867fcba759fd0.png

存储结构/* 二叉树的三叉链表存储表示 */

typedef struct BiTPNode

{

TElemType data;

struct BiTPNode *parent,*lchild,*rchild; /* 双亲、左右孩子指针 */

}BiTPNode,*BiPTree;

下面给出二叉树采用三叉链表,实现了二叉树的构造、遍历、深度、宽度、结点个数、叶子个数 以及 结点的交换、层次、祖先、双亲、左孩子、右孩子、左兄弟、右兄弟各种功能:#include

using namespace std;

const int MaxBTreeSize = 20;

template

class BTNode

{

public:

T data;

BTNode* lchild;

BTNode* rchild;

BTNode* parent;

BTNode():lchild(NULL),rchild(NULL),parent(NULL)

{ }

};

template

class BTree

{

public: //提供接口

BTree();

BTree(const BTree& bTree);

~BTree();

const BTree& operator=(const BTree& bTree);

void createBTree();//按先序建立二叉树

void InitBiTree(); //初始化二叉树

void destoryBTree();//销毁二叉树

bool isEmptyBTree();//检查二叉树是否为空

void inOrderTraverse(); //中序遍历

void preOrderTraverse();//先序遍历

void postOrderTraverse(); //后序遍历

void levelOrderTraverse();//层序遍历

int heightBTree();//二叉树高度

int widthBTree(); //二叉树宽度

int nodeCountBTree(); //二叉树结点个数

int LeavesCountBTree();//二叉树叶子个数

int nodeLevelBTree(T item);//结点item在的层次

bool allParentBTree(T item);//找item的所有祖先

void findCommonAncestor(const BTNode* p1,const BTNode* p2,BTNode*& ancestor);//二叉树中2个节点p1, p2的最小公共祖先节点

void longPathBTree();//输出从每个叶子结点到根结点的最长路径

bool isFullBTree(); //判断二叉树是否是完全二叉树

void exchangeChildBTree();//交换二叉树的孩子

bool findBTree(const T item,BTNode*& ret)const; //查找结点

bool getParent(const BTNode* p,BTNode*& ret) const; //返回父亲

bool getLeftChild(const BTNode* p,BTNode*& ret) const; //返回左孩子

bool getRightChild(const BTNode* p,BTNode*& ret) const; //返回右孩子

bool getLeftSibling(const BTNode* p,BTNode*& ret) const; //返回左兄弟

bool getRightSibling(const BTNode* p,BTNode*& ret) const;//返回右兄弟

protected://为了继承

BTNode* root;

private://为了实现公有函数

void create(BTNode*& p);

//以p为根结点建树

void createParent(BTNode* p);

//为以p为根节点的树设置其指向双亲的结点

void copyTree(BTNode*& copyTreeRoot,BTNode* otherTreeRoot);

//把以otherTreeRoot为根节点的部分拷贝到copyTreeRoot为根节点的部分

void destory(BTNode*& p);

//销毁以p为根节点的部分

void inOrder(BTNode* p);

//中序遍历以p为根节点的部分

void preOrder(BTNode* p);

//先序遍历以p为根节点的部分

void postOrder(BTNode* p);

//后序遍历以p为根节点的部分

void levelOrder(BTNode* p);

//层次遍历以p为根节点的部分

int height(BTNode* p);

//计算以p为根节点的高度

int width(BTNode* p);

int max(int x,int y);

int min(int x,int y);

//计算以p为根,俩孩子的最值

int nodeCount(BTNode* p);

//计算以p为根节点的结点个数

int leavesCount(BTNode* p);

//计算以p为根节点的叶子个数

void nodeLevel(T item,BTNode* p,int level,int& nlevel);

//计算以p为根节点的中item所在层次,如有多个元素,则返回一个最小值(离根最近),如果没有出现,则返回0

bool find(BTNode*p,const T item,bool& isFind,BTNode*& cur)const;

//在p指向的二叉树中,返回 值为item的指针

bool allParent(T item,BTNode* p,BTNode* path[MaxBTreeSize],int& len,int& seat,bool& isFind);

//找item的所有祖先

void longPath(BTNode* p,int len,int& maxLen,BTNode*& longNode);

输出从每个叶子结点到根结点的最长路径

bool isFull(BTNode* p);

//判断以p为根的二叉树是不是完全二叉树

void exchangeChild(BTNode*& p);

//交换以p为根节点的二叉树

};

template

BTree::BTree()

{

root = NULL;

}

template

BTree::BTree(const BTree& bTree)

{

if (bTree.root==NULL)

{

root = NULL;

}

else

{

copyTree(this->root,bTree.root);

}

}

template

BTree::~BTree()

{

destory(root);

}

template

const BTree& BTree::operator=(const BTree& bTree)

{

if (this!=&bTree)//避免自己赋值

{

if (root!=NULL)//

{

destory(root);//自己有成员,先销毁

}

if (bTree.root==NULL)

{

root = NULL;

}

else

{

copyTree(this->root,bTree.root);

}

}

return *this;

}

template

void BTree::createBTree()

{

cout<

create(root);

}

/*借助先序遍历创建二叉树*/

template

void BTree::create(BTNode*& p)

{

T newData

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值