/// Binary Search Tree - Implemenation in C++
/// Simple program to create a BST of integers and search an element in it
#include<iostream>
#include "cstdio"
#include "queue"
using namespace std;
///Definition of Node for Binary search tree
struct BstNode {
int data;
BstNode* left;
BstNode* right;
};
bool fist=true;
/// Function to create a new Node in heap
BstNode* GetNewNode(int data) {
BstNode* newNode = new BstNode();
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
} ///To insert data in BST, returns address of root node
BstNode* Insert(BstNode* root,int data) {
if(root == NULL) { // empty tree
root = GetNewNode(data);
} else if(data <= root->data) {
root->left = Insert(root->left,data);
} else {
root->right = Insert(root->right,data);
}
return root;
}
///To search an element in BST, returns true if element is found
bool Search(BstNode* root,int data) {
if(root == NULL) {
return false;
}
else if(root->data == data) {
return true;
}
else if(data <= root->data) {
return Search(root->left,data);
}
else {
return Search(root->right,data);
}
} ///PreOrder Display
void PreOrder(BstNode *root)
{
if(root!=NULL){
PreOrder(root->left);
PreOrder(root->right);
printf("%d ",root->data);
}
}
///PostOrder Display
void PostOrder(BstNode *root)
{
if(root!=NULL){
printf("%d ",root->data);
PostOrder(root->left);
PostOrder(root->right);
}
} ///InOrder Display
void InOrder(BstNode *root)
{
if(root!=NULL){
InOrder(root->left);
printf("%d ",root->data);
InOrder(root->right);
}
} ///LevelOrder Display
void LevelOrder(BstNode *root)
{
queue<BstNode *>Q;
while(!Q.empty())Q.pop();
Q.push(root);
while(!Q.empty())
{
root=Q.front();
Q.pop();
if(fist==false)
{
cout<<root->data;
fist=true;
}
else
{
cout<<" "<<root->data;
}
if(root->left!=NULL)Q.push(root->left);
if(root->right!=NULL)Q.push(root->right);
}
cout<<endl;
} ///################### Test ############################
int main() {
BstNode* root = NULL; // Creating an empty tree, root is to store the address of the root node
/*Code to test the logic*/
root = Insert(root,);
root = Insert(root,);
root = Insert(root,);
root = Insert(root,);
root = Insert(root,);
root = Insert(root,);
cout<<"PreOrder:  ";
PreOrder(root);
cout<<endl;
cout<<"InOrder(从小到大):  ";
InOrder(root);
cout<<endl;
cout<<"PostOrder:  ";
PostOrder(root);
cout<<endl;
cout<<"LevelOrder:  ";
LevelOrder(root);
cout<<endl; int number;
cout<<"Enter number be searched\n";
cin>>number; if(Search(root,number) == true) cout<<"Found\n";
else cout<<"Not Found\n"; return ;
}

数据结构基础---Binary Search Tree的更多相关文章

  1. 数据结构之Binary Search Tree (Java)

    二叉查找树简介 二叉查找树(Binary Search Tree), 也成二叉搜索树.有序二叉树(ordered binary tree).排序二叉树(sorted binary tree), 是指一 ...

  2. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

  3. 【数据结构05】红-黑树基础----二叉搜索树(Binary Search Tree)

    目录 1.二分法引言 2.二叉搜索树定义 3.二叉搜索树的CRUD 4.二叉搜索树的两种极端情况 5.二叉搜索树总结 前言 在[算法04]树与二叉树中,已经介绍过了关于树的一些基本概念以及二叉树的前中 ...

  4. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  5. 数据结构 《5》----二叉搜索树 ( Binary Search Tree )

    二叉树的一个重要应用就是查找. 二叉搜索树 满足如下的性质: 左子树的关键字 < 节点的关键字 < 右子树的关键字 1. Find(x) 有了上述的性质后,我们就可以像二分查找那样查找给定 ...

  6. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

    1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...

  7. 《数据结构与算法分析——C语言描述》ADT实现(NO.03) : 二叉搜索树/二叉查找树(Binary Search Tree)

    二叉搜索树(Binary Search Tree),又名二叉查找树.二叉排序树,是一种简单的二叉树.它的特点是每一个结点的左(右)子树各结点的元素一定小于(大于)该结点的元素.将该树用于查找时,由于二 ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. 二叉查找树(binary search tree)详解

    二叉查找树(Binary Search Tree),也称二叉排序树(binary sorted tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树上所有结点的值均小于 ...

随机推荐

  1. 2016年JD工作遇到的问题:1-5,搭建环境和熟悉项目过程中的坑

    1.更新不需要权限的项目A,却提示没有权限. 先从Git上更新项目A的代码,提示没有权限. 然后从Git上更新其它项目B的代码,正常. 再更新项目A的代码,正常了. 奇葩问题! 2.Eclipse中, ...

  2. fsync体会

    看这个链接:http://www.postgresql.org/docs/9.1/static/runtime-config-wal.html 是这样说的: fsync (boolean) If th ...

  3. Android StateListDrawable的坑

    有问题的代码: StateListDrawable background = new StateListDrawable(); CircleDrawable pressedDrawable = new ...

  4. CC3100BoosterPack和CC31XXEMUBOOST板子的测试

    1. 先测试右边的CC3100BoosterPack,测试发现LDO坏了,无法输出3.3V,所以只能用左边的板子供电. 2. 插上CC31XXEMUBOOST板子的J1,两个板子插在一起,等待驱动安装 ...

  5. Linux - 信息收集

    1. #!,代表加载器(解释器)的路径,如: #!/bin/bash echo "Hello Boy!" 上面的意思是说,把下面的字符(#!/bin/bash以下的所有字符)统统传 ...

  6. Go中的系统Signal处理

    package main import "fmt" import "os" import "os/signal" import " ...

  7. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  8. 【js笔记】数组那些事[0]

    js中数组是一个特殊的对象,索引是它的属性,整数索引在内部被转化为字符串类型. 1 数组的创建 new关键字方法:var arr=new Array() var arr=new Array(10); ...

  9. 杀死 tomcat 进程的脚本

    新建一个.sh 文件 把下面的内容复制进去.然后 把这个文件放到tomcat 的bin目录下在关闭tomcat 执行这个脚本. 可以解决 在关闭tomcat的时候 总是遗留一些tomcat进程没有结束 ...

  10. 《百词斩·象形9000》第一册(上) 符号Symbol 1

    001-upon prep. 在......上面 Wish upon a star.#对着星星许愿. 002-think V. 想,思索,认为:以为,预料 What do you think?#你认为 ...