Questions tagged [binary-search-tree]

A binary search tree is a data structure which consists of a root node with left and right child nodes. The left node and all of its descendants have smaller values than the root node, while the right node and all of its descendants have larger values than the root node. The children of the root node follow this same pattern. This gives us a tree consisting of ordered elements.

In computer science, a binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure where each node has a comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that node's right sub-tree. Each node has no more than two child nodes. Each child must either be a leaf node or the root of another binary search tree. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node. BSTs are also dynamic data structures, and the size of a BST is only limited by the amount of free memory in the operating system. The main advantage of binary search trees is that it remains ordered, which provides quicker search times than many other data structures. The common properties of binary search trees are as follows:

  1. The left subtree of a node contains only nodes with keys less than the node's key.

  2. The right subtree of a node contains only nodes with keys greater than the node's key.

  3. The left and right subtree each must also be a binary search tree.

  4. Each node can have up to two successor nodes.

  5. There must be no duplicate nodes.

  6. A unique path exists from the root to every other node.

Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.

6319 questions
7
votes
4 answers

Binary search tree over AVL tree

As far as I know the time complexity between AVL trees and Binary Search Trees are the same in average case, with AVLs beating BSTs in worst case scenarios. This gives me a hint that AVLs are always superior than BSTs in every possible way to…
7
votes
2 answers

Possible permutations of BST's input

I am given a string, i.e. "CPHBDZ". By inserting (in this order) letters to a BST I will have: C / \ B P / \ H Z / D If we change order of the string to "CBPHDZ" we will get identical tree. And I have to find and list all permutations…
7
votes
2 answers

Deletion in Left Leaning Red Black Trees

I am learning about Left Leaning Red Black Trees. In the deletion algorithm peresented in the paper, if the key matches for a node and the right subtree is NULL for that node, then that node is deleted. But there may be a left subtree as well which…
7
votes
1 answer

Printing Level Order Binary Search Tree Formatting

I have implemented the following code to print a binary search tree in level order. public void printLevelOrder(int depth) { for (int i = 1; i <= depth; i++) { printLevel(root, i); } } public void printLevel(BinaryNode t,…
ILostMySpoon
  • 2,399
  • 2
  • 19
  • 25
7
votes
14 answers

converting a binary search tree to doubly linked list

This question was asked in a recent coding interview. Q : Given a binary tree, write a program to convert it to a doubly linked list. The nodes in the doubly linked list are arranged in a sequence formed by a zig-zag level order traversal My…
akash
  • 1,801
  • 7
  • 24
  • 42
6
votes
6 answers

Recursive insertion of BST

I have made a function for insertion in BST using loops and it is working perfectly fine. Now, when iam writing to do it using recursion i don't know why it's not working properly, however the logic is correct according to me. It seems that no…
Zohaib
  • 363
  • 2
  • 4
  • 15
6
votes
3 answers

Find swapped nodes in a BST

I am trying to write a program that can detect and print two nodes in BST that have been swapped. In a three level tree, I reached near to the solution using this approach. If (!AllSubTreeAreValid()) { //Nodes swapped on same side of main root…
Cipher
  • 5,894
  • 22
  • 76
  • 112
6
votes
4 answers

How do I remember the root of a binary search tree in Haskell

I am new to Functional programming. The challenge I have is regarding the mental map of how a binary search tree works in Haskell. In other programs (C,C++) we have something called root. We store it in a variable. We insert elements into it and do…
6
votes
4 answers

Returning Array from Recursive Binary Tree Search

Hi I've made a simple Binary Tree and added a pre-order traversal method. After throwing around some ideas I got stuck on finding a way to return each value from the traverse_pre() method in an array. class BST: def __init__(self, val): …
6
votes
2 answers

Binary tree implementation in C question as found in K&R

So I've been reading through the K&R C book and have a question.. in the 6th Chapter on structs on page 140-141, there is code that looks like this (I took out some of the more irrelevant parts) /* the program loops through a tree looking for some…
adelbertc
  • 7,270
  • 11
  • 47
  • 70
6
votes
8 answers

How to solve codility minMaxDivision

I have been trying to wrap my head around this codility question for 1H30,and how to solve with binary search. I found the answer but I cant understand the logic behind it. Can someone who gets it kindly walk me through this answer. This is the…
bihire boris
  • 1,530
  • 3
  • 19
  • 44
6
votes
3 answers

searching a binary search tree for parents efficiently

I'm trying to solve the following problem: at first we have a bst with root 0 and nothing else. not we add n given numbers like a which: not for instance we start to add n = 7 numbers to the tree: 19 3 5 25 21 -4 2 after adding all the numbers,the…
Farzin Nasiri
  • 710
  • 1
  • 10
  • 27
6
votes
7 answers

binary tree -print the elements according to the level

This question was asked to me in an interview: lets say we have above binary tree,how can i produce an output like below 2 7 5 2 6 9 5 11 4 i answered like may be we can have a level count variable and print all the elements sequentially by …
Vijay
  • 65,327
  • 90
  • 227
  • 319
6
votes
3 answers

Creating a List from a Binary Search Tree

I'm trying to make a list of all items in a binary search tree. I understand the recursion but I don't know how to make it return each value and then append it into a list. I want to create a function called makeList() that will return a list of all…
chrisheinze
  • 1,129
  • 2
  • 9
  • 14
6
votes
4 answers

Issue checking if binary tree is also binary search tree

I'm trying to solve this problem but I'm having some troubles: In a binary search tree (BST): The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is…
Héctor
  • 24,444
  • 35
  • 132
  • 243