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
1 answer

How to implement Inorder Traversal in a Binary Tree in Golang

I am trying to implement a simple binary tree in Golang in order to understand concepts being taught in class. I'm a bit new to Golang but at the same time, I'm having a hard time wrapping my head around the concepts of recursion and where to insert…
Ikechukwu Anude
  • 346
  • 1
  • 4
  • 16
1
vote
4 answers

getting segmentation fault in searching an element in binary search tree in c++

node ** BST :: searchElement(node **tree, int item) { if( ((*tree)->data == item) || ( (*tree) == NULL) ) return tree; else if( item < (*tree)->data) return searchElement( &(*tree)->left, item); else return…
user710130
  • 11
  • 4
1
vote
0 answers

Why are my AVL balance functions losing data?

Whenever the number of nodes in the AVL pass 8 nodes and the algorithm tries to re-balance, on inserting the 9th node, most of the nodes in the tree are lost I implemented an AVL tree in c and the insert function checks the trees balance after…
1
vote
3 answers

Dictionary implementation (Balance Binary Search tree v.s. hash table)

Under what circumstances would it be better to implement a Dictionary ADT using a balanced binary search tree rather than a hash table? My assumption was that it is always better to use a binary search tree because of its natural ordering. But it's…
Glara
  • 11
  • 1
  • 2
1
vote
1 answer

What are some good-enough ways I can insert an object (Packet) into a binary search tree?

I'm trying to insert a Packet object into this binary search tree. But the problem is, I don't really know of a good-enough way of doing this or how to go about doing it. I'm looking for some pointers in the right direction and to be shown what to…
1
vote
2 answers

Getting NullPointerException whille inserting element in BST

I am trying to insert element in Binary Search Tree with iterative method but i am getting NullPointerException and i am not able to figure out why this error is getting. I tried changing the loop and checking the temp but i didnt get what is…
1
vote
1 answer

How do I write this algorithm to search for the next question in a question tree?

I've been struggling to write the correct algorithm for this. So I have a tree of questions that looks like this: [ { question: "Question 1", answers: { "yes": [ { question: "Question 1a", answers: ["A",…
Evert
  • 2,022
  • 1
  • 20
  • 29
1
vote
0 answers

How to add value to root without using a temp node?

I am trying to create a function for adding a node to BST. The code for insert function is as below. void insert(struct node **root,int data){ if(!(*root)){ struct node *temp=(struct node*)malloc(sizeof(struct node)); …
1
vote
1 answer

Binary Search Tree (BST) search method for string

i've built a BST that have elements of (country code(string), indicator code(string), indicator name(string) and ArrayList of Years(int) and values(string)). I'm trying to figure out how to prompt the user to search by entering Indicator code and…
id frn
  • 73
  • 7
1
vote
2 answers

Vector error,a very confusing segmentation error?

So basically,I am doing a code which searches for an element of a vector inside a vector.While I thought of the approach , implementing it got me a segmentation error. I narrowed down the problem In the code if I decomment the line in the for loop…
user34096
  • 15
  • 5
1
vote
1 answer

Binary Search Tree Problem

Why the search and successor and predecessor returns -1? // BST.cpp : main project file. #include "stdafx.h" #include #include #define SIZE 10 using namespace std; struct Node { int value; …
Stanley321
  • 21
  • 1
  • 3
1
vote
2 answers

What is the most efficient way to implement a BST in such a way the find(value) function is optimized for random values in the tree on x86?

Suppose we have a binary search tree of depth $n$, where every level is full, we know in advance how big the tree will be, and we will run the find(value) function a lot. The values(integers) we will want to find will be uniformly random across the…
1
vote
1 answer

How to distinguish between two BSTs

Here are the combinations of different BSTs with same element, the arrangement looks different depending on the sequence of addition of nodes to the tree structure: 1 1 1 1 1 \ \ …
Gaurav
  • 1,570
  • 5
  • 17
1
vote
1 answer

How to make this Binary serach tree insert method work

I am implementing different methods in a binary search tree and am stuck on the insert method as it just doesn't seem to work. I have been trying to implement the insert method for a while now but nothing seems to work it's always returning null.…
Anmol
  • 11
  • 2
1
vote
1 answer

Find maximum subtree in the given BST such that it has no duplicates

Given the BST which allows duplicates as separate vertices, how do I find the highest subtree such that it has no duplicates. This is the idea: (1) Check if the root value appears in its right subtree (inserting this way: left < root <= right). If…
1 2 3
99
100