BinarySearchTree(1)二叉查找树链式递归实现

来源:互联网 发布:淘宝客推广 编辑:程序博客网 时间:2024/05/22 00:16

binary_search_tree.h

/*-----------------------------------------------  Created By EverSteins  Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/#ifndef BINARY_SEARCH_TREE_H#define BINARY_SEARCH_TREE_Htypedef int ElemType;class BinarySearchTree{public:BinarySearchTree():root_(NULL){}~BinarySearchTree();bool Insert(const ElemType& entry);bool Remove(const ElemType& entry);bool Search(const ElemType& entry) const;void InOrderTraverse() const;bool IsEmpty() const;private:struct TreeNode{TreeNode(const ElemType& entry,TreeNode *left,TreeNode *right):entry_(entry),left_(left),right_(right){}ElemType entry_;TreeNode *left_;TreeNode *right_;};void RemoveAllNodes(TreeNode *node);bool Search(const ElemType& target,TreeNode **&node_ptr) const;void InOrderTraverseRecursive(TreeNode *node) const;TreeNode *root_;#define DISALLOW_COPY_AND_ASSIGN(TypeName) \  TypeName(const TypeName&);               \  void operator=(const TypeName&)DISALLOW_COPY_AND_ASSIGN(BinarySearchTree);#undef DISALLOW_COPY_AND_ASSIGN};#endif

binary_search_tree.cc

/*-----------------------------------------------  Created By EverSteins  Email:EverSteins@gmail.com 转载请注明出处 ------------------------------------------------*/  #include "stdafx.h"#include <iostream>#include "utility.h"#include "binary_search_tree.h"using namespace std;BinarySearchTree::~BinarySearchTree(){RemoveAllNodes(root_);}void BinarySearchTree::RemoveAllNodes(TreeNode *node){//采用递归后续遍历删除所有结点if (node == NULL)return;RemoveAllNodes(node->left_);RemoveAllNodes(node->right_);delete node;}bool BinarySearchTree::Insert(const ElemType& entry){return Insert(entry,&root_);}bool BinarySearchTree::Insert(const ElemType& entry,TreeNode **node_ptr){//pre:node_ptr=&root_TreeNode *node = *node_ptr;if (node == NULL){*node_ptr = new TreeNode(entry,NULL,NULL);return true;}if (entry < node->entry_)return Insert(entry,&node->left_);else if (node->entry_ < entry)return Insert(entry,&node->right_);else         //相同元素时return false;}bool BinarySearchTree::Remove(const ElemType& entry){return Remove(entry,&root_);}bool BinarySearchTree::Remove(const ElemType& entry,TreeNode **node_ptr){//pre:node_ptr=&root_TreeNode *node = *node_ptr;if (node == NULL)return false;if (entry == node->entry_){Delete(node_ptr);return true;}else if (entry < node->entry_)return Remove(entry,&node->left_);elsereturn Remove(entry,&node->right_);}bool BinarySearchTree::Delete(TreeNode **node_ptr){//todo}bool BinarySearchTree::Search(const ElemType& entry) const{return Search(entry,root_);}bool BinarySearchTree::Search(const ElemType& entry,TreeNode *node){if (node == NULL)return false;if (entry == node->entry_)return true;else if (entry < node->entry_)return (entry,node->left_);else if (entry > node->entry_)return (entry,node->right_);}void BinarySearchTree::InOrderTraverse() const{InOrderTraverseRecursive(root_);}void BinarySearchTree::InOrderTraverseRecursive(TreeNode *node) const{if (node == NULL)return;InOrderTraverseRecursive(node->left_);cout<<node->entry_<<',';InOrderTraverseRecursive(node->right_);}bool BinarySearchTree::IsEmpty() const{return (root_ == NULL)? true : false;}

utility.h

#ifndef UTILITY_H#define UTILITY_H#include <cstddef>#include <cassert>#include <cstdlib>#include <stdlib.h>#endif