Binary search tree
- #ifndef __TREE_H
- #define __TREE_H
- #include <iostream>
- template<typename T> class TreeNode {
- private:
- T _data;
- TreeNode<T>* _left;
- TreeNode<T>* _right;
- TreeNode<T>* _parent;
- public:
- TreeNode(T data,
- TreeNode<T>* parent=,
- TreeNode<T>* left=,
- TreeNode<T>* right=)
- :_data(data),_parent(parent),_left(left),_right(right) {}
- void insertAtLeft(T data);
- void insertAtRight(T data);
- T& getData();
- TreeNode<T>*& getLeft();
- TreeNode<T>*& getRight();
- TreeNode<T>*& getParent();
- };
- template<typename T>
- T& TreeNode<T>::getData()
- {
- return _data;
- }
- template<typename T>
- TreeNode<T>*& TreeNode<T>::getLeft()
- {
- return _left;
- }
- template<typename T>
- TreeNode<T>*& TreeNode<T>::getRight()
- {
- return _right;
- }
- template<typename T>
- TreeNode<T>*& TreeNode<T>::getParent()
- {
- return _parent;
- }
- template<typename T>
- void TreeNode<T>::insertAtLeft(T data)
- {
- _left = new TreeNode(data,this);
- }
- template<typename T>
- void TreeNode<T>::insertAtRight(T data)
- {
- _right = new TreeNode(data,this);
- }
- template <typename T> class Tree{
- private:
- TreeNode<T>* _root;
- int _size;
- protected:
- TreeNode<T>* findIn(TreeNode<T>* position,T element) const;
- /*
- * The last tmp argument is necessary,
- * it is used here to changed the parent's _left/_right field
- * to potint the node which is created
- */
- TreeNode<T>* insertIn(TreeNode<T>* position,
- T element,TreeNode<T>* tmp);
- void travIn(TreeNode<T>* position);
- void travPrev(TreeNode<T>* position);
- void travPost(TreeNode<T>* position);
- public:
- Tree(T data)
- :_root(new TreeNode<T> (data)),_size() { }
- TreeNode<T>* find(T element) const;
- TreeNode<T>* findMax() const;
- TreeNode<T>* findMin() const;
- TreeNode<T>* insert(T data);
- void traversalIn();
- void traversalPrev();
- void traversalPost();
- };
- template<typename T>
- void Tree<T>::travIn(TreeNode<T>* position)
- {
- if(!position)
- return ;
- travIn(position->getLeft());
- std::cout << position->getData() << " ";
- travIn(position->getRight());
- }
- template<typename T>
- void Tree<T>::travPrev(TreeNode<T>* position)
- {
- if(!position)
- return ;
- std::cout << position->getData() << " ";
- travPrev(position->getLeft());
- travPrev(position->getRight());
- }
- template<typename T>
- void Tree<T>::travPost(TreeNode<T>* position)
- {
- if(!position)
- return ;
- travPost(position->getLeft());
- travPost(position->getRight());
- std::cout << position->getData() << " ";
- }
- template<typename T>
- void Tree<T>::traversalPost()
- {
- travPost(_root);
- std::cout << std::endl;
- }
- template<typename T>
- void Tree<T>::traversalPrev()
- {
- travPrev(_root);
- std::cout << std::endl;
- }
- template<typename T>
- void Tree<T>::traversalIn()
- {
- travIn(_root);
- std::cout << std::endl;
- }
- template<typename T>
- TreeNode<T>* Tree<T>::insertIn(TreeNode<T>* position,T data,
- TreeNode<T>* tmp)
- {
- if(!position){
- if(!tmp)
- return new TreeNode<T>(data);
- else
- return (tmp->getData() >data ? tmp->getLeft():tmp->getRight())
- = new TreeNode<T>(data,tmp);
- }
- if(data < position->getData())
- insertIn(position->getLeft(),data,position);
- else if(position->getData() < data)
- insertIn(position->getRight(),data,position);
- else
- return position;
- }
- template<typename T>
- TreeNode<T>* Tree<T>::insert(T data)
- {
- return insertIn(_root,data,);
- }
- template<typename T>
- TreeNode<T>* Tree<T>::findIn(TreeNode<T>* position,T data)const
- {
- while(position && position->getData() != data)
- {
- if(position->getData() > data)
- position = position->getLeft();
- else if(position->getData() < data)
- position = position->getRight();
- }
- return position;
- }
- template<typename T>
- TreeNode<T>* Tree<T>::find(T data) const
- {
- return findIn(_root,data);
- }
- template<typename T>
- TreeNode<T>* Tree<T>::findMax() const
- {
- TreeNode<T>* tmp = _root;
- while(tmp->getRight())
- tmp = tmp->getRight();
- return tmp;
- }
- template<typename T>
- TreeNode<T>* Tree<T>::findMin() const
- {
- TreeNode<T>* tmp = _root;
- while(tmp->getLeft())
- tmp = tmp->getLeft();
- return tmp;
- }
- #endif
二叉搜索树
Binary search tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
随机推荐
- (原) 1.2 Zookeeper伪集群安装
本文为原创文章,转载请注明出处,谢谢 Zookeeper伪集群安装 zookeeper单机安装配置可以查看 1.1 zookeeper单机安装 1.复制三份zookeeper,分别为zookeeper ...
- 经典案例:那些让人赞不绝口的创新 HTML5 网站
在过去的10年里,网页设计师使用 Flash.JavaScript 或其他复杂的软件和技术来创建网站.但现在你可以前所未有的快速.轻松地设计或创造互动的.有趣好看的网站.如何创建?答案是 HTML5 ...
- 微信网页授权(OAuth2.0) PHP 源码简单实现
提要: 1. 建议对OAuth2.0协议做一个学习. 2. 微信官方文档和微信官网工具要得到充分利用. 比较简单,直接帖源代码了.其中“xxxxxxxxxx”部分,是需要依据自己环境做替换的 /** ...
- git和svn
git 分布式管理工具 svn 集中式管理工具 1. Git是分布式的,SVN是集中式的,好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并,也可以不用联网在本地提交 ...
- Android自带的theme
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 android:theme="@andro ...
- Android Property Animation动画
3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...
- SQL for SQLite
语法 verb + subject + predicate commannds(命令) SQL由命令组成,以分号为结束.命令有token组成,token由white space分隔,包括空格.tab. ...
- 转 #ifndef/#define/#endif使用详解
想必很多人都看过“头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用”.但是是否能理解“被重复引用”是什么意思?是不能在不同的两个文件中使用include来包含这个头文件 ...
- Android实现与PHP服务器的交互
今天算是有点小激动呢!拿到Android与PHP这个课题已经两个星期了,直到今天才算是有了一点点小收获. 虽然还是没能成功上传到服务器,不过已经看到了曙光,已经实现了一半了,那就是已经连接到了服务器. ...
- 阿里云服务器怎么去掉tomcat的8080端口
前言:最近在阿里云申请了一个云服务器,然后已买了一个域名.比如www.haha.com ; 在云服务器上安装了tomcat ,访问服务器就成了 http://www.haha.com:8080/ . ...