binary-search-tree

How to Serialize Binary Tree

偶尔善良 提交于 2019-11-30 10:59:35
问题 I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index for the left child and 2*i + 1 for the right child. The interviewer seemed more or less pleased, but I'm wondering what serialize means exactly? Does it specifically pertain to flattening the tree for writing to disk, or would serializing a tree also include just turning the tree into a linked

How to delete a node with 2 children nodes in a binary search tree?

风流意气都作罢 提交于 2019-11-30 10:31:23
How to delete a node with 2 children nodes in a binary tree? Is there any kind of method to remove it? I googled it. But didn't get clear idea about it. Anybody explain it with diagrammatic representation? How to delete the node '5' from the this image and what might be the outcome? you replace said node with the left most child on its right side, or the right most child on its left side. You then delete the child from the bottom that it was replaced with. Deleting five could yield either a tree with 3 as its root or 18 as its root, depending on which direction you take. It looks like you got

Why storing data only in the leaf nodes of a balanced binary-search tree?

我与影子孤独终老i 提交于 2019-11-30 09:09:43
I have bought a nice little book about computational geometry. While reading it here and there, I often stumbled over the use of this special kind of binary search tree. These trees are balanced and should store the data only in the leaf nodes, whereas inner nodes should only store values to guide the search down to the leaves. The following image shows an example of this trees (where the leaves are rectangles and the inner nodes are circles). I have two questions: What is the advantage of not storing data in the inner nodes? For the purpose of learning, I would like to implement such a tree.

recursive delete on a binary tree

霸气de小男生 提交于 2019-11-30 04:40:14
问题 I am trying to understand how the recursive method of deletion of a binary search tree works. The code that I came across in many places looks as follows: void destroy_tree(struct node *leaf) { if( leaf != 0 ) { destroy_tree(leaf->left); destroy_tree(leaf->right); free( leaf ); } } I can't understand however a) how does it work if there are no returns in the routine? b) when free() gets to be called? I think about, e.g., such a tree: 10 / \ 6 14 / \ / \ 5 8 11 18 So my understanding is that I

What is the difference between std::set and std::map [duplicate]

主宰稳场 提交于 2019-11-30 02:56:38
问题 This question already has answers here : What's the difference between set<pair> and map in C++? (7 answers) Closed 5 years ago . I'm relatively new to c++ programming and was wondering if someone could help clarify a few questions for me. http://www.cplusplus.com/reference/set/set/ http://www.cplusplus.com/reference/map/map/ I've been reading on how to implement STL binary search trees and I keep noticing that std::set and std::map are constantly mentioned as the methods for accomplishing

Proof that the height of a balanced binary-search tree is log(n)

拜拜、爱过 提交于 2019-11-30 02:02:16
The binary-search algorithm takes log(n) time, because of the fact that the height of the tree (with n nodes) would be log(n). How would you prove this? Let's assume at first that the tree is complete - it has 2^N leaf nodes. We try to prove that you need N recursive steps for a binary search. With each recursion step you cut the number of candidate leaf nodes exactly by half (because our tree is complete). This means that after N halving operations there is exactly one candidate node left. As each recursion step in our binary search algorithm corresponds to exactly one height level the height

To find largest element smaller than K in a BST

╄→гoц情女王★ 提交于 2019-11-30 01:53:45
Given a binary search tree and an integer K, i would like to find the largest element less than K. In the below tree, for K = 13, result = 12 for K = 10, result = 8 for K = 1 (or) 2, result = -1 10 5 12 2 8 11 14 I tried the below logic. But is there any better way to do this ? int findNum(node* node, int K) { if(node == NULL) { return -1; } else if(K <= node->data) { return findNum(node->left,K); } else if(K > node->data) { int t = findNum(node->right,K); return t > node->data ? t : node->data; } return -1; } That's O(log n), which is the minimum. However, you can improve the efficiency

Given a BST and its root, print all sequences of nodes which give rise to the same bst

那年仲夏 提交于 2019-11-30 00:53:26
Given a BST, find all sequences of nodes starting from root that will essentially give the same binary search tree. Given a bst, say 3 / \ 1 5 the answer should be 3,1,5 and 3,5,1. another example 5 / \ 4 7 / / \ 1 6 10 the outputs will be 5,4,1,7,6,10 5,4,7,6,10,1 5,7,6,10,4,1 etc The invariant here however is that the parent's index must always be lesser than its children. I am having difficulty implementing it. Atul Vaibhav I assume you want a list of all sequences which will generate the same BST. In this answer, we will use Divide and Conquer . We will create a function findAllSequences

How to Serialize Binary Tree

不羁的心 提交于 2019-11-29 22:59:05
I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index for the left child and 2*i + 1 for the right child. The interviewer seemed more or less pleased, but I'm wondering what serialize means exactly? Does it specifically pertain to flattening the tree for writing to disk, or would serializing a tree also include just turning the tree into a linked list, say. Also, how would we go about flattening the tree into a (doubly) linked list, and then

Pre-order to post-order traversal

一曲冷凌霜 提交于 2019-11-29 21:35:46
If the pre-order traversal of a binary search tree is 6, 2, 1, 4, 3, 7, 10, 9, 11, how to get the post-order traversal? You are given the pre-order traversal of the tree, which is constructed by doing: output, traverse left, traverse right. As the post-order traversal comes from a BST, you can deduce the in-order traversal (traverse left, output, traverse right) from the post-order traversal by sorting the numbers. In your example, the in-order traversal is 1, 2, 3, 4, 6, 7, 9, 10, 11. From two traversals we can then construct the original tree. Let's use a simpler example for this: Pre-order: