binary-search-tree

Convert Sorted Array to Binary Search Tree (Picturing the Recursion)

人走茶凉 提交于 2019-12-22 15:09:18
问题 I want to convert a sorted integer array into a binary search tree. I believe I understand how to do this. I have posted my pseudo-code below. What I cannot picture is how the recursion actually works. So if my array is 1, 2, 3, 4, 5... I first make 3 the root of my BST. Then I make 2 the left-child node of 3. Then do I make 1 the left-child node of 2, and come back to 2? I don't get how the recursion steps through the whole process... Thanks in advance, and apologies if this question is

How can a node X without right child have a successor?

纵然是瞬间 提交于 2019-12-22 12:34:22
问题 I have a trouble understanding what's a successor of a node X whenever it doesn't have a right child. From what I had understood, if a node X had no right child, then it would not have a successor. But my textbook says the following: If the right sub-tree of node X is empty and X has a successor Y ... How can X have a successor when it has not right child? 回答1: The successor is simply the next element in the ordered sequence; it doesn't necessarily have to be a child element. For example, the

Remove recursively from a binary search tree

自闭症网瘾萝莉.ら 提交于 2019-12-22 01:51:01
问题 This is homework; please don't just give me code I have two methods: remove(T data) and removeRec(Node<T> node, T data) . In its current state, it seems my code only removes the root node of the BST. @Override public T remove(T data) { if (data == null) { throw new IllegalArgumentException("Data is null"); } if (root == null) { throw new java.util.NoSuchElementException("BST is empty"); } else { size--; BSTNode<T> dummy = new BSTNode<T>(null); return removeRec(root, data, dummy).getData(); /

Remove method binary search tree

…衆ロ難τιáo~ 提交于 2019-12-21 19:11:12
问题 I am trying to implement a remove method for the BST structure that I have been working on. Here is the code with find, insert, and remove methods: public class BST { BSTNode root = new BSTNode("root"); public void insert(BSTNode root, String title){ if(root.title!=null){ if(title==root.title){ //return already in the catalog } else if(title.compareTo(root.title)<0){ if(root.leftChild==null){ root.leftChild = new BSTNode(title); } else{ insert(root.leftChild,title); } } else if(title

Recursive Binary Search Tree Insert

牧云@^-^@ 提交于 2019-12-21 13:01:22
问题 So this is my first java program, but I've done c++ for a few years. I wrote what I think should work, but in fact it does not. So I had a stipulation of having to write a method for this call: tree.insertNode(value); where value is an int. I wanted to write it recursively, for obvious reasons, so I had to do a work around: public void insertNode(int key) { Node temp = new Node(key); if(root == null) root = temp; else insertNode(temp); } public void insertNode(Node temp) { if(root == null)

Inserting an equal value element

倖福魔咒の 提交于 2019-12-21 06:48:49
问题 I am currently studying binary search trees, and I was wondering what do you do if you try to insert an element that is the same value as the root? Where does it go? 回答1: The definition of BST is that it is a ordered set, thus duplicates are not allowed to be inserted. This is usually due to more complex structures being built atop the BST. Depending on the desired behavior, you may want to throw an exception, error or silently ignore when duplicates are inserted. However, depending on your

Find the parent node of a node in binary search tree

给你一囗甜甜゛ 提交于 2019-12-21 05:46:34
问题 So I want to find the parent node of a Node in a binary tree. Suppose that I input 30,15,17,45,69,80,7 in the tree through a text file. The tree should be 30 15 45 7 17 69 80 And here is my code : Node* BST::searchforparentnode(Node* pRoot, int value) { if(pRoot->pleft == NULL && pRoot->pright == NULL) return NULL; if(pRoot->pleft->value == value || pRoot->pright->value == value) return pRoot; if(pRoot->value > value) return searchforparentnode(pRoot->pleft,value); if(pRoot->value < value)

Hash table - implementing with Binary Search Tree

☆樱花仙子☆ 提交于 2019-12-21 05:39:10
问题 From Cracking the Coding Interview , page 71: Alternatively, we can implement hash table with a BST. We can then guarantee an O(log n) lookup time, since we can keep the tree balanced. Additionally we may use less space, since a large array no longer needs to be allocated in the very beginning. I know the basics of linked lists, hash tables and BSTs, but I am unable to understand these lines. What does it actually mean? Would this final data structure would be a Trie? 回答1: The full text of

Algorithm- Sum of distances between every two nodes of a Binary Search Tree in O(n)?

大憨熊 提交于 2019-12-21 04:00:34
问题 The question is to find out sum of distances between every two nodes of BinarySearchTree given that every parent-child pair is separated by unit distance. It is to be calculated after every insertion. ex: ->first node is inserted.. (root) total sum=0; ->left and right node are inserted (root) / \ (left) (right) total sum = distance(root,left)+distance(root,right)+distance(left,right); = 1 + 1 + 2 = 4 and so on..... Solutions I came up with: Brute-force. Steps: perform a DFS and track all the

Inserting an element in Binary Tree

我只是一个虾纸丫 提交于 2019-12-21 03:58:28
问题 Tried exploring a lot over the net, but could get any help, Everywhere its like adding a node to the Binary Search tree. Question: Requesting for algorithm and code snippet for adding a node to the Binary tree . ( or point me to correct URL ) Assumption: As per my understanding, Binary Tree and Binary Search Tree is different? Correct me if I am wrong. ( request: if you are writing your code snippet please use proper variable name, that helps in understanding ) Eg: Binary Tree 5 7 3 x1 x2 x3