How to Build a Binary Search Tree with Given Data?

It should be 20 and 15.In summary, when data containing 30, 40, 50, 60, 10, 20, 55, 15 is inserted one by one, a Binary Search Tree (BST) is formed with 30 as the root. 40 is placed on the right subtree and 10 is placed on the left subtree. As the data is inserted, the rule that every node on the right subtree has to be larger than the current node and every node on the left subtree has to be smaller than the current node is followed. The final BST would look like the image provided.
  • #1
Henry R
25
0
Draw a Binary Search Trees (BST) produced when data containing 30,40,50,60,10,20,55,15 is inserted one by one.

Thanks.
 
Technology news on Phys.org
  • #2
Henry R said:
Draw a Binary Search Trees (BST) produced when data containing 30,40,50,60,10,20,55,15 is inserted one by one.

Thanks.

Note that every node on the right subtree has to be larger than the current node and every node on the left subtree has to be smaller than the current node.
 
  • #3
evinda said:
Note that every node on the right subtree has to be larger than the current node and every node on the left subtree has to be smaller than the current node.

This is ...View attachment 3676
 

Attachments

  • Binary tree.PNG
    Binary tree.PNG
    6.4 KB · Views: 61
  • #4
Henry R said:

Since the data is inserted one by one, $30$ has to be the root.
$40$ is greater than $30$, so it has to be at the right subtree.
So, at the beginning it should be like that:

View attachment 3677Can you continue? (Thinking)
 

Attachments

  • m.png
    m.png
    799 bytes · Views: 64
  • #5
evinda said:
Since the data is inserted one by one, $30$ has to be the root.
$40$ is greater than $30$, so it has to be at the right subtree.
So, at the beginning it should be like that:

https://www.physicsforums.com/attachments/3677Can you continue? (Thinking)

sure.View attachment 3679
 

Attachments

  • b.PNG
    b.PNG
    9.4 KB · Views: 59
  • #6
How is 55 smaller than 40?
 
  • #7
Bacterius said:
How is 55 smaller than 40?

Yeah! Sorry for that.
 

1. What is a Binary Search Tree (BST)?

A Binary Search Tree is a data structure in computer science that is used to store and organize data in a hierarchical manner. It is a type of binary tree where each node has at most two children, and the values in the left subtree are less than the value in the root node, while the values in the right subtree are greater than the value in the root node.

2. What are the advantages of using a Binary Search Tree?

One of the main advantages of using a Binary Search Tree is its efficient search and insertion operations. As the elements are organized in a specific order, it is easier and faster to locate a specific value or insert a new value into the tree. Additionally, BSTs can also be used for sorting data, as the values can be retrieved in a sorted order.

3. How is a Binary Search Tree different from a regular binary tree?

A Binary Search Tree differs from a regular binary tree in the way the elements are organized. In a BST, the values in the left subtree are smaller than the root node, while the values in the right subtree are larger. This organization allows for efficient search and insertion operations. In a regular binary tree, there is no specific order or rule for organizing the elements.

4. What is the time complexity of search and insertion in a Binary Search Tree?

The time complexity of search and insertion in a Binary Search Tree is O(log n), where n is the number of nodes in the tree. This is because the search and insertion operations only require traversing through the height of the tree, which is log n in a balanced BST. In the worst case, where the tree is unbalanced, the time complexity can be O(n).

5. Can a Binary Search Tree contain duplicate values?

Yes, a Binary Search Tree can contain duplicate values. However, it is common practice to not allow duplicate values in a BST, as it can affect the efficiency of search operations. If duplicate values are allowed, it is important to have a defined rule for how they will be organized in the tree.

Similar threads

  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top