自己实现了一下二叉搜索树的数据结构。记录一下:

#include <iostream>

using namespace std;

struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int value) { val=value; left=NULL; right=NULL; }
}; class SearchTree{ public:
SearchTree();
~SearchTree();
void Destory(TreeNode *);
void Insertnode(int);
void Preorder(TreeNode *);
void Inorder(TreeNode *);
void Postorder(TreeNode *);
void Predisplay();
void Indisplay();
void Postdisplay();
private:
TreeNode *root; }; SearchTree::SearchTree()
{
root=NULL;
} SearchTree::~SearchTree()
{
cout<<"析构二叉搜索树:"<<endl;
Destory(root);
} void SearchTree::Destory(TreeNode *node)
{
if(node!=NULL)
{
Destory(node->left);
Destory(node->right);
cout<<node->val<<" ";
delete node;
}
} void SearchTree::Insertnode(int value)
{
if(root==NULL)
root=new TreeNode(value);
else
{
TreeNode *p,*pre;
pre=p=root;
while(p)
{
if(p->val==value)
return;
else if(p->val>value)
{
pre=p;
p=p->left;
}
else
{
pre=p;
p=p->right;
} }
p=new TreeNode(value);
if(pre->val>value)
pre->left=p;
else
pre->right=p;
}
} void SearchTree::Predisplay()
{
Preorder(root);
} void SearchTree::Preorder(TreeNode *root)
{
if(root)
{
cout<<root->val<<" ";
Preorder(root->left);
Preorder(root->right);
}
} void SearchTree::Indisplay()
{
Inorder(root);
} void SearchTree::Inorder(TreeNode *root)
{
if(root)
{
Inorder(root->left);
cout<<root->val<<" ";
Inorder(root->right);
}
} void SearchTree::Postdisplay()
{
Postorder(root);
} void SearchTree::Postorder(TreeNode *root)
{
if(root)
{
Postorder(root->left);
Postorder(root->right);
cout<<root->val<<" ";
}
} int main()
{
SearchTree t;
int a[]={7,4,2,3,15,35,6,45,55,20,1,14};
int n=sizeof(a)/sizeof(a[0]);
cout<<"构造二叉搜索树:"<<endl;
for(int i=0;i<n;++i)
{
cout<<a[i]<<" ";
t.Insertnode(a[i]);
}
cout<<endl<<"先序遍历序列: "<<endl;
t.Predisplay();
cout<<endl<<"中序遍历序列: "<<endl;
t.Indisplay();
cout<<endl<<"后序遍历序列: "<<endl;
t.Postdisplay();
cout<<endl;
return 0;
}

c++实现二叉搜索树的更多相关文章

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

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

  2. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. [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 ...

  7. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

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

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

  9. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  10. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  2. 水晶报表 IE设置

    水晶报表:Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表.水晶报表是业内最专业.功能最强的报表系统,它除了强大的报表功能外,最大的优势是实现了与绝大多数流 ...

  3. 【shell】Shell命令合集(0)

    Ccat zdd 浏览文件zdd的内容cat zdd1 zdd2 浏览多个文件的内容cat -n zdd浏览文件zdd的内容并显示行号 cd 回到起始目录,也即刚登陆到系统的目录,cd后面无参数cd ...

  4. C结构体struct用法小结

    结构体和int,float等类型一样是一种常用的类型,它是由各种基本数据类型构成,通常包含有struct关键字,结构体名,结构体成员,结构体变量. 一.结构体定义 通常有3种定义方式,以例子方式表示: ...

  5. LeetCode OJ--Combination Sum **

    https://oj.leetcode.com/problems/combination-sum/ 给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得 ...

  6. 腾讯云使用liveRoom开启直播时,报“房间已存在”错误?

    利用腾讯云roomService服务,移动直播,创建房间api,CreateRoom时有时报“房间已存在”错误. 分析流程发现,CreateRoom会传入roomId到roomService后台,后台 ...

  7. POJ3086 Treats for the Cows(区间DP)

    题目链接  Treats for the Cows 直接区间DP就好了,用记忆化搜索是很方便的. #include <cstdio> #include <cstring> #i ...

  8. HDU 3549 Flow Problem (dinic模版 && isap模版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意: 给你一个有向图,问你1到n的最大流. dinic模版 (n*n*m) #include ...

  9. luogu P1027 Car的旅行路线

    题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第I个城市中高速铁路了的单位 ...

  10. 我眼中的AI

    !!!!本文禁止转载,引用,仅供观看 最初了解人工智能是在我上大二的时候,在看头条的时候经常看到有关人工智能的事情,当时的我和大多数的人一样,感觉人工智能很神奇.当时就整晚整晚的想人工智能会取代人类呀 ...