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
6
votes
7 answers

Is O(logn) always a tree?

We always see operations on a (binary search) tree has O(logn) worst case running time because of the tree height is logn. I wonder if we are told that an algorithm has running time as a function of logn, e.g m + nlogn, can we conclude it must…
Martin08
  • 20,990
  • 22
  • 84
  • 93
6
votes
2 answers

How to use libavl?

I'm trying to use GNU libavl (http://adtinfo.org/) for one of my academic projects. I need a simple enough tutorial on how to use the BST(Binary search tree) implementation provided by the library. What I need to do is sort a (key,value) pair(about…
Madushan
  • 894
  • 1
  • 10
  • 22
6
votes
2 answers

Threaded Binary Search Trees Advantage

An explanation about Threaded Binary Search Trees (skip it if you know them): We know that in a binary search tree with n nodes, there are n+1 left and right pointers that contain null. In order to use that memory that contain null, we change the…
6
votes
2 answers

Problems about Binary Search Tree in Clojure (immutable structure)

I have run into problems in writing the code for delete a node from the tree. given a BST and a key value, find the key in the tree and delete it. so here is my thought, first, if BST is nil then return nil and if the BST have only one node the…
Landey
  • 63
  • 2
6
votes
1 answer

Binary trees and quicksort?

I have a homework assignment that reads as follows (don't flame/worry, I am not asking you to do my homework): Write a program that sorts a set of numbers by using the Quick Sort method using a binary search tree. The recommended implementation is…
user2664110
6
votes
7 answers

Flatten binary search to in order singly linked list [C]

I am trying to flatten a binary search tree to a singly linked list. Binary search tree: 6 / \ 4 8 / \ \ 1 5 11 / 10 Flattened Singly Linked List: 1 -> 4 -> 5 -> 6 -> 8 -> 10 -> 11 I can't seem to…
kyle
  • 2,563
  • 6
  • 22
  • 37
6
votes
2 answers

Binary Search Tree key/value pair - I know the value but not the key C++

I have a simple question that I am confused about. I know what is the concept of having a key/value pair in a Binary Search Tree and how the tree looks like when it is built. What I am unsure is of how to search for a value in such a BST if I have…
Faizan
  • 137
  • 1
  • 2
  • 8
6
votes
1 answer

Verifying whether a tree is bst or not Python

I have a practice interview question which tells me to verify if a tree is a balanced search tree or not and give a verification method... I have the class as Class Node: def __init__(self, k, val): self.key = k self.value = val …
koala421
  • 786
  • 3
  • 11
  • 27
6
votes
1 answer

when to use inorder, preorder and postorder traversal

I understand the code behind how to do inorder, preorder, and postorder traversal on a binary search tree. However, I'm confused about the application. When would you use each? It would be really helpful to illustrate cases of when each method of…
user1054740
  • 503
  • 2
  • 7
  • 13
6
votes
1 answer

A sequence that forms the same AVL and splay trees?

Is there such a sequence of numbers (1-7, all numbers used, only once each), that would form equal AVL and splay tree?
6
votes
2 answers

Software to draw Binary Search Tree or any other tree structures

I need to plot BSTs on the computer. Is there any software that does this job?
GJ.
  • 870
  • 5
  • 9
  • 26
6
votes
2 answers

Find the total depth of every node in a binary search tree recursively?

I have been at this problem for awhile now and I can't quite figure the logic. Say I have a binary tree that looks something like the following: 8 1 * 0 = 0 / \ 4 12 2 * 1 = 2 / \ /…
Andrew
  • 815
  • 8
  • 17
6
votes
1 answer

Binary Tree - Counting nodes on a level

I'm writing a binary tree class, and I'm stuck on a levelCount method, where I need to count the number of nodes on a level of the tree. The class and method look something like this: public class ConsTree extends BinaryTree { …
user1547050
  • 337
  • 2
  • 7
  • 15
6
votes
2 answers

How to construct Binary Search Tree bottom-up

Given a sorted array, it is very easy to visualize a BST from it in a top-down manner. For example, if the array is [1,2,3,4,5,6,7], we can see that the root will be the middle element, that is 4. To its left there will be a subtree whose root is…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
6
votes
3 answers

In a BST two nodes are randomly swapped we need to find those two nodes and swap them back

Given a binary search tree, in which any two nodes are swapped. So we need to find those two nodes and swap them back(we need to swap the nodes, not the data) I tried to do this by making an additional array which has the inorder traversal of the…
Aman Singhal
  • 835
  • 1
  • 11
  • 24