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

Binary Tree Get All Left / Right Nodes

Here's my database structure id | parent_id | depth | position --------------------------------- 2 | 1 | 1 | left --------------------------------- 3 | 1 | 1 | right --------------------------------- 4 | 2 | 2 …
Karl Wong
  • 594
  • 2
  • 7
  • 23
1
vote
1 answer

Hash Table vs Binary Search Tree, Big O Access and Search

I'm looking at this Big O cheat sheet: https://www.bigocheatsheet.com/ I don't understand the following when looking at Hash Tables and Binary Search Trees. Hash Tables: Access: N/A Binary Search Tree: Access: O(log N) Why is the Access operation of…
1
vote
1 answer

Binary search tree implementation. Seems to fail at adding new values and searching for existing ones

Generic implementation in C#. A very quick example, so obviously the code can be improved quite a bit, however at the moment I want to get it to work in its current form. Adding new nodes, or deleting existing ones, does not work. When the print()…
Al2110
  • 566
  • 9
  • 25
1
vote
2 answers

How do I insert a new node into a binary tree in C?

I've been trying to get this to work for a while now, but there is clearly something I don't understand. I have to insert a new node into a binary tree with "phone" as it's value. void bst_insert_node(bstree* bst, unsigned long phone, char *name) { …
Kilzimir
  • 33
  • 1
  • 5
1
vote
0 answers

Avoid collision of nodes while representing Binary Tree in SFML

following is the code of the prgoram I have created in SFML (Please note, this is a Binary SEARCH Tree and also that I have only given the codes of Display and Insertion as others aren't required, so that to keep the code's length as small as…
Hassan Ashas
  • 530
  • 1
  • 5
  • 14
1
vote
4 answers

Split string into two variables in C

I have been givin an assignment which uses C to read a given file and input data into a binary tree. My current problem is splitting the line read from the file into two different variables. The file that has been given contains two bits of data, an…
Polecalex
  • 323
  • 2
  • 12
1
vote
1 answer

Find keys closest to an integer argument in Haskell tree

There is a plenty of solutions how to find closest lower and upper keys in binary tree in imperative languages, but a lack of same questions for doing it in purely functional style like that of Haskell. I'm curious to learn how it's possible to walk…
1
vote
1 answer

Error on checking if a tree is a Binary Search Tree

I am currently trying to check if a tree is a BST, while keeping notice of the fact that the values must not be equal to any other one in the tree. I tried keeping count of the interval on which each value should be ( considering a min and a max as…
1
vote
1 answer

Zipping tuples inside Haskell tree

I'm seeking any suggestion how to solve a problem with a binary search tree in Haskell. It has declaration as follows: data TreeMap v = Leaf | Node { pair::(Integer, v), l::TreeMap v, r::TreeMap v} deriving (Show, Read, Eq, Ord) Now I want to zip…
1
vote
1 answer

Traversing binary search tree, how to advoid code duplication

I have been working on this small program. It reads data(members) from a file and stores it in a binary search tree. There are two features, print all the members and print all members but only with the same boat class. To achieve this it traverses…
1
vote
1 answer

Why does my Python recursive function return "none" instead of "True"?

I have created a search function which is supposed to find a value in a binary tree and return True if the value is found and False if it isn't. The tree is correct, I've tested. The different cases work when I print a string "Found" or "Not found".…
Killkenny
  • 11
  • 2
1
vote
0 answers

String Compare Not Returning Correct results

I am trying to compare two strings. One string will come from a variable which the user inputs. The other string is accessed from a variable in a BST. I have tried to use strcmp and compare. I have some output that lists each variable to show…
1
vote
2 answers

How to initialize value to function whose return type is boolean?

I want to initialize true for checkForBST(node* rootptr) function. What should i do? I know variable initialization but I always get confused in function initialization. Below is my checkForBST(node* rootptr) function: bool checkForBST(node*…
1
vote
1 answer

BST Construction with multiple choices

I encountered the following question: The Height of a BST constructed from the following list of values 10 , 5 , 4 , 3 , 2 , 1 , 0 , 9 , 13 , 11 , 12 , 16 , 20 , 30 , 40 , 14 will be: A) 5 B) 6 C) 7 D) 8 Now to certain point I can construct the…
Miriam Arbaji
  • 319
  • 1
  • 3
  • 17
1
vote
0 answers

How to remove a range of Binary Search Tree nodes that uses generic nodes

I have a BST that uses Generics loaded in from a local file containing all the counties, their state and the population of those counties. my compareTo() is running off of the Population values. How can I create a method calling a min/max that…