数据结构之二叉树
基本术语:
度:结点的子结点的个数;
树高度(深度):树中结点的最大层数;
分支结点:度大于 0的结点;
叶子结点:度为 0的结点;
结点层次:根结点为第一层,往下递增;
结点深度:从根结点自顶向下逐层累加;
有序树:从左到右,子树有序;交换子结点位置树不同;
无序树:交换子结点后树是相同的;
两种特殊树:
满二叉树:高度为h的二叉树恰好有2^h-1个元素。
完全二叉树:从满二叉树中删除k个其编号为2^h -i个元素,1<=i<=k<2^h,所得到的的二叉树。
#include <iostream>
using namespace std;
/* 二叉树结点 */
template<class T>
struct binaryTreeNode
{
T element;
binaryTreeNode<T>* leftChild; //左子树
binaryTreeNode<T>* rightChild; //右子树
binaryTreeNode()
{
leftChild = rightChild = NULL;
}
binaryTreeNode(const T& theElement)
{
element = theElement;
leftChild = rightChild = NULL;
}
binaryTreeNode(const T& theElement, binaryTreeNode* theleftChild, binaryTreeNode* therightChild)
{
element = theElement;
leftChild = theleftChild;
rightChild = therightChild;
}
};
/* 二叉树链式描述 */
template<class E>
class linkedBinaryTree
{
private:
public:
binaryTreeNode<E>* root;
int treesize;
linkedBinaryTree()
{
root = NULL;
treesize = 0;
}
linkedBinaryTree(linkedBinaryTree<E>* x)
{
root = x->root;
treesize = x->treesize;
}
~linkedBinaryTree(){};
/* 判断二叉树是否为空 */
bool empty() const
{
return treesize == 0;
}
/* 获取二叉树元素个数 */
int size() const
{
return treesize;
}
/* 获取二叉树节点个数 */
int getNode(binaryTreeNode<E>* x)
{
if (x == NULL)
{
return 0;
}
return (getNode(x->leftChild) + getNode(x->rightChild) + 1);
}
/* 获取二叉树节点个数最多的层(宽度) */
/* 有bug,欢迎大家讨论 */
int getMaxNode(binaryTreeNode<E>* x)
{
if (x == NULL)
{
return 0;
}
int maxwidth = 0;
if (x->leftChild == NULL && x->rightChild == NULL)
{
return 1;
}
else
{
int width = (getMaxNode(x->leftChild) > getMaxNode(x->rightChild)) ? getMaxNode(x->leftChild) : getMaxNode(x->rightChild);
maxwidth = (width > maxwidth) ? width : maxwidth;
}
return maxwidth;
}
/* 交换二叉树的左右子树 */
binaryTreeNode<E>* swapTree(binaryTreeNode<E>* x)
{
if (x == NULL)
{
return x;
}
else
{
binaryTreeNode<E>* left = x->leftChild;
binaryTreeNode<E>* right = x->rightChild;
x->leftChild = right;
x->rightChild = left;
}
}
/* 前序遍历二叉树 */
void preOrder(binaryTreeNode<E>* x)
{
if (x != NULL)
{
cout << x->element << ",";
preOrder(x->leftChild);
preOrder(x->rightChild);
}
}
/* 中序遍历二叉树 */
void inOrder(binaryTreeNode<E>* x)
{
if (x != NULL)
{
inOrder(x->leftChild);
cout << x->element << ",";
inOrder(x->rightChild);
}
}
/* 后序遍历二叉树 */
void postOrder(binaryTreeNode<E>* x)
{
if (x != NULL)
{
postOrder(x->leftChild);
postOrder(x->rightChild);
cout << x->element << ",";
}
}
/* 删除二叉树 */
void erase()
{
root = NULL;
treesize = 0;
}
/* 二叉树插入元素 */
void insertBinaryTree(E theElement)
{
if (treesize == 0)
{
root = new binaryTreeNode<E>(theElement,NULL,NULL);
}
else
{
binaryTreeNode<E>* newNode = new binaryTreeNode<E>(theElement,NULL,NULL);
binaryTreeNode<E>* p = root;
while (p != NULL)
{
if (p->leftChild == NULL && p->rightChild == NULL)
{
p->leftChild = newNode;
break;
}
else if (p->leftChild != NULL && p->rightChild == NULL)
{
p->rightChild = newNode;
break;
}
else if (p->leftChild == NULL && p->rightChild != NULL)
{
p->leftChild = newNode;
break;
}
else if(p->leftChild != NULL && p->rightChild != NULL)
{
binaryTreeNode<E>* q = p;
p = p->leftChild;
if (p->leftChild == NULL && p->rightChild == NULL)
{
p->leftChild = newNode;
break;
}
else if (p->leftChild != NULL && p->rightChild == NULL)
{
p->rightChild = newNode;
break;
}
else if (p->leftChild == NULL && p->rightChild != NULL)
{
p->leftChild = newNode;
break;
}
else
{
p = q->rightChild;
}
}
}
}
treesize++;
}
/* 获取二叉树的高度 */
int high(binaryTreeNode<E>* x)
{
if (x == NULL)
{
return 0;
}
int lefthigh = high(x->leftChild);
int righthigh = high(x->rightChild);
return 1 + (lefthigh > righthigh ? lefthigh : righthigh);
}
/* 获取左右子树最大高度差 */
int getHighDifference(binaryTreeNode<E>* x)
{
int leftHigh = high(x->leftChild);
int rightHigh = high(x->rightChild);
return leftHigh > rightHigh ? (leftHigh - rightHigh) : (rightHigh - leftHigh);
}
};
//测试代码
int main()
{
linkedBinaryTree<int>a;
a.insertBinaryTree(1);
a.insertBinaryTree(2);
a.insertBinaryTree(3);
a.insertBinaryTree(4);
a.insertBinaryTree(5);
a.insertBinaryTree(6);
a.insertBinaryTree(7);
a.insertBinaryTree(8);
a.insertBinaryTree(9);
a.insertBinaryTree(10);
a.insertBinaryTree(11);
a.insertBinaryTree(12);
linkedBinaryTree<int>b(a);
binaryTreeNode<int>* c = b.root;
cout << "前序遍历:";
b.preOrder(c);
cout << endl;
cout << "中序遍历:";
b.inOrder(c);
cout << endl;
cout << "后序遍历:";
b.postOrder(c);
cout << endl;
cout << "高度:" << b.high(c)<< endl;
cout << "节点数:" << b.getNode(c) << endl;
cout << "最多节点层数:" << b.getMaxNode(c) << endl;
cout << "交换左右子树后:" << endl;
b.swapTree(c);
cout << "前序遍历:";
b.preOrder(c);
cout << endl;
cout << "中序遍历:";
b.inOrder(c);
cout << endl;
cout << "左右子树高度差值:" << b.getHighDifference(c) << endl;
return 0;
}