今天继续昨天的复习内容,预计完成:
二叉树的层次遍历
二叉树叶节点数量计算
二叉树深度(高度)计算
二叉查找树的建立,插入,遍历
一.二叉树的层次遍历
正如其名字,层次遍历是优先遍历同一层的节点,再向下遍历。可以使用简单的广度优先算法(BFS)来实现,广度优先算法利用的是队列先进先出的特点,之后会专门找一天来复习。建立一个队列,先将根节点入队,输出队首元素,若有左右子节点,则继续将其入队访问,每次循环将队首元素出队,直到队列为空。下面是示例代码:
template<typename T>
void My_BiTree<T>::LevelTraversing()
{
queue<BiNode<T>*> NodeQueue;
NodeQueue.push(parent);
while(!NodeQueue.empty())
{
std::cout<<NodeQueue.front()->No<<" ";
if(NodeQueue.front()->lchild!=NULL)
{
NodeQueue.push(NodeQueue.front()->lchild);
}
if(NodeQueue.front()->rchild!=NULL)
{
NodeQueue.push(NodeQueue.front()->rchild);
}
NodeQueue.pop();
}
std::cout<<std::endl;
}
输出结果:四种遍历结果,最后一行是层次遍历
很简单,编译一遍过,思路要清晰。
二.二叉树的叶节点统计,高度计算
这两个都是递归分治的思想,故放到一起写。函数体处理三种情况:该结点为空(空树),节点的左右子结点都为NULL(到达了叶子),节点的左右结点不为空。
最后在返回值时就体现出递归的思想了,一开始有点晕,还是要多加分析,总结,消化。
template<typename T>
int My_BiTree<T>::CountLeaf(BiNode<T> *node)
{
if(node==NULL)return 0;
if(node->lchild==NULL&&node->rchild==NULL)return 1;
return CountLeaf(node->lchild)+CountLeaf(node->rchild);
}
template<typename T>
int My_BiTree<T>::CountLeaf()
{
CountLeaf(parent);
}
template<typename T>
int My_BiTree<T>::GetDepth(BiNode<T> *node)
{
if(node==NULL)return 0;
if(node->lchild==NULL&&node->rchild==NULL)return 1;
depthL=GetDepth(node->lchild);
depthR=GetDepth(node->rchild);
return depthL > depthR ? depthL+1 : depthR+1;
//可以写个max函数但没必要,直接用三目运算符
}
运行结果:
三.二叉查找树的构建
二叉查找树也称为二叉排序树,是一种特殊的二叉树,它具有如下性质:
1.左子树上的所有节点的值均小于它根节点的值
2.右子树上的所有节点的值均大于它根节点的值
3.二叉树中不存在值相等的节点。
4.中序遍历一定会得到一个递增的序列
相关查找特点(摘自百度百科)
与次优二叉树相对,二叉排序树是一种动态树表。其特点是:树的结构通常不是一次生成的,而是在查找过程中,当树中不存在关键字等于给定值的结点时再进行插入。新插入的结点一定是一个新添加的叶子结点,并且是查找不成功时查找路径上访问的最后一个结点的左孩子或右孩子结点。
数据结构及算法分析:
节点仍然用普通二叉树的:
template<typename T>
class BiNode{
public:
BiNode<T> *lchild;
BiNode<T> *rchild;
T data;
char No;
};
然后定义BST:
template<typename T>
class BST{
public:
BST();
~BST(){};
void CreateBST(T InData);
void InsertElem(T InData) {InsertElem(parent,InData);}
void InTraverse(){InTraverse(parent);}
bool SearchElem(T ShData);
private:
BiNode<T> *parent;
void InsertElem(BiNode<T> *node,T InData);
void InTraverse(BiNode<T> *node);
};
这里就只写中序遍历了,看看是否为递增的序列
相关函数的实现:
template<typename T>
BST<T>::BST()
{
parent=NULL;
}
template<typename T>
void BST<T>::CreateBST(T InData)
{
if(parent==NULL)
{
parent=new BiNode<T>;
parent->data=InData;
parent->lchild=NULL;
parent->rchild=NULL;
}
}
//递归插入元素
template<typename T>
void BST<T>::InsertElem(BiNode<T> *node,T InData)
{
if(parent==NULL)
{
std::cout<<"please create a root"<<std::endl;
return;
}
if(InData==node->data)
{
std::cout<<"the tree includes the same data"<<std::endl;
return;
}
else
{
if(InData<node->data)
{
if(node->lchild==NULL)
{
node->lchild=new BiNode<T>;
node->lchild->data=InData;
node->lchild->lchild=NULL;
node->lchild->rchild=NULL;
std::cout<<"insert complete"<<std::endl;
}
else
{
InsertElem(node->lchild,InData);
}
}
else
{
if(node->rchild==NULL)
{
node->rchild=new BiNode<T>;
node->rchild->data=InData;
node->rchild->lchild=NULL;
node->rchild->rchild=NULL;
std::cout<<"insert complete"<<std::endl;
}
else
{
InsertElem(node->rchild,InData);
}
}
}
}
//中序遍历
template<typename T>
void BST<T>::InTraverse(BiNode<T> *node)
{
if(node!=NULL)
{
InTraverse(node->lchild);
std::cout<<node->data<<" ";
InTraverse(node->rchild);
}
}
测试:
int main()
{
/*My_BiTree<int> b1;
b1.PreTraversing();
b1.InTraversing();
b1.PostTraversing();
b1.LevelTraversing();
std::cout<<"leaf:"<<b1.CountLeaf()<<std::endl;
std::cout<<"depth:"<<b1.GetDepth()<<std::endl;
std::cout<<"---------------------"<<std::endl;*/
BST<int> bst;
bst.CreateBST(7);
bst.InsertElem(5);
bst.InsertElem(3);
bst.InsertElem(16);
bst.InsertElem(13);
bst.InTraverse();
return 0;
}
运行结果:
没有问题,明天来写查找和删除。
后面预计用两天来复习哈夫曼树和平衡树