Skip to main content

Difference between Binary Tree and Binary Search Tree Vijay Sinha The Crazy Programmer

In this article, we will look at the difference between Binary Tree and Binary Search Tree. These topics are very important because these act as an underlying data structure for various other data structures. So, we will look at the description of each with examples and compare their properties.

Binary Tree

A Binary tree is a type of Tree data structure, Non-Linear (data is not contiguous or in sequence) in nature. It represents data in a hierarchical format. A binary tree like a regular tree comprises of nodes that are not Ordered. Each node in a binary tree can have a maximum of 2 child nodes. It can have 0,1 or 2 nodes. A node is represented as a parent or child node. A Parent node can have at most two children- Left Child and Right Child. The node which has at least two children is called Internal Node. Hence, each node in memory contains the following attributes:

  • Data (Can be of any type).
  • Left Pointer which has the reference for Left Child.
  • Right Pointer which has the reference for Right Child.

Also Read: Difference between Tree and Graph Data Structure

Let us look at the following example:

In the above binary tree, you can see the tree is not ordered, the Node 1 is the root node. The left arrow and the right arrow indicate the left and right child of each node respectively. Nodes present at the last level are the leaf nodes. Hence, the given binary tree has Nodes 1, 2, 3 as the parent nodes. Node 1 and Node 2 are the internal nodes as they have 2 children.

Operations on Binary Tree with Complexities:

  • Search: To search an element we need to traverse all nodes in the tree. With the assumption that we do Level Order Traversal for searching the time complexity to implement search is O(n) for n nodes in a tree.
  • Insert: If we consider a Skewed Binary Tree, then inserting an element would require traversing to the last node of the tree then insert it. So the overall complexity will be O(n).
  • Delete: For deleting a node we have to first search it in the tree then deallocate the memory. Thus, like the search operation, it also requires O(n) time.

Binary Search Tree

A Binary Search Tree (BST) is a special type of binary tree. It is a node based binary tree data structure where the nodes are arranged in a specific order. The nodes contain the same structure as in a binary tree but they differ in arrangements. It is an Ordered tree, which follows the given conditions:

  • The left child of a node contains value or data less than it’s parent node.
  • The right child of a node contains value or data greater than the parent node’s data.
  • Hence, the left subtrees contain nodes with values lesser than the root of the tree and the right subtrees contain nodes with values greater than root of the tree.
  • The left and the right subtrees of each node if they exist must also be a binary search tree and should follow the above rules. As each node’s data must be greater or lesser than it’s parent node so no nodes with Duplicate keys or values are allowed.

Below, a typical Binary Search Tree is shown:

 

In the above tree, you can see the tree is ordered. For every parent node, e.g. Node 7 (Root Node), it’s left child (Node 2) is smaller than it whereas it’s right child (Node 9) is greater than it. Each subtree of a node is itself  a binary search tree and follows the above rules.

Note: The In-Order traversal of the binary search tree will give the nodes in sorted order. The leftmost node has the smallest node value.

Operations on BST with Complexities:

The whole idea of using a BST or Binary Search Tree is to optimize the search operation for each lookup. If we search a Node in a BST we remove half sub-tree at every step because of it’s Ordered nature. For instance, consider a BST of n nodes, at each comparison we search for a node at either the left half or the right half. We reduce the search space to n/2 at each step until the element is found. The worst case for operations occurs in Skewed Binary Search Trees.

  • Search: Searching for an element in a binary search tree generally takes O(log n) time or O(h), h is the height of the tree. In the worst case, the time taken to search an element is O(n) if the given BST is Skewed BST.
  • Insert: The time is the same as search operation O(log n) or O(h) but in the worst case, it takes O(n) time.
  • Delete: Overall complexity to search and deallocate memory is same as above O(log n) but in worst case O(n) time.

Now Let us look at the key differences between Binary Tree and a BST:

Binary Tree vs Binary Search Tree

Binary Tree

Binary Search Tree

1. It is a Non-linear data structure, a specialized form of Tree data structure, where each node has a maximum of two child nodes. 1. It is a node based binary tree where each node has maximum of two children and the trees on the left half and right half of each node are itself a Binary Search Tree.
2. There is no Ordering of data while arranging nodes in a binary tree. 2. A BST is an Ordered tree, the value of the left child is smaller than it’s parent node and the value of a right child is greater than the value of it’s parent node. Hence, The same is followed for the subtrees.
3. It is useful in representing data in a Hierarchical format. 3. It is useful in representing data in an Ordered format.
4. Nodes with Duplicate values are allowed. 4. Duplicate nodes are not allowed.
5. The operations on a binary tree take a comparatively longer time. As a result the Search, Insert, and Delete operation takes O(n) time. 5. Binary Search Trees are sorted which provides fast search, insert and delete operation taking almost O(log n) time. Hence, for lookups we mainly use BST’s as all the keys are arranged in sorted order.
6. It acts as the base for implementing Full Binary Tree, BST’s, Perfect Binary Tree, etc. 6. It is used in implementing Balanced Binary Search Trees like AVL Trees, Red Black Trees, etc.

That’s it for the article we looked at them in detail description of each with their examples along with the major differences.

Feel free to ask your doubts or to leave suggestions in the comment section below.

The post Difference between Binary Tree and Binary Search Tree appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/3bUoPbK

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20