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
1
vote
4 answers

help with insert on first BST

EDIT there a small thing that I am missing!! the error is still there So I am attempting to learn how to code my first BST, and it is hard.... I am already having trouble with just a few lines of codes. the problem is in the insert, but I have…
kingcong3
  • 193
  • 1
  • 2
  • 14
1
vote
0 answers

c++ maps with multiple key comparisons

I have a paired key map named mymap and I have defined a comparison operator named cmplt_cmprsn shown below #include #include struct prtl_cmprsn { bool operator()( const std::pair &left, const…
linuxfreebird
  • 799
  • 2
  • 8
  • 16
1
vote
1 answer

An alternative method to create an AVL tree from a sorted array in O(n) time

I need some help in this data structure homework problem. I was requested to write an algorithm that creates an AVL tree from a sorted array in O(n) time. I read this solution method: Creating a Binary Search Tree from a sorted array They do it…
PhysicsPrincess
  • 342
  • 1
  • 10
1
vote
1 answer

Is there any way to improve my py3 code for a binary search tree question?

I am working on a python3 question about BST from https://www.testdome.com/questions/python/binary-search-tree/24973?visibility=1&skillId=9: Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in…
ywan
  • 107
  • 7
1
vote
2 answers

PHP5 adding 1 HR to blank time variables/calculations

I've recently upgraded to PHP5, and have noticied that within an application I've built there seems to be an extra hour added to some of my variables and caculations. I am using: date_default_timezone_set('Europe/London'); which I understand means…
James P
  • 47
  • 1
  • 5
1
vote
0 answers

Converting to a Double Threaded BST

I'm trying to make this program work with a double threaded BST, instead of a normals Binary Search Tree I have tried to look at other implementations both double and single to see how they are performed, but it still isn't clicking BSTNode*…
hdano99
  • 21
  • 3
1
vote
4 answers

Intersection of 2 binary search trees

Hey, So I want to create a new tree which is basically the intersection (mathematical definition of intersection) of 2 given binary search trees. I have a method that prints out all the nodes at a particular level of the tree and I have a method…
dawnoflife
  • 1,572
  • 10
  • 42
  • 62
1
vote
0 answers

Result is NoneType even after updating the Node using insertNode (BST)

Result is initialized as None. Result is updated with insertNode. Since Result is None, insertNode should have updated Result with the argument Node. But I'm getting None even after insertNode method call. Trying to Merge two…
1
vote
1 answer

How do I fix my object initialization for a template BST class?

This problem occurs in my main.cpp: using namespace std; #include #include "BST.h" #include "Packet.h" int main() { BST test; // It occurs on this line! Packet one(1, "testPacket", 1, 1); system("Pause"); } The error on…
1
vote
0 answers

How to correctly remove node from Binary Search Tree C++

Ive been trying to delete specific values from my tree but instead of deleting them it sets them to 0. My implementation seems to be pretty standard compared to the other binary search trees on the web. All my other functions work fine except for my…
jasonv94
  • 11
  • 1
1
vote
1 answer

Delete smallest element in a BST (translate C to Lisp)

I've been trying for more than an hour to translate the following C code to Lisp to fix the bst-remove function in the BST code in Paul Graham's book ANSI Common Lisp (which as explained in the errata to that book, is broken) and I am completely…
mwal
  • 2,803
  • 26
  • 34
1
vote
1 answer

how can i implement generic BST?

i want to create a generic bst , every node has key and value,the methods and will work by the key. do i have to extends comparable in the Tree class? or only in the node class? i tried to figure out if i have to extends comparable by the node,or…
Agar123
  • 95
  • 7
1
vote
1 answer

Segmentation fault in a Binary Tree

Either I've been staring at this code for way too long or I just can't figure this one out. but when I use an 8000 number text file in descending order; 8000, 7999, ... I get a segmentation fault in the height function. If someone could take a…
tpar44
  • 1,431
  • 4
  • 22
  • 35
1
vote
1 answer

Is there a method to splt a Map based on the FIRST node to satisfy a predicate?

There is a function called Map.partition that splits the map into 2 maps with one containing ALL elements that satisfy the predicate. The predicate takes a key and a value as arguments and examines each element of the map to determine which result…
Chechy Levas
  • 2,206
  • 1
  • 13
  • 28
1
vote
1 answer

Trim Binary Search Tree Using Recursion

TRIM BST Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the…