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

#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. windows服务-监视文件

    配置一个xml其中有是否开启监视.监视时间.监视路径. FileSystemWatcher watcherName = new FileSystemWatcher(); watcherName.Inc ...

  2. Vue实现增删改查功能

    简单的表单CURD功能demo <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  3. live-server 快速搭建静态服务

    作为切图仔呢,平时都是在修改页面样式.展示后台返回的数据.调试js效果,这时候呢就需要搭一个服务器以便我们调试.平时我一般用的都是wamp,在www目录下新建项目然后开始撸代码,不过有时候呢不太方便, ...

  4. ASP.NET MVC 1.0 哈哈。。转过来,还没学。。

    原文发布时间为:2009-06-13 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET MVC 1.0 发布了投递人 itnews 发布于 2009-03-19 00:46 评论(8) ...

  5. angular-关于分页

    列表渲染数据量庞大的时候,我们需要用到一个filter来控制我们的列表进行循环渲染. 这就要用到一个filter,limitTo. 在此,我使用了变量来进行控制,可以随时调换每页的数量,并且配合分页按 ...

  6. PE笔记之NT头PE文件头

    typedef struct _IMAGE_FILE_HEADER {       WORD    Machine;                         //014C-IMAGE_FILE ...

  7. CentOS下Samba使用

    1. 软件 Samba需要以下三个基本软件包,相关依赖包未列出 samba: The Samba SMB server samba-client: Samba (SMB) client program ...

  8. TCP/IP和HTTP协议与Socket的区别联系

    参考资料: http://www.cnblogs.com/goodcandle/archive/2005/12/10/socket.html http://www.2cto.com/net/20121 ...

  9. Python 复习-1

    #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/10/27 22:46 # @Author : lijunjiang # @Fi ...

  10. javascript 动态添加城市

    匿名函数的使用 createTextnode 创建文本 createElement   创建元素 appendChild     将文本或元素追加 <!DOCTYPE html> < ...