Difference between binary tree and binary search tree

后端 未结 12 1650
情深已故
情深已故 2020-11-29 14:44

Can anyone please explain the difference between binary tree and binary search tree with an example?

相关标签:
12条回答
  • 2020-11-29 14:52

    Binary Tree stands for a data structure which is made up of nodes that can only have two children references.

    Binary Search Tree (BST) on the other hand, is a special form of Binary Tree data structure where each node has a comparable value, and smaller valued children attached to left and larger valued children attached to the right.

    Thus, all BST's are Binary Tree however only some Binary Tree's may be also BST. Notify that BST is a subset of Binary Tree.

    So, Binary Tree is more of a general data-structure than Binary Search Tree. And also you have to notify that Binary Search Tree is a sorted tree whereas there is no such set of rules for generic Binary Tree.

    Binary Tree

    A Binary Tree which is not a BST;

             5
           /   \
          /     \
         9       2
        / \     / \
      15   17  19  21
    

    Binary Search Tree (sorted Tree)

    A Binary Search Tree which is also a Binary Tree;

             50
           /    \
          /      \
         25      75
        /  \    /  \
      20    30 70   80
    

    Binary Search Tree Node property

    Also notify that for any parent node in the BST;

    • All the left nodes have smaller value than the value of the parent node. In the upper example, the nodes with values { 20, 25, 30 } which are all located on the left (left descendants) of 50, are smaller than 50.

    • All the right nodes have greater value than the value of the parent node. In the upper example, the nodes with values { 70, 75, 80 } which are all located on the right (right descendants) of 50, are greater than 50.

    There is no such a rule for Binary Tree Node. The only rule for Binary Tree Node is having two childrens so it self-explains itself that why called binary.

    0 讨论(0)
  • 2020-11-29 14:54

    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

    Binary Search Tree (BST) is a special type of Binary Tree that follows following condition:

    1. left child node is smaller than its parent Node
    2. right child node is greater than its parent Node
    0 讨论(0)
  • 2020-11-29 14:55

    A tree can be called as a binary tree if and only if the maximum number of children of any of the nodes is two.

    A tree can be called as a binary search tree if and only if the maximum number of children of any of the nodes is two and the left child is always smaller than the right child.

    0 讨论(0)
  • 2020-11-29 14:56

    A binary search tree is a special kind of binary tree which exhibits the following property: for any node n, every descendant node's value in the left subtree of n is less than the value of n, and every descendant node's value in the right subtree is greater than the value of n.

    0 讨论(0)
  • 2020-11-29 14:56

    Binary tree

    Binary tree can be anything which has 2 child and 1 parent. It can be implemented as linked list or array, or with your custom API. Once you start to add more specific rules into it, it becomes more specialized tree. Most common known implementation is that, add smaller nodes on left and larger ones on right.

    For example, a labeled binary tree of size 9 and height 3, with a root node whose value is 2. Tree is unbalanced and not sorted. https://en.wikipedia.org/wiki/Binary_tree

    For example, in the tree on the left, A has the 6 children {B,C,D,E,F,G}. It can be converted into the binary tree on the right.

    Binary Search

    Binary Search is technique/algorithm which is used to find specific item on node chain. Binary search works on sorted arrays.

    Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful or the remaining half is empty. https://en.wikipedia.org/wiki/Binary_search_algorithm

    A tree representing binary search. The array being searched here is [20, 30, 40, 50, 90, 100], and the target value is 40.

    Binary search tree

    This is one of the implementations of binary tree. This is specialized for searching.

    Binary search tree and B-tree data structures are based on binary search.

    Binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type of container: data structures that store "items" (such as numbers, names etc.) in memory. https://en.wikipedia.org/wiki/Binary_search_tree

    A binary search tree of size 9 and depth 3, with 8 at the root. The leaves are not drawn.

    And finally great schema for performance comparison of well-known data-structures and algorithms applied:

    Image taken from Algorithms (4th Edition)

    0 讨论(0)
  • 2020-11-29 15:03

    In a Binary search tree, all the nodes are arranged in a specific order - nodes to the left of a root node have a smaller value than its root, and all the nodes to the right of a node have values greater than the value of the root.

    0 讨论(0)
提交回复
热议问题