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

Is there a built-in data structure in Python with BST semantics?

In other languages, there are built-in data structures with binary search tree semantics, for example C++'s std::map. Note that I am using C++ as an example, but not because I am attempting to draw a direct comparison between C++ itself and Python.…
OregonTrail
  • 8,594
  • 7
  • 43
  • 58
1
vote
0 answers

Why is pointer dropping value in C++?

Here's a simple binary search tree (BST) that I cooked up to illustrate my problem struct BST { int key; BST *left, *right; }; Here's a function that inserts a node into the BST: bool insert (BST *root, BST *node) { // base case. Do the…
babrar
  • 179
  • 2
  • 14
1
vote
1 answer

Why I am unable to get the binary tree level order traversal from following code?

I am trying to do a level order traversal of a binary search tree and return the final result in a multi-dimensional array. eg. if root node is 2 and nodes at level 1 are 1,4 then it should return [[2], [1,4]] as returnColumnSizes from the code. I…
Akshat
  • 45
  • 1
  • 9
1
vote
1 answer

why the content of the struct could be changed in the function

This is an operation to inert X into a BinTree, what confuses me is that why the BST->Left can not be changed in step①,however, it can be changed in step② or ster③,I think these three steps are all in the function, why there is a difference between…
陈润泽
  • 13
  • 4
1
vote
2 answers

Issues with implementing treeSort()

So, my doctor asking me to implement treeSort() and then test it on int[1000000] and calculate the time. I have class BSTree which contains the following methods: public void treeSort(E[] data) { inorder(data, new Process(),…
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1
vote
1 answer

Print all nodes including nodes java

I want to create a binary search tree tool in which it outputs all the nodes including the null In my current code, it only create nodes in the input array including its left and child then print Is it possible to print all nodes including in a null…
MrFapoh
  • 23
  • 5
1
vote
2 answers

BST(Binary Search Tree) Testdome in Python

I need solution about the problem which is posted in test-dome. Here is Problem Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node's left subtree and is smaller than…
1
vote
2 answers

Not getting correct path from root node to point

I am trying to find a path from the root node to a given point but ends up in getting an empty array.In the output I should get List of string of path id's from root to the given point. Please check below link for detailed description about…
1
vote
0 answers

Binary search tree with duplicate values and number of nodes greater/smaller than entered value?

I am trying to develop a Binary search tree that can store duplicate values. Please see the problem below. Sample Case: Below is my Code: #include #include long int N=0; //Total number of nodes present struct node { long…
1
vote
1 answer

What is the best data structure to perform the following queries and why am I getting wrong result in mine

I need to build a data structure that: learn x (insert x) forget x (deletes x). if x not present, do nothing decrease x n - decreases the count of x by n, if n >= count, then the node is simply deleted. and if x not present then do…
1
vote
0 answers

Why use morris traversal not recursion in binary tree or binary search tree?

I am learning data structures, i am wondering why not use recursion but morris traversal. Both of them have O(n) time complexity and O(1) extra space.
Abhi38
  • 307
  • 1
  • 2
  • 6
1
vote
1 answer

stack overflow exception while getting height of binary tree

I am trying to create a simple Binary Tree, then add several integers to the tree and finally print out the height of the tree. Although I seem to be getting a stack overflow exception when calling the BinaryTree height() method. Can anyone tell me…
craig2020
  • 321
  • 1
  • 6
  • 12
1
vote
0 answers

Recursive Binary Tree in swift

This algorithm correctly searches a tree and returns true if the searchValue is in the tree, I understand the base case, but I'm a little lost on the else statement, I'm not understanding what the || does in this scenario, is it returning both of…
tHatpart
  • 1,302
  • 3
  • 12
  • 27
1
vote
2 answers

Segmentation fault while inserting in Binary search tree

I am trying to build a basic Binary search tree in C++ and running into some problems, specifically when I try to insert a node via function and read, it gives me segmentation fault. But the same node struct works perfectly fine when I manually…
1
vote
1 answer

Under which circumstances can a Binary Search Tree and a Partially-Ordered Tree be equivalent?

This is my first question over here, and probably not the last. I'm currently working with different kinds of trees, specially binary search trees, but also some others like AVL or partially-ordered trees. I've been thinking about the possibility of…