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

#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. vue的roter使用

    1在src下建立router文件夹,再建立router.js import Vue from 'vue' import Router from 'vue-router' import home fro ...

  2. css 两列自适应布局的4种思路

    前面的话 前面已经介绍过css 两列布局中单列定宽单列自适应布局的6种思路的两列布局,而两列自适应布局是指一列由内容撑开,另一列撑满剩余宽度的布局方式.本文将从float.table.flex和gri ...

  3. c语言中的main函数讨论

    **从刚开始写C程序,相比大家便开始写main()了.虽然无数的教科书和老师告诉我们main是程序的入口.那么main函数是怎么被调用的,怎么传入参数,返回的内容到哪里了,返回的内容是什么?接下来我们 ...

  4. 修改手機的 input source 及 charger 及 usb 相關電路後,容易忽略的事項

    input source 及 charger 需要注意, 是否可以在關機的狀況下充電, 當然 開機充電 是一定要的. usb 部分需要注意, 是否可以在沒有電或者是有電的狀況下 download 程式 ...

  5. ext2/3/4的inode结构说明

    系统环境:Ubuntu15.10/ext4 今天在复习<鸟哥的私房菜-基础学习篇>,看到inode大小为128bytes,想看下这128字节里面到底是什么样的. 于是我查了下google, ...

  6. MongoDB的使用[转]

    http://www.cnblogs.com/TankMa/archive/2011/06/08/2074947.html

  7. Codeforces 707C. Pythagorean Triples-推公式的数学题

    两道C题题解,能推出来公式简直是无敌. http://codeforces.com/problemset/problem/707/C codeforces707C. Pythagorean Tripl ...

  8. 不一样视角的Glide剖析

    推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) Glide是一个快速高效的Android图片加载库,注重于平滑的 ...

  9. 在typescript中import第三方类库clipboard报错

    一.问题 在实际开发项目中就遇到了这样的问题,需要在Vue+Typescript项目中添加复制文本的功能,就找了clipboard插件,先是新建了一个新的项目用来实验看看是否好用,都写好了以后发给别人 ...

  10. Network | CIDR

    无类别(现在) 无类别域间路由(Classless Inter-Domain Routing.CIDR)是一个用于给用户分配IP地址以及在互联网上有效地路由IP数据包的对IP地址进行归类的方法. CI ...