二叉查找树(binary search tree)

二叉查找树的性质:对于树中的每个节点x,它的左子树中所有项的值不大于x的值,它的右子树中所有项的值不小于x的值。

二叉查找树应具有以下操作
  ① 寻找最小项 FIND_MIN
  ② 寻找最大项 FIND_MAX
  ③ 是否包含 CONTAINS
  ④ 树是否为空 IS_EMPTY
  ⑤ 清空树 MAKE_EMPTY
  ⑥ 插入节点 INSERT
  ⑦ 移除节点 REMOVE
  ⑧ 打印树 PRINT_TREE (中序遍历)
  ⑨ 深拷贝 DEEP_CLONE

二叉查找树(binary search tree)的抽象数据类型(abstract data type)见如下类模板

  1 #include <iostream>
  2
  3 using namespace std;
  4
  5 template <typename Comparable>
  6 class BinarySearchTree
  7 {
  8 public:
  9     BinarySearchTree() { root = NULL; }
 10     BinarySearchTree(const BinarySearchTree &rhs) { root = deepClone(rhs.root); }
 11     ~BinarySearchTree() { makeEmpty(); }
 12
 13     bool contains(const Comparable &x) const { return contains(x, root); }
 14     const Comparable& findMin() const { return findMin(root); }
 15
 16     const Comparable& findMax() const { return findMax(root); }
 17     bool isEmpty() const { return (!root)?true:false; }
 18
 19     void makeEmpty() { makeEmpty(root); }
 20     void insert(const Comparable &x) { insert(x, root); }
 21     void remove(const Comparable &x) { remove(x, root); }
 22
 23     void printTree() const { printTree(root); cout << endl; }
 24
 25     const BinarySearchTree& operator=(const BinarySearchTree &rhs)
 26     {
 27         if (this != &rhs)
 28         {
 29             makeEmpty();
 30             root = deepClone(rhs.root);
 31         }
 32         return *this;
 33     }
 34
 35 private:
 36     struct BinaryNode
 37     {
 38         Comparable element;
 39         BinaryNode *left;
 40         BinaryNode *right;
 41
 42         BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt): element(theElement),left(lt),right(rt) { }
 43     };
 44
 45     BinaryNode *root;
 46
 47
 48     BinaryNode * findMin(BinaryNode *t) const
 49     {
 50         if (!t)
 51             return NULL;
 52         if (!t->left)
 53             return t;
 54         return findMin(t->left);
 55     }
 56
 57     BinaryNode * findMax(BinaryNode *t) const
 58     {
 59         if (!t)
 60             while (!t->right)
 61                 t = t->right;
 62         return t;
 63     }
 64
 65     bool contains(const Comparable &x, BinaryNode *t) const
 66     {
 67         if (!t)
 68             return false;
 69         else if (x < t->element)
 70             return contains(x, t->left);
 71         else if (x > t->element)
 72             return contains(x, t->right);
 73         else
 74             return true;
 75     }
 76
 77     void makeEmpty(BinaryNode *&t)
 78     {
 79         if (t)
 80         {
 81             makeEmpty(t->left);
 82             makeEmpty(t->right);
 83             delete t;
 84         }
 85         t = NULL;
 86     }
 87
 88     void insert(const Comparable &x, BinaryNode *&t) const
 89     {
 90         if (!t)
 91             t = new BinaryNode(x, NULL, NULL);
 92         else if (x < t->element)
 93             insert(x, t->left);
 94         else if (x > t->element)
 95             insert(x, t->right);
 96         else
 97             ;
 98     }
 99
100     void remove(const Comparable &x, BinaryNode *&t) const
101     {
102         if (!t)
103             return;
104         if (x < t->element)
105             remove(x, t->left);
106         else if (x > t->element)
107             remove(x, t->right);
108         else if (!t->left && !t->right)
109         {
110             t->element = findMin(t->right)->element;
111             remove(t->element, t->right);
112         }
113         else
114         {
115             BinaryNode *oldNode = t;
116             t = (!t->left)?t->left:t->right;
117             delete oldNode;
118         }
119     }
120
121     void printTree(BinaryNode *t) const
122     {
123         if (!t)
124             return;
125         printTree(t->left);
126         cout << t->element << " ";
127         printTree(t->right);
128     }
129
130     BinaryNode * deepClone(BinaryNode *t) const
131     {
132         if (!t)
133             return NULL;
134         return new BinaryNode(t->element, deepClone(t->left), deepClone(t->right));
135     }
136 };

main.cpp 如下

#include <ctime>

#include "BinarySearchTree.hpp"

using namespace std;

const int LENGTH = 18;

void generateTree(BinarySearchTree<int> *tree)
{
    srand((unsigned int)time(NULL));
    int i = LENGTH;
    while(i--)
    {
        tree->insert(rand() % 100);
    }
}

int main()
{
    BinarySearchTree<int> *tree = new BinarySearchTree<int>();
    generateTree(tree);
    tree->printTree();
}

结果:

时间: 2025-01-12 11:47:41

二叉查找树(binary search tree)的相关文章

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

二叉查找树(Binary Search Tree),也称二叉排序树(binary sorted tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值 任意节点的左.右子树也分别为二叉查找树 没有键值相等的节点(no duplicate nodes) 本文地址:http://www.cnblogs.com/archimedes/p/binary-search-tree

【LeetCode】二叉查找树 binary search tree(共14题)

链接:https://leetcode.com/tag/binary-search-tree/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [220]Contains Duplicate III [315]Count of Smaller Numbers After Self [327]Count of Range Sum [352]Data Stream as Disjoint Intervals [493]

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

[leetcode] Validate Binary Search Tree (检验是否为二叉查找树) Python

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: 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

二叉查找树——A1064.Complete Binary Search Tree(30) 构建完全二叉查找树,利用完全二叉查找树的性质:左孩子为2x ,右孩子为 2x + 1

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 1010; int temp[maxn],initial[maxn],n; int ind; void inorder(int root){//中序遍历 if(root > n){ return; } inorder(

【LeetCode】Validate Binary Search Tree 二叉查找树的判断

题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树的所有点都小于或等于这个点的值,右子树的所有节点的值大于该节点的值: 2.最左节点值最小,最右节点值最大: 3.中序遍历结果值是一个非降的数列 问题:如果用Integer.MAX_VALUE会过不了测试用例,可是按照TreeNode中的定义,val值也是int呀,没办法用了Long,如果谁知道,麻烦

【Recover Binary Search Tree】cpp

题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}&

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 follows: 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

LeetCode98 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). (Medium) Assume a BST is defined as follows: 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