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

questions about a binary search tree (how to compute the followers)

Given a list of strings, we can use an FBST to track which strings follow which other strings in the list. Create a function compute-followers that consumes los, and produces a FBST. Every string in los should appear as a key exactly once in the…
1
vote
1 answer

Why is splay tree in textbook different than mine?

In Data Structures and Algorithm Analysis in C++ (4th Edition) by Mark Allen Weiss, on page 162 at figure 4.50, the book describes how splaying the left most child of tree with only left children would ultimately look like. Where I am confused is…
1
vote
2 answers

BST with connection to parent - loop

I have problem with infinite loop in getMinimal() method. It works in this way : 1)Take node, 2)If node has other node on the left - go to other one. 3)Repeat as far as node has sth on the left side 4)Return the minimal node. But sometimes it works…
user10573717
1
vote
1 answer

Stack overflow error when i try to assign a new pointer to a structure

When I call the function newNode an exception is thrown and says stack overflow, I checked the parameters of the node in it says that they can't be read. struct node { int data; struct node*…
1
vote
1 answer

Counting number of nodes in Binary Search Tree C++

I am struggling to understand why the function CountNodes() below counts all the nodes in a BST. If we assume we have the following BST: 20 / \ 10 30 / \ / \ 5 15 25 35 If I call…
Reno
  • 1,039
  • 3
  • 14
  • 22
1
vote
2 answers

Problem with Binary Search tree code for insert operation

I am unable to access the left,right nodes from my root node in my insert function as evidenced by the terminal output. What is causing the error. Code Body: #include class Node { public: int key; int data; Node * left; …
1
vote
1 answer

Issues with comparing two generic objects

My project is a Phonebook that is using BSTree. Each node of the tree is BTNode. In the main class, I replace E with Pair class, which has (String name, String number), when I define the nodes. I have the following comparator class to compare…
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1
vote
0 answers

WPL tree algorithm implementation

I have read the article on WPL tree and became interested. Googling the the algorithm didn't returned any result. Does anyone have the idea how to implement this algorithm?
1
vote
3 answers

Unable to print complete Binary Search Tree because my logic to iterate backwards from the lowest Node is flawed

I have a program which is using for-loop to print all the elements in the BST in-order. I know that in order to do so, I need to print the left-Node, Parent Node, and then the right node. I can make such programs work when there is no for-loop…
user4252523
  • 69
  • 2
  • 8
1
vote
0 answers

Need code to perform binary search tree with graphics on java - insertion and deletion

I need to perform binary search tree operations - insert, delete, search on java with graphics. The UI must look like this: https://i.stack.imgur.com/o4yyU.png I've written the code for insertion and have made the UI. But I feel the code I've…
lalala123
  • 83
  • 6
1
vote
1 answer

No return from the Deletion function of Binary Search Tree

I tried to define a delete function for a BST, and have run into some trouble. There are insert, find_max, search, preorder, and delete functions in this Solution class. I expected to delete elements by using these function and test the result by…
Yin
  • 23
  • 1
  • 5
1
vote
1 answer

Program in to generate chained list in python

I am developing a program in python and as part of it I have to link all the lists that have an element in common in a certain position, that is, there is an input element and an output element and I want to gather all those that follow the chain.…
1
vote
1 answer

Calculating height of a BST returning -1 confusion?

Why are we returning -1 whenthere is no node or nullptr? I cant figure out its logic and how will it cancel with the +1 int height( BinaryNode * node ) const { if ( node == nullptr ) return -1; else return 1 + std::max(…
1
vote
0 answers

Error C2447 '{': missing function header (old-style formal list?)

When I try to compile this code, I get these errors from the Visual Studio compiler: Error C2447 '{': missing function header (old-style formal list?) Binary Sarch Tree 127 Error C2061 syntax error: identifier 'Node' Binary Sarch Tree …
1
vote
3 answers

How do I switch my remove method in a BST from being recursive to being iterative?

I am wondering how I can switch my remove method from being recursive to being iterative. My recursive method is working perfectly fine, but all my attempts at making it iterative are not. Where am I going wrong and how can I fix it? So here's my…
A aqua
  • 15
  • 1
  • 7