二叉樹和二叉搜索樹之間的區別

本文翻譯自: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. 二元搜索樹遵循不變量,即左子節點的值應小於根節點的鍵,而右子節點的值應大於根節點的鍵。

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