二叉树ADT_BinaryTree

本文介绍了二叉树的基本概念,包括其定义、性质和不同类型的二叉树,如满二叉树和完全二叉树。同时,对比了二叉树与普通树的区别,强调了二叉树在子树次序上的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树是结点的有限集合, 该集合或者为空集, 或者是由一个根和两棵互不相交的称为该根的左子树和右子树的二叉树组成.

二叉树可以为空集, 可以有空二叉树, 也可以有空的左子树 或/和 又子树.

二叉树的性质: 1.第i层至多有2^(i - 1)个结点. 2.高度为h的二叉树上至多有2*h - 1个结点. 3.包含n个元素的二叉树高度至少为>=

log2(n + 1)取整. 3.任意一颗二叉树中, 若叶结点的个数为n0, 度为2的结点个数为n2, 则必有n0 = n2 + 1. 


树与二叉树区别: 1.树不能为空树, 二叉树可以为空. 2.树的子树之间是无序的, 其子树不分次序. 二叉树中结点的子树要分左右子树. 


满二叉树: 高度为h的二叉树恰好有2^h - 1个结点.

完全二叉树: 一棵二叉树中, 只有最下面两层结点的度可以小于2, 并且最下面一层的叶结点集中在靠左的若干位置上.

扩充二叉树(2 - 树): 除叶子结点外, 其余结点都必须有两个孩子.


树与二叉树区别: 1.树不能为空树, 二叉树可以为空. 2.树的子树之间是无序的, 其子树不分次序. 二叉树中结点的子树要分左右子树. 

实现代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
template <class T>
struct BTNode
{
	/* data */
	BTNode() { lChild = rChild = NULL; }
	BTNode(const T& x) {
		element = x;
		lChild = rChild = NULL;
	}
	BTNode(const T& x, BTNode<T>* l, BTNode<T>* r) {
		element = x;
		lChild = l;
		rChild = r;
	}
	T element;
	BTNode<T>* lChild, *rChild;
};
template <class T>
class BinaryTree
{
public:
	BinaryTree() { root = NULL; }
	bool IsEmpty() const; // 判断是否为空, 是返回true
	void Clear(); // 移去所有结点, 成为空二叉树
	bool Root(T& x) const; // 若二叉树为空, 则x为根的值, 返回true
    BTNode<T>* Root();
    int Size();
    int Count() { return Count(root); }
	void MakeTree(const T& x, BinaryTree<T>& left, BinaryTree<T>& right); // 构造一颗二叉树, 根的值为x, left & right为左右子树
	void BreakTree(T& x, BinaryTree<T>& left, BinaryTree<T>& right); // 拆分二叉树为三部分, x为根的值, left & right为左右子树
	void PreOrder(void (*Visit)(T& x)); // 先序遍历二叉树
	void InOrder(void (*Visit)(T& x)); // 中序遍历二叉树
	void PostOrder(void (*Visit)(T& x)); // 后序遍历二叉树
	/* data */
protected:
	BTNode<T>* root;
private:
	void Clear(BTNode<T>* t);
    int Size(BTNode<T> *t); // 返回二叉树结点个数
	int Count(BTNode<T> *t); // 返回二叉树只有一个孩子的结点个数
	void PreOrder(void (*Visit)(T &x), BTNode<T> *t);
	void InOrder(void (*Visit)(T &x), BTNode<T> *t);
	void PostOrder(void (*Visit)(T &x), BTNode<T> *t);
};
template <class T>
void Visit(T &x)
{
	cout << x << '\t';
}

template <class T>
BTNode<T>* BinaryTree<T>::Root()
{
    return root;
}
template <class T>
bool BinaryTree<T>::Root(T &x) const
{
	if(root) {
		x = root -> element;
		return true;
	}
	return false;
}

template <class T>
void BinaryTree<T>::Clear(BTNode<T>* t)
{
	if(t) {
		Clear(t -> lChild);
		Clear(t -> rChild);
		cout << "delete" << t -> element << "..." << endl;
		delete t;
	}
}

template <class T>
void BinaryTree<T>::MakeTree(const T& x, BinaryTree<T>& left, BinaryTree<T>& right)
{
	if(root || &left == &right) return;
	root = new BTNode<T>(x, left.root, right.root);
	left.root = right.root = NULL;
}

template <class T>
void BinaryTree<T>::BreakTree(T& x, BinaryTree<T>& left, BinaryTree<T>& right)
{
	if(!root || &left == &right || left.root || right.root) return;
	x = root -> element;
	left.root = root -> lChild;
	right.root = root -> rChild;
	delete root;
	root = NULL;
}

template <class T>
void BinaryTree<T>::PreOrder(void (*Visit)(T& x))
{
	PreOrder(Visit, root);
}
template <class T>
void BinaryTree<T>::PreOrder(void (*Visit)(T& x), BTNode<T>* t)
{
	if(t) {
		Visit(t -> element);
		PreOrder(Visit, t -> lChild);
		PreOrder(Visit, t -> rChild);
	}
}

template <class T>
void BinaryTree<T>::InOrder(void (*Visit)(T& x))
{
	InOrder(Visit, root);
}
template <class T>
void BinaryTree<T>::InOrder(void (*Visit)(T& x), BTNode<T>* t)
{
	if(t) {
		InOrder(Visit, t -> lChild);
		Visit(t -> element);
		InOrder(Visit, t -> rChild);
	}
}

template <class T>
void BinaryTree<T>::PostOrder(void (*Visit)(T& x))
{
	PostOrder(Visit, root);
}
template <class T>
void BinaryTree<T>::PostOrder(void (*Visit)(T& x), BTNode<T>* t)
{
	if(t) {
		PostOrder(Visit, t -> lChild);
		PostOrder(Visit, t -> rChild);
		Visit(t -> element);
	}
}

template <class T>
int BinaryTree<T>::Size()
{
	return Size(root);
}
template <class T>
int BinaryTree<T>::Size(BTNode<T> *t)
{
	if(!t) return 0;
	return Size(t -> lChild) + Size(t -> rChild) + 1;
}

template <class T>
int BinaryTree<T>::Count(BTNode<T> *t)
{
	if(!t) return 0;
	if(((t -> lChild) && (!t -> rChild)) || ((!t -> lChild) && (t -> rChild))) return 1;
	return Count(t -> lChild) + Count(t -> rChild);
}

int main(int argc, char const *argv[])
{
	BinaryTree<char> a, b, x, y, z; // 构造过程见课本75页.
	char e;
	y.MakeTree('E', a, b);
	z.MakeTree('F', a, b);
	x.MakeTree('C', y, z);
	y.MakeTree('D', a, b); // 用y前y已经被置空
	z.MakeTree('B', y, x);
	cout << endl << "PreOrder\t";
	z.PreOrder(Visit);
	cout << endl << "InOrder\t\t";
	z.InOrder(Visit);
	cout << endl << "PostOrder\t";
	z.PostOrder(Visit);
	cout << endl;
	cout << "Tree's size = " << z.Size() << endl;
    cout << "Tree's count = " << z.Count() << endl;
	z.BreakTree(e, y, x);
	cout << endl << "PreOrder\t";
	z.PreOrder(Visit);
	cout << endl << "InOrder\t\t";
	z.InOrder(Visit);
	cout << endl << "PostOrder\t";
	z.PostOrder(Visit);
	cout << endl;
	cout << "Tree's size = " << z.Size() << endl;
    cout << "Tree's count = " << z.Count() << endl;
	return 0;
}


/* * 二叉树节点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(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值