二叉搜索树

二叉搜索树

孤街醉人 提交于 2020-02-28 04:39:26
二叉搜索树,又名二叉排序树, 它或者是一棵空树,或者是具有下列性质的二叉树 : 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。 结构体: struct tree { char key; tree *left; tree *right; }; 插入函数: void intree(tree *tr,tree *temp) { if(tr->key>temp->key) { if(tr->left==NULL) tr->left=temp; else intree(tr->left,temp); } else { if(tr->right==NULL) tr->right=temp; else intree(tr->right,temp); } } 例题:hdu 3791 连接:http://acm.hdu.edu.cn/showproblem.php?pid=3791 解析:建立二叉搜索树,然后利用中序遍历返回字符串,查看字符串是否相等。 代码: #include<iostream> #include<string> using namespace std; struct tree { char key; tree *left; tree *right; }; void intree(tree *tr

[HDOJ3791]二叉搜索树

别来无恙 提交于 2020-02-28 04:39:03
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3791 给一个序列,让你判断接下来输入的n个序列是否可以生成同一个二叉树。 BFS一遍就可以了。 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 typedef struct Node { 23 Node* left; 24 Node* right; 25 int data; 26 Node() { left = NULL; right =

力扣第95题 不同的二叉搜索树II

人盡茶涼 提交于 2020-02-27 00:42:35
力扣第95题 不同的二叉搜索树II 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; vector<TreeNode*> generateTree(int start, int end) { vector<TreeNode*> vect; if (start > end) { vect.push_back(NULL); return vect; } for (int i = start; i <= end; i++) { vector<TreeNode*> left = generateTree(start, i - 1); vector<TreeNode*> right = generateTree(i + 1, end); for (vector<TreeNode*>::iterator itorl = left.begin(); itorl != left.end(); itorl++) { for (vector<TreeNode*>::iterator itorr = right.begin(); itorr !=

二叉搜索树转换成单链表

穿精又带淫゛_ 提交于 2020-02-26 15:43:59
题目概述 二叉树数据结构TreeNode可用来表示单向链表(其中left置空,right为下一个链表节点)。实现一个方法,把二叉搜索树转换为单向链表,要求值的顺序保持不变,转换操作应是原址的,也就是在原始的二叉搜索树上直接修改。 思路 BST的半线性结构转换成线性结构,其实就是 中序遍历 而已。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* convertBiNode(TreeNode* root) { if (root == nullptr) return root; std::stack<TreeNode*> stack; // 中序遍历需要的栈 while(root) { stack.push(root); root = root->left; } TreeNode* newHead = stack.top(); // 最左的那棵叶节点即为单链表头结点 while(!stack.empty()) {

未 二叉搜索树与双向链表

走远了吗. 提交于 2020-02-26 13:26:58
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ ///??????????????????????????????? public class Solution { TreeNode head = null; TreeNode realHead = null; public TreeNode Convert(TreeNode pRootOfTree) { ConvertSub(pRootOfTree); return realHead; } private void ConvertSub(TreeNode pRootOfTree) { if(pRootOfTree==null) return; ConvertSub(pRootOfTree.left); if (head == null) { head = pRootOfTree; realHead = pRootOfTree; } else { head.right = pRootOfTree;

二叉搜索树(Binary Search Tree)

烂漫一生 提交于 2020-02-25 07:26:53
一. 定义, 性质 二叉查找树 ( Binary Search Tree ),也称二叉搜索树、有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的 二叉树 : 若任意节点的左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值; 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 任意节点的左、右子树也分别为二叉查找树; 没有键值相等的节点(no duplicate nodes) 中序遍历为递增序列 搜索、插入、删除的复杂度等于树高,期望O(logN),最坏O(N)(数列有序,树退化成线性表)。 二. 查询操作 Node* search(int key) { Node* n = root; while(n!=NULL && key!=n->data) { if (key<n->data) n = n->left; else n = n->right; } return n; } // 找到以x为根的(子)树的最小结点 Node* minimum(Node* x) { if (x == 0) return 0; while (x->left!=NULL) x = x->left; return x; } // find the minimum node in the whole

验证二叉搜索树

依然范特西╮ 提交于 2020-02-23 14:21:34
给定一个二叉树,判断其是否是一个有效的二叉搜索树。 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数。 节点的右子树只包含大于当前节点的数。 所有左子树和右子树自身必须也是二叉搜索树。 示例 1: 输入: 2 / \ 1 3 输出: true 示例 2: 输入: 5 / \ 1 4 / \ 3 6 输出: false 解释: 输入为: [5,1,4,null,null,3,6]。 根节点的值为 5 ,但是其右子节点值为 4 。 code:递归 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { private: long preVisit=LONG_MIN;//int有边界值 public: bool isValidBST(TreeNode* root) { if(root==nullptr)//不加||(root->left==nullptr&&root->right==nullptr),因为遍历到叶子结点时不会更新perVisit值

二叉搜索树(Binary Search Tree)

谁都会走 提交于 2020-02-21 08:07:27
二叉搜索树 (BST, Binary Search Tree ),也称 二叉排序树 或 二叉查找树 。 二叉搜索树:一棵二叉树,可以为空;如果不为空,满足以下性质: 非空 左子树 的 所有键值小于其根结点 的键值; 非空 右子树 的 所有键值大于其根结点 的键值; 左右子树都是二叉搜索树 ; Wiki中的定义 : The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. The left and right subtrees each must also be a binary search tree. Each node can have up to two successor nodes. There must be no duplicate nodes . A unique path exists from the root to every other node. 二叉搜索树操作的特别函数: Position Find(ElementType X,BinTree BST)

将有序数组转换为二叉搜索树

谁都会走 提交于 2020-02-18 09:12:59
问题描述: 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。 本题中,一个高度平衡二叉树是指一个二叉树 每个节点 的左右两个子树的高度差的绝对值不超过 1。 示例: 给定有序数组: [-10,-3,0,5,9], 一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树: 0 / \ -3 9 / / -10 5 解题思路: 用数组最中间的的值将数组分为三部分:左半部分、中间值、右半部分; 用中间值构造节点,数组左半部分构造节点的左子树,右半部分右子树; 对数组的左半部分与右半部分重复上述操作。 实现代码: private static TreeNode test(int nums[], int lo, int hi) { if (lo > hi) return null; if (lo == hi) return new TreeNode(nums[lo]); int mid = lo + (hi - lo) / 2; TreeNode node = new TreeNode(nums[mid]); node.left = test(nums, lo, mid-1); node.right = test(nums, mid+1, hi); return node; } public static TreeNode

【数据结构】二叉搜索树的概念和常见操作

独自空忆成欢 提交于 2020-02-16 19:36:00
什么是二叉搜索树? 我们把一般的查找操作分为两类,静态查找和动态查找。在静态查找中有一种很快的查找方法(二分查找时间复杂度为O( log(n) )。它之所以可以将时间复杂度降的这么低,是因为在查找之前对数据进行了顺序的排序。在查找时查找的顺序是固定的,是一个判定树一样的结构。把一个线性的查找过程转换成了一个类似树的查找过程。查找效率就是树的高度。从此我们得到启示:在查找时为什么不能把数据直接用二叉树的结构存储。比起线性结构树的动态性更强,它的插入删除操作更加方便。这样就形成了二叉搜索树。 二叉搜索树定义 : 一颗二叉树,它可以为空,如果不为空,满足下边的性质: (1)非空左子树的所有节点的值,都小于根节点的值。 (2)非空右子树的所有节点的值,都大于根节点的值。 (3)左右子树都是二叉搜索树。 这样在查找的时候,从根节点找,如果小于去左子树找,如果大于去右子树找。 二叉搜索树的常见操作: //从二叉搜索树中找到元素data,并返回该节点的地址,如果找不到返回NULL; Binary_Tree * Find ( Binary_Tree * BST , Elementype data ) ; //从搜索二叉树中BST中,找到最小值data并返回该节点的地址(如果树为空,返回NULL) Binary_Tree * Find_Min ( Binary_Tree * BST ) ; /