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 print PostOrder and PreOrder by using TreeNode?

So I been working on this TreeNode program and I couldn't figure out how to print the PostOrder and PreOrder. I got the inOrder correct, but couldn't figure out the rest of the code for PreOrder and PostOrder. Here my code for inOrder for TreeNode: …
Paco
  • 7
  • 5
1
vote
1 answer

Why is this code for deleting a Node in BST instead of deleting the node making it 0

void Delete(Node* &root,int data){ if(root == NULL) return; if(root -> key > data) Delete(root->left,data); else if(root -> key < data) Delete(root->right,data); else{ // key found ; …
rv7
  • 37
  • 5
1
vote
1 answer

Search function for a binary tree isn't returning the found node

This is what I'm inserting into my tree: I'm searching for: Node nd = searchNodeIterativly(root, "Ortiz"); and I'm getting a null pointer error. Since "Ortiz" is actually in the tree, I don't see why my return in the loop isn't working. Is it the…
1
vote
1 answer

Binary Search Tree algorithm which returns an array of value within a range

Write a recursive range function that given a binary search tree, two integers k1 and k2 such that k1 ≤ k2 and a vector v, inserts in v all the keys in ascending order organized in the tree such that k1 ≤ k ≤ k2 . The function returns the size of…
ddon-90
  • 2,816
  • 1
  • 20
  • 29
1
vote
1 answer

How to find the immediate larger element in a BST given a specific node?

I'm using a BST. Given a specific node, how do I find the immediate larger element in the tree?
1
vote
1 answer

Dual recursion across kd-trees to find the closest approach between two sets of points

I have constructed kd-trees for two sets of points, in order to find the closest bichromatic pairing between the two sets: The kd-trees are stored as python dictionaries, which can be found in the code below, and are passed to a function…
1
vote
2 answers

pygraphviz: finding the max rank node using successors

I'm trying to find the max rank node and the depth. Here is my code. import pygraphviz as pgv class Test: def __init__(self): self.G = pgv.AGraph(directed=True) self.G.add_node('a') self.G.add_node('b') …
jason
  • 3,811
  • 18
  • 92
  • 147
1
vote
2 answers

Why is the output going into infinity

This is the code i am using to add in the BST.And this is the preorder function.Why is it going into infinity?I am taking the input from the user about the element to add.But the output goes into infinity. void add(){ struct node*…
1
vote
2 answers

Find minimum height on Binary Search Tree - how to trace second-to-last update of height

Below is the code that I'm using to find the minimum height of a Binary Search Tree (BST). Original source is here. The definition of minimum height used here is the distance from the root node to the first leaf node that does not contain two…
akb10
  • 83
  • 10
1
vote
1 answer

Why my parameter/object is being shown as NoneType object?

I am teaching myself binary search tree and in this program i am inserting data in the tree but error 'NoneType' object has no attribute 'data' error is occuring. from collections import deque class Node: def __init__(self, data): …
Mayank Pant
  • 145
  • 1
  • 8
1
vote
0 answers

Exception in thread "main" java.lang.NullPointerException when trying to create a binary tree and how can I fix it?

Can someone help me find the issue? I am trying to assemble a binary search tree from a list I am new to all of this sorry It trys to assemble A node list then makes a queue from the output and uses the queue to assemble the tree Here is the…
1
vote
0 answers

Is there a way to serialize a tree without using extra identifier for null?

Recently I attended an interview and I was asked to serialize a given tree. As I was about to start, the interviewer asked about the approach to take. I replied, it's going to be preorder with some special char for null. He said, that will waste a…
1
vote
2 answers

Binary search tree insertion program is showing segmentation fault

The node insertion code is throwing segmentation fault. This code is throwing segmentation fault when i am trying to print the data stored in the root node. Below is the implementation of the insertion program for the Binary Search Tree. This…
1
vote
1 answer

Double-Threaded Binary Search Tree - Saving Parent & Grandparent Nodes in Order to Set Threads

For an assignment for school, I need to take a binary search tree and convert it into a double-threaded binary search tree. I have a general understanding on how the threads work, but I realize that in order to know where it exactly the threads are,…
1
vote
2 answers

insert on first BST giving end of no-void function errror?

So I am attempting to learn how to code my first BST, and it is hard.... I am already having trouble with just a few lines of codes. the problem is in the insert, but I have included everything so that I could get some feedback on my style/other…
kingcong3
  • 193
  • 1
  • 2
  • 14