二叉树首先要有树节点

template<class T>
class BinaryNode
{
public:
T element;
BinaryNode *left;
BinaryNode *right; public:
BinaryNode(T passelement);
~BinaryNode();
}; template<class T>
BinaryNode<T>::BinaryNode(T passelement)
{
this->element=passelement;
this->left=NULL;
this->right=NULL;
} template<class T>
BinaryNode<T>::~BinaryNode()
{
}

二叉树对象则比较复杂

 template<class T>
class BinarySearchTree
{
private:
BinaryNode<T> *m_proot;
public:
const T findMin() const;//获取最小值
const T findMax() const;//获取最大值
bool contains(const T& xele) const;//判断是否包含
void makeEmpty();//清空二叉树
void insert(const T& xele);//插入
void remove(const T& xele);//删除
void inordertrav();//中序遍历
void toDoublelist();//转化为双向链表
void printDoublelist();//打印双向链表
public://构造与析构函数
BinarySearchTree();
BinarySearchTree(const BinarySearchTree& bst);
~BinarySearchTree();
private://全部用于递归调用
void makeEmpty(BinaryNode<T>* &t);
const T findMin(BinaryNode<T>* &t);
void remove(const T& xele, BinaryNode<T>* &t);
void insert(const T& xele, BinaryNode<T>* &t);
void inordertrav(BinaryNode<T>* &t);
void toDoublelist(BinaryNode<T>* &t);
BinaryNode<T>* getlLeftTail(BinaryNode<T>* &t);//获取左子树的最大节点
BinaryNode<T>* getlRightHead(BinaryNode<T>* &t);//获取右子树的最小节点
};

具体函数实现如下:

1.判断是否包含:

template<class T>
bool BinarySearchTree<T>::contains(const T& xele) const
{
BinaryNode<T>* pcurrent = m_proot;
while (true)
{
if (pcurrent == NULL)//指针为空
{
return false;
}
else if (xele < pcurrent->element)
pcurrent = pcurrent->left;
else if (xele > pcurrent->element)
pcurrent = pcurrent->right;
else
{
pcurrent = NULL;
return true;
}
} }

2.返回最小值:

template<class T>
const T BinarySearchTree<T>::findMin() const
{
BinaryNode<T>* m_pcurrent = m_proot;
while (m_pcurrent->left != NULL)
{
m_pcurrent = m_pcurrent->left;
}
return m_pcurrent->element;
}

或:

template<class T>
const T BinarySearchTree<T>::findMin(BinaryNode<T>* &t)
{
if (t == NULL)
return NULL;
if (t->left == NULL)
return t->element;
else
return findMin(t->left); }
template<class T>
const T BinarySearchTree<T>::findMin()
{
findMin(m_proot);
}

3.返回最大值:

template<class T>
const T BinarySearchTree<T>::findMax() const
{
BinaryNode<T>* m_pcurrent = m_proot;
while (m_pcurrent->right != NULL)
{
m_pcurrent = m_pcurrent->right;
}
return m_pcurrent->element;
}

4.清空:

template<class T>
void BinarySearchTree<T>::makeEmpty(BinaryNode<T>* &t)
{
if (t!=NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
template<class T>
void BinarySearchTree<T>::makeEmpty()
{
makeEmpty(m_proot);
}

5.插入:

template<class T>
void BinarySearchTree<T>::insert(const T& xele, BinaryNode<T>* &t)
{
if (t == NULL)
t = new BinaryNode<T>(xele);
else if (xele < t->element)
return insert(xele, t->left);
else if (xele > t->element)
return insert(xele, t->right);
else
;
}
template<class T>
void BinarySearchTree<T>::insert(const T& xele)
{
insert(xele, m_proot);
}

6.删除:

template<class T>
void BinarySearchTree<T>::remove(const T& xele, BinaryNode<T>* &t)
{
if (t == NULL)
return;
else if (xele < t->element)
remove(xele, t->left);
else if (xele > t->element)
remove(xele, t->right);
else
{
if (t->left != NULL&&t->right != NULL)
{
t->element = findMin(t->right);
remove(t->element, t->right);
}
else
{
BinaryNode<T>* oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
}
template<class T>
void BinarySearchTree<T>::remove(const T& xele)
{
remove(xele, m_proot);
}

7.中序遍历:

template<class T>
void BinarySearchTree<T>::inordertrav()
{
inordertrav(m_proot);
}
template<class T>
void BinarySearchTree<T>::inordertrav(BinaryNode<T>* &t)//参数是根节点
{
if (NULL == t)
return;
if (NULL != t->left)
inordertrav(t->left);
cout << t->element << "," << endl;
if (NULL != t->right)
inordertrav(t->right);
}

8.转换为双向链表,需要注意的是:不能使用中序遍历的方法去实现转换,这样会引起指针异常;转换后以前操作二叉树的函数全部失效

template<class T>
BinaryNode<T>* BinarySearchTree<T>::getlLeftTail(BinaryNode<T>* &t)
{
BinaryNode<T>* pC = t;
while (true)
if (NULL != pC->right)
pC = pC->right;
else
break;
return pC;
}
template<class T>
BinaryNode<T>* BinarySearchTree<T>::getlRightHead(BinaryNode<T>* &t)
{
BinaryNode<T>* pC = t;
while (true)
if (NULL != pC->left)
pC = pC->left;
else
break;
return pC;
}
template<class T>
void BinarySearchTree<T>::toDoublelist()
{
toDoublelist(m_proot);
}
template<class T>
void BinarySearchTree<T>::toDoublelist(BinaryNode<T>* &t)
{ if (NULL == t)
return;
if (NULL != t->left)
{
BinaryNode<T>* listtail = getlLeftTail(t->left);//先记录下来要与根节点的左指针相连的。
toDoublelist(t->left);
listtail->right = t;
t->left = listtail;
} /*这个方法会出现问题
//不为空
if (NULL != m_plist)
{
t->left = m_plist;
m_plist->right = t;
}
else//为空表示使双向链表的头
{
m_phead = t;
}
//为下一次连接做准备
m_plist = t; cout << m_plist->element << ",";*/ if (NULL != t->right)
{
BinaryNode<T>* listhead = getlRightHead(t->right);
toDoublelist(t->right);
listhead->left = t;
t->right = listhead; } }

9.打印链表:

template<class T>
void BinarySearchTree<T>::printDoublelist()
{
BinaryNode<T>* phead = m_proot;
while (true)
{
if (phead->left != NULL)
phead = phead->left;
else
break;
}
while (phead->right!=NULL)
{
cout << phead->element << ",";
phead = phead->right;
}
cout << phead->element;
}

C++二叉查找树实现及转化为双向链表的更多相关文章

  1. 【剑指offer】二叉搜索树转双向链表

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26623795 题目描写叙述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表. ...

  2. [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  3. 剑指offer26:将二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

    1 题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 2 思路和方法 在二叉搜索树中,每个结点都有两个分别指向其左.右子树的 ...

  4. 剑指offer 面试题36.二叉搜索树与双向链表

    中序递归,一个pre节点记录前一个节点 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; Tre ...

  5. JAVA集合类(代码手写实现,全面梳理)

    参考网址:https://blog.csdn.net/weixin_41231928/article/details/103413167 目录 一.集合类关系图 二.Iterator 三.ListIt ...

  6. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  7. 九度-剑指Offer

    二维数组中的查找 分析:既然已经给定了每一行从左至右递增,那么对于每一行直接二分查找即可,一开始还想着每一列同样查找一次,后来发现每一行查找一遍就能够遍历所有的元素了. #include <cs ...

  8. JDK1.8 HashMap--treeifyBin()方法

    /*树形化*/ final void treeifyBin(Node<K,V>[] tab, int hash) { int n, index; Node<K,V> e;// ...

  9. HashMap源码分析(Java8)

    1. HashMap public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, ...

随机推荐

  1. hiho1092_have lunch together

    题目 两个人从同一个点出发,在一个餐厅中寻找两个相邻的座位,需要是的从出发点到达座位的距离总和最短.题目链接: Have Lunch Together     最短路程,一开始以为要用dijkstra ...

  2. hiho_1049 二叉树遍历

    题目大意 给出一棵二叉树的前序和中序遍历结果,求出后序遍历的结果.保证二叉树中节点值均不相同. 分析 通过前序和中序遍历的结果,我们可以构建出二叉树,若构建出二叉树,则后序遍历的结果很容易求出(当然递 ...

  3. 将cantk runtime嵌入到现有的APP中

    1,先取cantk-runtime-demos到本地: git clone https://github.com/drawapp8/cantk-runtime-demos 2,创建一个Android ...

  4. js到处excel

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. Java线程(二):线程同步synchronized和volatile

    上篇通过一个简单的例子说明了线程安全与不安全,在例子中不安全的情况下输出的结果恰好是逐个递增的(其实是巧合,多运行几次,会产生不同的输出结果),为什么会产生这样的结果呢,因为建立的Count对象是线程 ...

  6. 小程序---根据数据库反向生成java文件

    工作中写entry太繁琐,写了一个小程序反向生成.从而大大减少了工作量 import java.io.File; import java.io.FileWriter; import java.io.I ...

  7. 笔记3:关于VBS整人代码的浅谈

    今天又看到有人在群里刷屏了.就想到了以前玩过的发QQ骚扰信息程序了.其实蛮简单的 和网上很多的整人代码差不多 一.直接在网上搜索“VBS整人代码”,然后找到有用的代码复制着. ps:在网上有很多有意思 ...

  8. 笔记2:傻瓜式盗QQ程序

    1.一个简单的盗QQ号的方法 仿做一个QQ的窗体 PS:当然里面有用的控件只有两个输入框和一个登陆按钮,其他的尽量做得像一些吧! 点登陆时的后台代码: PS:需要导入两个包:using System. ...

  9. js 时间处理

    1.格式化时间 function GetDateTimeFormatter(value) {        if (value == undefined) {            return &q ...

  10. poj2284 That Nice Euler Circuit(欧拉公式)

    题目链接:poj2284 That Nice Euler Circuit 欧拉公式:如果G是一个阶为n,边数为m且含有r个区域的连通平面图,则有恒等式:n-m+r=2. 欧拉公式的推广: 对于具有k( ...