查找二叉树的C++代码实现

(1)查找二叉树的查找效率是logn,构建二叉树的效率是nlogn

(2)insert操作很简单,remove操作,如果存在左右子节点需要注意将右子树的最小节点替换到要删除值,同时删除该最小节点

(3)如果使用template类,记得将实现代码和声明代码都放在.h文件中。如果分别放在.h和.cpp文件,会出现link error.

#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H

#include <iostream>

using namespace std;

/*
1.template类不能分开成.h和.cpp文件,否则将报错Link 2009错误
*/

//定义遍历模式
enum ORDER_MODE
{
ORDER_MODE_PREV = 0,
ORDER_MODE_MID = 1,
ORDER_MODE_POST = 2
};

//定义二叉树节点,只有左右子节点
template<typename Comparable>
struct BinaryNode
{
Comparable element;
BinaryNode* left;
BinaryNode* right;

BinaryNode(const Comparable& theElement, BinaryNode<Comparable>* lt, BinaryNode<Comparable>* rt):
element(theElement), left(lt), right(rt)
{

}
};

template<typename Comparable>
class BinarySearchTree
{
public:
BinarySearchTree(void)
{
m_root = NULL;
}

BinarySearchTree(const BinarySearchTree<Comparable>& rhs)
{
m_root = rhs.m_root;
}
~BinarySearchTree()
{
makeEmpty();
}

public:

const Comparable& findMin() const
{
BinaryNode<Comparable>* node = findMin(m_root);
if (node != NULL)
{
return node->element;
}
}
const Comparable& findMax() const
{
BinaryNode<Comparable>* node = findMax(m_root);
if (node != NULL)
{
return node->element;
}
}
bool contains(const Comparable& x) const
{
return contains(x, m_root);
}
bool isEmpty() const
{
return m_root != NULL? true, false;
}
void printTree(ORDER_MODE eOrderMode) const
{
if (eOrderMode == ORDER_MODE_PREV)
{
printTreeInPrevOrder(m_root);
}
else if (eOrderMode == ORDER_MODE_MID)
{
printTreeInMidOrder(m_root);
}
else if (eOrderMode == ORDER_MODE_POST)
{
printTreePostOrder(m_root);
}
}

void makeEmpty()
{
makeEmpty(m_root);
}
void insert(const Comparable& x)
{
insert(x, m_root);
}
void remove(const Comparable& x)
{
remove(x, m_root);
}


private:
void insert(const Comparable& x, BinaryNode<Comparable>*& t) const
{
if (t == NULL)
{
t = new BinaryNode<Comparable>(x, NULL, NULL);
}
else if (x < t->element)
{
insert(x, t->left);
}
else if (x > t->element)
{
insert(x, t->right);
}
}

void remove(const Comparable& x, BinaryNode<Comparable>*& t) const
{
if (t == NULL)
{
return;
}
if (x < t->element)
{
remove(x, t->left);
}
else if (x > t->element)
{
remove(x, t->right);
}
else 
{
if (t->left != NULL &&
t->right != NULL)
{
//存在两个子节点,将右子树最小的节点替换到该节点
//删除右子树最小的节点,该节点最多有一个右节点
t->element = findMin(t->right)->element;
remove(t->element, t->right);
}
else
{
BinaryNode<Comparable>* oldNode = t;
t = (t->left != NULL)? t->left: t->right;
delete oldNode;
}
}
}
BinaryNode<Comparable>* findMin(BinaryNode<Comparable>* t) const
{
if (t == NULL)
{
return NULL;
}

if (t->left == NULL)
{
return t;
}
else 
{
return findMin(t->left);
}
}
BinaryNode<Comparable>* findMax(BinaryNode<Comparable>* t) const
{
if (t != NULL)
{
while (t->right != NULL)
{
t = t->right;
}
}
return  t;
}
bool contains(const Comparable& x, BinaryNode<Comparable>* t) const
{
if (t == NULL)
{
return false;
}
else if (x < t->element)
{
return contains(x, t->left);
}
else if (x > t->element)
{
return contains(x, t->right);
}
else 
{
return true;
}
}
void makeEmpty(BinaryNode<Comparable>*& t)
{
if (t != NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}

void printTreeInPrevOrder(BinaryNode<Comparable>* t) const
{
if (t != NULL)
{
cout << t->element << ” “;
printTreeInPrevOrder(t->left);
printTreeInPrevOrder(t->right);
}
}
void printTreeInMidOrder(BinaryNode<Comparable>* t) const
{
if (t != NULL)
{
printTreeInMidOrder(t->left);
cout << t->element << ” “;
printTreeInMidOrder(t->right);
}
}
void printTreePostOrder(BinaryNode<Comparable>* t) const
{
if (t != NULL)
{
printTreePostOrder(t->left);
printTreePostOrder(t->right);
cout << t->element << ” “;
}
}

private:
BinaryNode<Comparable>* m_root;
};

#endif

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/luofeixiongsix/article/details/50623473
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞