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
2 answers

How can I sum all nodes under a given value in binary search tree?

My homework needs me to sum all numbers under the given value in a BST. However, I had no idea how to do it. Appreciate for any help. class BinarySearchTree: def __init__(self, data): self.data = data self.left = None …
Victoriavv
  • 93
  • 1
  • 9
1
vote
4 answers

Easiest to implement online sorted data structure in C

I'm scanning a large data source, currently about 8 million entries, extracting on string per entry, which I want in alphabetical order. Currenlty I put them in an array then sort an index to them using qsort() which works fine. But out of curiosity…
hippietrail
  • 15,848
  • 18
  • 99
  • 158
1
vote
2 answers

How do i write a function in JavaScript that compares two trees defined by TreeNodes a and b?

I'm trying to write a JavaScript function that compares two binary trees defined by TreeNodes a and b and returns true if they are equal in structure and in value and false otherwise. for example example of comparing both values and structure of two…
1
vote
2 answers

Writing a generic traverse function that allows the flexibility of dealing with multiple functions with differing parameters

I want to employ std::function to help me run a generic traverse function that traverses a BST and calls the parametrized function. My difficulty is that the parametrized function varies in its own parameters. So, for example, I was to generalize…
1
vote
3 answers

How to create a method that gets the previous node using a Binary Search Tree in Java?

I am working on a method that gets the previous node using a binary search tree. Now I think I got this, however I am struggling with my if statements. The instructions are the getPrevNode(BSTNode) method should find the node in the tree that comes…
Adan Vivero
  • 422
  • 12
  • 36
1
vote
1 answer

How to create an assignFirst method with Nodes in a BinarySearchTree?

I have a binarySearch Tree and I want to create a method assignFirst. This method should find the node in the tree with the smallest value and update the tree's "first" attribute accordingly. I have a lot of methods, but I don't want to include all…
Adan Vivero
  • 422
  • 12
  • 36
1
vote
0 answers

Finding index of the maximum value in a cyclic sorted array

I have to write a function which returns the index of the maximum value in a cyclic sorted array, without duplicates in O(logn). I have already tried this code and it does work, but for some reason, I get an error when the array has 2 or 6 elements.…
1
vote
0 answers

Understanding thread-safety

Let use as example standard Binary Search Tree, its nodes having an encapsulated value and pointers to the left and right children and its parent. Its primary methods are addNode(), removeNode() and searchNode(). I want to understand how to make my…
Asghabard
  • 191
  • 13
1
vote
0 answers

Valgrind reports memory leak in insert to bst function

As part of an assignment, I've created a set of utilities for working with binary search trees. Problem is, my binary tree insertion function seems to be leaking, according to Valgrind. I don't really see where the problem could be, as I compared my…
1
vote
1 answer

Desearilize binary search tree

I'm practicing for upcoming interviews and I'm trying to deserialise a binary search tree. I got it to work for serialising but I'm getting an argument number error but I don't see why. I expect "5 2 1 # # # 35 18 7 # # 30 # # #" to be turned back…
1
vote
2 answers

Throwing EmptyTreeException is not working

I'm currently working on a project where I have to make a binary search tree and I'm having a little problem implementing my getRootData method I've already tried importing different things or trying to find an implemented method but I haven't found…
1
vote
1 answer

how to convert a given level of a binary search tree into a linked chain?

I have to do a function where, given an int n and a binary search tree, i have to convert the level n of the bst int a linked list. for example if given the numer 2 and this tree 2 / \ 5 3 i have to make a liked list with 5 -…
iamuma
  • 11
  • 3
1
vote
2 answers

Static member in C

I was trying to write a code to determine if a tree is a BST. And I searched the solution from website for reference. One of the solutions is as follows: I really don't get how static pointer serves in this function. Can anyone explain to me? Thank…
Liu
  • 413
  • 4
  • 11
1
vote
1 answer

Binary search tree insertion data problem

i'm trying to implement my own binary search tree, i've stucked on inserting data, can you explain me what i'm doing wrong . void tree::add(int data) { tree * tmp = new tree; if (root == NULL) { root = tmp; root->data =…
Rooster
  • 13
  • 3
1
vote
0 answers

How to find lowest common node between two BST?

I need to find the lowest common node in two BST. I'm having trouble finding out how to do it. I should use recursion, not storing values in arrays. I tried to implement inorder search. But it's not working as it returns nothing. Here's my insert…
akemi
  • 21
  • 2
1 2 3
99
100