二叉树和二叉搜索树之间的区别

本文翻译自:Difference between binary tree and binary search tree

任何人都可以用一个例子解释二叉树二叉搜索树之间的区别吗?


#1楼

参考:https://stackoom.com/question/Qlmx/二叉树和二叉搜索树之间的区别


#2楼

A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element. 二叉树由节点组成,其中每个节点包含“左”指针,“右”指针和数据元素。 The "root" pointer points to the topmost node in the tree. “根”指针指向树中最顶层的节点。 The left and right pointers recursively point to smaller "subtrees" on either side. 左右指针递归地指向任一侧较小的“子树”。 A null pointer represents a binary tree with no elements -- the empty tree. 空指针表示没有元素的二叉树 - 空树。 The formal recursive definition is: a binary tree is either empty (represented by a null pointer), or is made of a single node, where the left and right pointers (recursive definition ahead) each point to a binary tree. 形式递归定义是:二叉树是空的(由空指针表示),或者由单个节点组成,其中左右指针(前面的递归定义)均指向二叉树。

A binary search tree (BST) or "ordered binary tree" is a type of binary tree where the nodes are arranged in order: for each node, all elements in its left subtree are less to the node (<), and all the elements in its right subtree are greater than the node (>). 二叉搜索树 (BST)或“有序二叉树”是一种二叉树,其中节点按顺序排列:对于每个节点,其左子树中的所有元素都少于节点(<),以及所有元素在其右子树中大于节点(>)。

    5
   / \
  3   6 
 / \   \
1   4   9    

The tree shown above is a binary search tree -- the "root" node is a 5, and its left subtree nodes (1, 3, 4) are < 5, and its right subtree nodes (6, 9) are > 5. Recursively, each of the subtrees must also obey the binary search tree constraint: in the (1, 3, 4) subtree, the 3 is the root, the 1 < 3 and 4 > 3. 上面显示的树是二叉搜索树 - “根”节点是5,其左子树节点(1,3,4)是<5,其右子树节点(6,9)是> 5。递归地,每个子树还必须服从二元搜索树约束:在(1,3,4)子树中,3是根,1 <3和4> 3。

Watch out for the exact wording in the problems -- a "binary search tree" is different from a "binary tree". 注意问题中的确切措辞 - “二叉搜索树”与“二叉树”不同。


#3楼

二叉搜索树是一种特殊的二叉树,它具有以下属性:对于任何节点n,n的左子树中的每个后代节点的值小于n的值,并且右子树中的每个后代节点的值是大于n的值。


#4楼

As everybody above has explained about the difference between binary tree and binary search tree, i am just adding how to test whether the given binary tree is binary search tree. 正如上面的每个人都解释了二叉树和二叉搜索树之间的区别,我只是添加如何测试给定的二叉树是否是二叉搜索树。

boolean b = new Sample().isBinarySearchTree(n1, Integer.MIN_VALUE, Integer.MAX_VALUE);
.......
.......
.......
public boolean isBinarySearchTree(TreeNode node, int min, int max)
{

    if(node == null)
    {
        return true;
    }

    boolean left = isBinarySearchTree(node.getLeft(), min, node.getValue());
    boolean right = isBinarySearchTree(node.getRight(), node.getValue(), max);

    return left && right && (node.getValue()<max) && (node.getValue()>=min);

}

Hope it will help you. 希望它会对你有所帮助。 Sorry if i am diverting from the topic as i felt it's worth mentioning this here. 对不起,如果我偏离主题,因为我觉得这里值得一提。


#5楼

Binary Tree is a specialized form of tree with two child (left child and right Child). 二叉树是一种特殊形式的树,有两个孩子(左孩子和右孩子)。 It is simply representation of data in Tree structure 它只是Tree结构中数据的表示

Binary Search Tree (BST) is a special type of Binary Tree that follows following condition: 二进制搜索树(BST)是一种特殊类型的二叉树,遵循以下条件:

  1. left child node is smaller than its parent Node left子节点小于其父节点
  2. right child node is greater than its parent Node 右子节点大于其父节点

#6楼

A binary tree is a tree whose children are never more than two. 二叉树是一棵树,其子项永远不会超过两个。 A binary search tree follows the invariant that the left child should have a smaller value than the root node's key, while the right child should have a greater value than the root node's key. 二元搜索树遵循不变量,即左子节点的值应小于根节点的键,而右子节点的值应大于根节点的键。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章