算法之旅 二叉树的小结

本文总结了二叉树的基本操作,包括插入关键字、三种遍历方法(先序、中序、后序)及判断二叉树是否平衡。提供了C++实现代码示例。

二叉树的小结

  • 真言

最近正在总结学习过的算法,看到二叉树这一块,就总结总结一下吧。

  • 内容
    • 插入
      • 在二叉树中插入一个关键字,
      • 查找出要插入的位置
      • 新建节点,并插入

      • // function:insert key into tree
        template<typename T>
        void tree<T>::_insert(btnode<T> * *bt,T d)
        {
        	if((*bt) == NULL)
        	{
        		(*bt) = new btnode<T>;
        		(*bt) -> key = d;
        	}		
        	else
        	{
        		btnode<T> * p = *bt ;
        		if(d < p->key)
        		{
        			this->_insert(&(p->Lchild),d);
        		}
        		else if(d>p->key)
        		{
        			this->_insert(&(p->Rchild),d);
        		}
        		else
        		{
        			cout<<d<<" has been inserted"<<endl;
        		}
        	}
        };


    • 三种遍历方法 先序遍历,中序遍历,后序遍历
    • 先序遍历
    • // fucntion:preorder visit tree
      template<typename T>
      void tree<T>::_preOrderTraver(btnode<T> * p)
      {
      	if(p != NULL )
      	{
      		this->_visit(p);
      		this->_preOrderTraver(p->Lchild);
      		this->_preOrderTraver(p->Rchild);	
      	}
      };

      中序遍历
    • // fucntion:inorder visit tree
      template<typename T>
      void tree<T>::_inOrderTraver(btnode<T> * p)
      {
      	if(p != NULL )
      	{	
      		this->_inOrderTraver(p->Lchild);
      		this->_visit(p);
      		this->_inOrderTraver(p->Rchild);	
      	}
      };

      后序遍历
    • // fucntion:postorder visit tree
      template<typename T>
      void tree<T>::_postOrderTraver(btnode<T> * p)
      {
      	if(p != NULL )
      	{	
      		this->_postOrderTraver(p->Lchild);
      		this->_postOrderTraver(p->Rchild);
      		this->_visit(p);			
      	}
      };


    • 判断是否是平衡树
      • // function:whether is balanced binary tree
        template<typename T>
        std::pair<size_t,bool> binarytree<T>::_IsBalancedBinaryTree(btnode<T> *p)
        {
        	if( p == NULL )
        		return std::make_pair(0,true);
        	else 
        	{
        		std::pair<size_t,bool> r = this -> _IsBalancedBinaryTree(p->Rchild);
        		std::pair<size_t,bool> l = this -> _IsBalancedBinaryTree(p->Lchild);
        		int h = (int)(r.first) - (int)(l.first);
        		h = abs(h);
        		if(h>1)
        			return std::make_pair(max(r.first,l.first)+1,false);
        		else return std::make_pair(max(r.first,l.first)+1,r.second && l.second);
        	}
        
        };


  • 实验


  • 代码

b-tree.h
#include <iostream>
#include <utility>
#include <string>
#include <cmath>
using namespace std;



template<typename T>
class btnode
{
public:
	T key;
	btnode * Lchild;
	btnode * Rchild; 

	// function:make function
	btnode( );
};


template<typename T>
btnode<T>::btnode()
{
	key = -1;
	Lchild = NULL;
	Rchild = NULL;
};


template<typename T>
class tree
{
public:

	// var:R id the root of the tree
	btnode<T> * R;
	
	// function:make function
	tree();

	// function:make tree 
	void _maketree(T * data,size_t s);

	// function:insert key into tree
	void tree<T>::_insert(btnode<T> * *bt,T d);

	// function:visit node p
	void _visit(btnode<T> * p);

	// fucntion:preorder visit tree
	void _preOrderTraver(btnode<T> * p);


	// fucntion:inorder visit tree
	void _inOrderTraver(btnode<T> * p);

	// fucntion:postorder visit tree
	void tree<T>::_postOrderTraver(btnode<T> * p);

	// function:whether is balanced binary tree
	std::pair<size_t,bool> _IsBalancedBinaryTree(btnode<T> *p);

};


// function:make function
template<typename T>
tree<T>::tree()
{
	this->R = NULL;
};


// function:make tree
// input:an array of data and its length
// output: a binary tree
template<typename T>
void tree<T>::_maketree(T * data,size_t s)
{
	for(size_t i= 0;i<s;i++)
	{
		this->_insert(&(this->R),data[i]);
	}
};

// function:insert key into tree
template<typename T>
void tree<T>::_insert(btnode<T> * *bt,T d)
{
	if((*bt) == NULL)
	{
		(*bt) = new btnode<T>;
		(*bt) -> key = d;
	}		
	else
	{
		btnode<T> * p = *bt ;
		if(d < p->key)
		{
			this->_insert(&(p->Lchild),d);
		}
		else if(d>p->key)
		{
			this->_insert(&(p->Rchild),d);
		}
		else
		{
			cout<<d<<" has been inserted"<<endl;
		}
	}
};


// function:visit node p
template<typename T>
void tree<T>::_visit(btnode<T> * p)
{
	if(p != NULL)
	{
		cout<<"key="<<p->key<<endl;
	}
	else
	{
		cout<<"node point null"<<endl;
	}
};

// fucntion:preorder visit tree
template<typename T>
void tree<T>::_preOrderTraver(btnode<T> * p)
{
	if(p != NULL )
	{
		this->_visit(p);
		this->_preOrderTraver(p->Lchild);
		this->_preOrderTraver(p->Rchild);	
	}
};

// fucntion:inorder visit tree
template<typename T>
void tree<T>::_inOrderTraver(btnode<T> * p)
{
	if(p != NULL )
	{	
		this->_inOrderTraver(p->Lchild);
		this->_visit(p);
		this->_inOrderTraver(p->Rchild);	
	}
};

// fucntion:postorder visit tree
template<typename T>
void tree<T>::_postOrderTraver(btnode<T> * p)
{
	if(p != NULL )
	{	
		this->_postOrderTraver(p->Lchild);
		this->_postOrderTraver(p->Rchild);
		this->_visit(p);			
	}
};

// function:whether is balanced binary tree
template<typename T>
std::pair<size_t,bool> tree<T>::_IsBalancedBinaryTree(btnode<T> *p)
{
	if( p == NULL )
		return std::make_pair(0,true);
	else 
	{
		std::pair<size_t,bool> r = this -> _IsBalancedBinaryTree(p->Rchild);
		std::pair<size_t,bool> l = this -> _IsBalancedBinaryTree(p->Lchild);
		int h = (int)(r.first) - (int)(l.first);
		h = abs(h);
		if(h>1)
			return std::make_pair(max(r.first,l.first)+1,false);
		else return std::make_pair(max(r.first,l.first)+1,r.second && l.second);
	}

};

test.cpp
#include <iostream>
#include "b-tree.h"
using namespace std;

// function:input data
std::pair<int*,size_t> Inputdata();

int main()
{
	// tree has the root : tree::R
	tree<int> * T = new tree<int>;
	//int size = 10;
	//int * data = new int[size];
	//for(int i=0;i<size;i++)
	//{
	//	data[i] = rand()%20;
	//}
	int data[9] = {4,7,3,2,8,6,5,9,10};
	std::pair<int*,size_t> d = Inputdata();
	T->_maketree(d.first,d.second);
	cout<<"preorder traversal"<<endl;
	T->_preOrderTraver(T->R);
	cout<<"inorder traversal"<<endl;
	T->_inOrderTraver(T->R);
	cout<<"postorder traversal"<<endl;
	T->_postOrderTraver(T->R);
	std::pair<size_t,bool> r=T->_IsBalancedBinaryTree(T->R);
	cout<<"whether it is balanced binary tree,result is "<<r.second<<endl;
	
	
	system("pause");
	return 0;
}

// function:input data
std::pair<int*,size_t> Inputdata()
{
	size_t s = 0;
	cout<<"please input the number(>=0) of data,size = "<<endl;
	cin>>s;
	std::pair<int*,size_t> r;
	int * p = NULL;
	if(s == 0)
	{
		r = std::make_pair(p,0);
		return r;
	}
	else
	{
		p = new int[s];
		cout<<"please input data"<<endl;
		for(size_t i = 0; i<s; i++)
		{
			cin>>p[i];
		}
		cout<<"ok,data input over"<<endl;
		r = std::make_pair(p,s);
		return r;
	}
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值