二叉查找树

二叉树是一种树形结构,它的特点是每个分支点嘴都只有2棵子树,在离散中称为2元树,当每个分支点(叶不是分支点)都有2个儿子时称为k元完全树。二叉树通常用做二叉查找树和二叉堆。


头文件:

/**********************
The BinaryTree head file 
Created by SKY_TM on 2015/1/27
**********************/

#pragma once

#include <iostream>
using namespace std;

class TreeNode
{
	friend class BinaryTree;
public :
	//重载输出操作符
	friend ostream &operator << (ostream &os, const TreeNode &node)
	{
		os << node.m_keyValue;

		return os;
	}
	TreeNode(int keyValue = 0, TreeNode *leftChild = nullptr, TreeNode *rightChild = nullptr)
		: m_keyValue(keyValue), m_leftChild(leftChild), m_rightChild(rightChild)
	{
		
	}

	TreeNode(const TreeNode &t)
		: m_keyValue(t.m_keyValue), m_leftChild(t.m_leftChild), m_rightChild(t.m_rightChild)
	{

	}
	virtual ~TreeNode()
	{
		
	}
public :
	TreeNode *m_leftChild;
	TreeNode *m_rightChild;
private :
	int m_keyValue;
};



//BinaryTree
class BinaryTree
{
public :
	BinaryTree(TreeNode *rootNode = nullptr)
		:m_rootNode(rootNode)
	{
		
	}
	BinaryTree(const int keyValue)
	{
		m_rootNode = new TreeNode(keyValue);
	}
	BinaryTree(const BinaryTree &BnrTree);
	virtual ~BinaryTree()
	{
		
	}
public :
	//三种遍历的顺序就是根被访问的位置在前面,在后面,还是在中间的问题
	void PreOrderTraverse(const TreeNode *const &node);//先序遍历
	void InOrderTraverse(const TreeNode *const &node);//中序遍历
	void PostOrderTraverse(const TreeNode *const &node);//后序遍历
	void Insert(const int &keyValue);//插入
	void Insert(TreeNode *&child);
	TreeNode *Copy(const TreeNode *const &oldNode);//深拷贝,拷贝这个动作无需改变oldNode的内容所以声明为const
	TreeNode *Search(const int keyValue);//查找
	TreeNode *GetRootNode();//获取整棵树的根节点
	void Visit(const TreeNode *const T);//打印节点中的内容
	void Destroy(TreeNode *node);//删除节点
private :
	void Insert(TreeNode *&root, const int &keyValue);
	void Insert(TreeNode *&root, TreeNode *&child);
	TreeNode *Search(const int keyValue, TreeNode *&root);
public :
	static TreeNode *Create(const int keyValue);//静态类构造函数,返回根节点.
private :
	TreeNode *m_rootNode;
};



下面是BinaryTree.h的实现文件:

/********************************
BinaryTree's completed file
Created by SKY-TM on 2015/1/27
********************************/

#include "BinaryTree.h"

//return the root node
TreeNode *BinaryTree::Create(const int keyValue)
{
	return new TreeNode(keyValue);
}

BinaryTree::BinaryTree(const BinaryTree &BnrTree)
{
	if(BnrTree.m_rootNode != nullptr)
	{
		m_rootNode = Copy(BnrTree.m_rootNode);
	}
	else 
	{
		m_rootNode = nullptr;
	}
}

void BinaryTree::Insert(const int &keyValue)
{
	if(m_rootNode != nullptr)
	{
		Insert(m_rootNode, keyValue);
	}
	else
	{
		m_rootNode = new TreeNode(keyValue);
	}
}

void BinaryTree::Insert(TreeNode *&child)
{
	if(m_rootNode != nullptr)
	{
		Insert(m_rootNode, child);
	}
	else
	{
		m_rootNode = child;
	}
}

void BinaryTree::Insert(TreeNode *&root, const int &keyValue)
{
	if(root != nullptr)
	{
		if(keyValue < root->m_keyValue)
		{
			Insert(root->m_leftChild, keyValue);
		}
		else
		{
			Insert(root->m_rightChild, keyValue);
		}
	}
	else
	{
		root = new TreeNode(keyValue);
	}
}

void BinaryTree::Insert(TreeNode *&root, TreeNode *&child)
{
	if(root != nullptr)
	{
		if(child->m_keyValue < root->m_keyValue)
		{
			Insert(root->m_leftChild, child);
		}
		else
		{
			Insert(root->m_rightChild, child);
		}
	}
	else
	{
		root = child;
	}
}

TreeNode *BinaryTree::Copy(const TreeNode *const &oldNode)
{
	if(oldNode != nullptr)
	{
		TreeNode *newNode = new TreeNode(oldNode->m_keyValue);
		newNode->m_leftChild = Copy(oldNode->m_leftChild);
		newNode->m_rightChild = Copy(oldNode->m_rightChild);
		return newNode;
	}

	return nullptr;
}


TreeNode * BinaryTree::Search(const int keyValue)
{
	return Search(keyValue, m_rootNode);
}

TreeNode *BinaryTree::Search(const int keyValue, TreeNode *&root)
{
	if(keyValue == root->m_keyValue)
	{
		return root;
	}
	else if(keyValue < root->m_keyValue)
	{
		return Search(keyValue, root->m_leftChild);
	}
	else if(keyValue > root->m_keyValue)
	{
		return Search(keyValue, root->m_rightChild);
	}

	return nullptr;
}

void BinaryTree::Visit(const TreeNode *const T)
{
	cout << *T << " ";
}

void BinaryTree::PreOrderTraverse(const TreeNode *const &node)
{
	if(node != nullptr)
	{
		Visit(node);
		PreOrderTraverse(node->m_leftChild);
		PreOrderTraverse(node->m_rightChild);
	}
}

void BinaryTree::InOrderTraverse(const TreeNode *const &node)
{
	if(node != nullptr)
	{
		InOrderTraverse(node->m_leftChild);
		Visit(node);
		InOrderTraverse(node->m_rightChild);
	}
}

void BinaryTree::PostOrderTraverse(const TreeNode *const &node)
{
	if(node != nullptr)
	{
		PostOrderTraverse(node->m_leftChild);
		PostOrderTraverse(node->m_rightChild);
		Visit(node);
	}
}

TreeNode *BinaryTree::GetRootNode()
{
	return m_rootNode;
}

void BinaryTree::Destroy(TreeNode *node)
{
	if(node != nullptr)
	{
		Destroy(node->m_leftChild);
		Destroy(node->m_rightChild);
		delete node;
	}

	return ;

}

再来就是main.cpp,作为测试:

/*********************
main.cpp
*********************/
#include "BinaryTree.h"

int main()
{
	BinaryTree *bTree = new BinaryTree();
	
	int a[7] = {10, 6, 5, 8, 14, 11, 18};

	for(int i = 0; i < 7; ++i)
	{
		bTree->Insert(a[i]);
	}

	if(bTree->Search(11) != nullptr)
	{
		cout << *bTree->Search(11) << endl;
	}
	bTree->PreOrderTraverse(bTree->GetRootNode());
	cout << endl;
	bTree->InOrderTraverse(bTree->GetRootNode());
	cout << endl;
	bTree->PostOrderTraverse(bTree->GetRootNode());
	cout << endl;
	

	BinaryTree bTree2 = *bTree;

	bTree2.PreOrderTraverse(bTree2.GetRootNode());
	cout << endl; 

	bTree->Destroy(bTree->GetRootNode());
	bTree2.Destroy(bTree2.GetRootNode());
	system("PAUSE");

	return 0;
}

这样一棵二叉查找树的创建和查找和遍历都有了。0-0


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值