20120919-二叉树 数据结构《数据结构与算法分析》

简介:

又是一次的毕业季,羡慕嫉妒啊....

二叉查找树类的框架:

复制代码
 1 template <typename Comparable>
 2 class BinarySearchTree
 3 {
 4 public:
 5     BinarySearchTree();
 6     BinarySearchTree(const BinarySearchTree & rhs)
 7     ~BinarySearchTree();
 8 
 9     const Comparable & findMin() const;
10     const Comparable & findMax() const;
11 
12     bool contains(const Comparable & x ) const;
13     bool isEmpty() const;
14     void printTree() const;
15 
16     void makeEmpty();
17     void insert(const Comparable & x);
18     void remove(const Comparable & x);
19 
20     const BinarySearchTree & operator = (const BinarySearchTree & rhs);
21 
22 private:
23     struct BinaryNode
24     {
25         Comparable element;
26         BinaryNode *left;
27         BinaryNode *right;
28 
29         BinaryNode(const  Comparable & theElement,BinaryNode *lt,BinaryNode *rt):element(theElement),left(lt),right(rt){}
30     };
31 
32     BinaryNode *root;
33 
34     void insert (const Comparable & x,BinaryNode * & t) const;
35     void remove (const Comparable & x ,BinaryNode * & t) const;
36     BinaryNode * findMin(BinaryNode *t) const;
37     BinaryNode * findMax(BinaryNode *t) const;
38     bool contains( const Comparable & x,BinaryNode * t) const;
39     void makeEmpty( BinaryNode * & t);
40     void printTree(BinaryNode *t) const;
41     BinaryNode * clone(BinaryNode *t) const;
42 };
复制代码

contains    insert   remove三种操作递归列表:

复制代码
bool contains (const Comparable & x) const
{
    return contains(x,root)
}
void insert(const Comparable & x)
{
    insert (x,root);
}
void remove(const Comparable & x)
{
    remove(x,root);
}
复制代码

二叉查找树的contains操作:

复制代码
 1 bool contains(const Comparable & x,BinaryNode * t) const
 2 {
 3     if( t == NUll )
 4         return false;
 5     else if ( x < t->element )
 6         return contains(x,t->left );
 7     else if (t->element < t)
 8         return contains(x,t->right);
 9     else
10         return true;
11 }
复制代码

 

使用函数对象 实现 二叉查找树:

复制代码
template <typename Object,typename Comparator = less<Object>>
class BinarySearchTree
{
    public:
    private:
        BinaryNode * root;
        Comparable isLessThan;

        bool contains( const Object & x,BinaryNode *t ) const
        {
            if(t == NULL)
                return false;
            else if (isLessThan(x,t->element))
                return contains(x,t->left);
            else if (isLessThan(t->element,x))
                return contains(x,t->right);
            else
                return true;
        }
};
复制代码

findMin方法的递归实现:

复制代码
1 BinaryNode * findMin( BinaryNode * t) const
2 {
3     if( t == NULL)
4         return NULL;
5     if(t->left == NULL)
6         return t;
7     return findMin(t->left);
8 }
复制代码

findMax方法的递归实现:

复制代码
1 BinaryNode * findMax(BinaryNode * t) const
2 {
3     if(t != NULL)
4         while( t ->right !=NULL)
5             t = t->right;
6     return t;
7 }
复制代码

二叉查找树插入操作:

复制代码
 1 void insert( const Comparable & x,BinaryNode * & t )
 2 {
 3     if( t== NULL)
 4         t = new BinaryNode(x,NULL,NULL);
 5     else if (x<t->element)
 6         insert(x,t->left);
 7     else if (t->element < x)
 8         insert(x,t->right);
 9     else
10         ;
11 }
复制代码

二叉查找树 删除操作:

复制代码
 1 void remove (const Comparable & x,BinaryNode * & t)
 2 {
 3     if( t == NULL)
 4         return;
 5     if( x <  t->element)
 6         remove( x,t->left);
 7     else if ( t->element < x)
 8         remove(x,t->right);
 9     else if (t->left != NULL && t->right!=NULL )
10     {
11         t->element = findMin( t->right)->element;
12         remove(t->element , t->right);
13     }
14     else
15     {
16         BinaryNode *oldNode = t;
17         t = ( t->left !=NULL) ? t->left : t->right;
18         delete oldNode;
19     }
20 }
复制代码

析构函数递归实现makeEmpty

复制代码
~BinarySearchTree()
{
    makEmpty();
}
void makeEmpty(BinaryNode * & t)
{
    if( t != NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t = NULL;
}
复制代码

operator= 递归实现clone:

复制代码
 1 const BinarySearchTree & operator=( const BianrySearchTree & rhs)
 2 {
 3     if(this != &rhs)
 4     {
 5         makeEmpty();
 6         root = clone(rhs.root);
 7     }
 8     return *this;
 9 }
10 
11 BinaryNode * clone( BinaryNode * t) const
12 {
13     if( t == NULL)
14         return NULL;
15     return new BinaryNode ( t->element,clone(t->left),clone(t->right));
16 }
复制代码
本文转自博客园xingoo的博客,原文链接:20120919-二叉树 数据结构《数据结构与算法分析》,如需转载请自行联系原博主。
相关文章
|
8天前
|
存储 人工智能 算法
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
这篇文章详细介绍了Dijkstra和Floyd算法,这两种算法分别用于解决单源和多源最短路径问题,并且提供了Java语言的实现代码。
34 3
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
|
4天前
|
存储 算法 Java
Set接口及其主要实现类(如HashSet、TreeSet)如何通过特定数据结构和算法确保元素唯一性
Java Set因其“无重复”特性在集合框架中独树一帜。本文解析了Set接口及其主要实现类(如HashSet、TreeSet)如何通过特定数据结构和算法确保元素唯一性,并提供了最佳实践建议,包括选择合适的Set实现类和正确实现自定义对象的hashCode()与equals()方法。
17 4
|
11天前
|
存储 算法 关系型数据库
数据结构与算法学习二一:多路查找树、二叉树与B树、2-3树、B+树、B*树。(本章为了解基本知识即可,不做代码学习)
这篇文章主要介绍了多路查找树的基本概念,包括二叉树的局限性、多叉树的优化、B树及其变体(如2-3树、B+树、B*树)的特点和应用,旨在帮助读者理解这些数据结构在文件系统和数据库系统中的重要性和效率。
13 0
数据结构与算法学习二一:多路查找树、二叉树与B树、2-3树、B+树、B*树。(本章为了解基本知识即可,不做代码学习)
|
11天前
|
存储 算法 搜索推荐
数据结构与算法学习十七:顺序储存二叉树、线索化二叉树
这篇文章主要介绍了顺序存储二叉树和线索化二叉树的概念、特点、实现方式以及应用场景。
14 0
数据结构与算法学习十七:顺序储存二叉树、线索化二叉树
|
11天前
|
机器学习/深度学习 搜索推荐 算法
探索数据结构:初入算法之经典排序算法
探索数据结构:初入算法之经典排序算法
|
11天前
|
存储 算法
探索数据结构:分支的世界之二叉树与堆
探索数据结构:分支的世界之二叉树与堆
|
11天前
|
存储 算法
数据结构与算法学习十六:树的知识、二叉树、二叉树的遍历(前序、中序、后序、层次)、二叉树的查找(前序、中序、后序、层次)、二叉树的删除
这篇文章主要介绍了树和二叉树的基础知识,包括树的存储方式、二叉树的定义、遍历方法(前序、中序、后序、层次遍历),以及二叉树的查找和删除操作。
15 0
|
11天前
|
算法 程序员 索引
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
栈的基本概念、应用场景以及如何使用数组和单链表模拟栈,并展示了如何利用栈和中缀表达式实现一个综合计算器。
16 1
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
|
11天前
初步认识栈和队列
初步认识栈和队列
35 10
|
5天前
数据结构(栈与列队)
数据结构(栈与列队)
11 1