data-structures

Confused - Height of Binary tree

ⅰ亾dé卋堺 提交于 2021-02-20 06:47:11
问题 I'm somewhat confused between the logic of calculating the height of binary tree. Code 1 public static int findHeight(Tree node) { if(node == null) return 0; else { return 1+Math.max(findHeight(node.left), findHeight(node.right)); } } Code 2 public static int findHeight(Tree node) { if(node == null) return -1; else { return 1+Math.max(findHeight(node.left), findHeight(node.right)); } } I think, the second one is correct, since it gives the correct answer for below code :- Tree t4 = new Tree(4

How to add a function that can compare two tree structure in java?

爱⌒轻易说出口 提交于 2021-02-20 05:28:08
问题 I have created a tree data structure, but I am unable to add two methods that can get me those nodes which are different and those nodes which are same. The ** ones are those which are similar nodes and the * ones are different nodes. Tree Structure 1 >23720634 (Root) **24751368** **24751324** **24751324** **23726962** **24751382** **23728776** **23724832** **23727632** **23728875** **23728813** *23722966* **24751324** **17712706** Tree Structure 2 >25764379 (Root) **24751368** **24751324** *

Techniques used in checking if a binary tree is symmetric

我只是一个虾纸丫 提交于 2021-02-20 04:28:33
问题 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Question link is here The recursion method need to traverse the tree twice. But one of the comment provided a solution used a technique called 'Null check'. I can't understand why in this way can we avoid checking the tree twice? Here is his code in C++: bool isSymmetric(TreeNode* root) { if (!root) return true; return isSymmetric(root->left, root->right); } bool isSymmetric(TreeNode* t1, TreeNode*

Word Frequency Statistics in C (not C++)

末鹿安然 提交于 2021-02-19 08:22:27
问题 Given a string consists of words separated by a single white space, print out the words in descending order sorted by the number of times they appear in the string. For example an input string of “ab bc bc” would generate the following output: bc : 2 ab : 1 The problem would be easily resolved if C++ data structures, like a map, is used. But if the problem could only be solved in plain old C, it looks much harder. What kind of data structures and algorithms shall I use here? Please be as

Convert BFS code for a graph into a DFS code

*爱你&永不变心* 提交于 2021-02-19 07:19:59
问题 I'm sorry if this question sounds ambiguous but I was asked this in an interview. Write a program for BFS in a graph/tree. I wrote the popular code using a queue. Now he asked me to convert it to a DFS code by modifying just one line of the BFS code I had just written. The only answer I could think of was to use a stack for DFS. Then I implemented the stack using 2 queues. So in the end my answer was: use 1 queue for BFS. for DFS use 2 queues instead. He did not give me any feedback . Wasn't

Convert BFS code for a graph into a DFS code

半城伤御伤魂 提交于 2021-02-19 07:18:25
问题 I'm sorry if this question sounds ambiguous but I was asked this in an interview. Write a program for BFS in a graph/tree. I wrote the popular code using a queue. Now he asked me to convert it to a DFS code by modifying just one line of the BFS code I had just written. The only answer I could think of was to use a stack for DFS. Then I implemented the stack using 2 queues. So in the end my answer was: use 1 queue for BFS. for DFS use 2 queues instead. He did not give me any feedback . Wasn't

How do I assign multiple symbols to the same value in Ruby?

吃可爱长大的小学妹 提交于 2021-02-19 05:40:29
问题 The idea of what I am trying to do is to clump synonym symbols to the same value, without having to redefine the same value over and over. Basically turn this: fruits = { orange: "Citrus", grapefruit: "Citrus", tangerine: "Citrus" } Into this: fruits = { orange:, grapefruit:, tangerine: => "Citrus" } What is the proper syntax for accomplishing this? Thanks 回答1: Use a hash, in order to access the type of fruit using the fruit name. For example: fruits = %i{ orange grapefruit tangerine apple }

Benefit of a sentinel node in a red black tree?

半城伤御伤魂 提交于 2021-02-19 04:27:06
问题 I created a doubly-linked list, and the benefits of a sentinel node were clear - no null checks or special cases at list boundaries. Now I'm writing a red black tree, and trying to figure out if there is any benefit to such a concept. My implementation is based on the last two functions in this article (top down insertion/deletion). The author uses a "dummy tree root" or "head" to avoid special cases at the root for his insertion/deletion algorithms. The author's head node is scoped to the

equivalent to a sorted dictionary that allows duplicate keys

为君一笑 提交于 2021-02-18 20:15:49
问题 I need a data structure that can sort objects by the float keys they're associated with, lowest first. The trouble is that the keys represent cost so there are often duplicates, I don't care about this because if two have the same cost I'll just grab the first as it makes no difference, the problem is that the compiler complains. Is there a data structure that behaves in the same way but allows duplicate keys? EDIT - I still need the duplicates though because if one turns out to be a dead-end

equivalent to a sorted dictionary that allows duplicate keys

蓝咒 提交于 2021-02-18 20:15:16
问题 I need a data structure that can sort objects by the float keys they're associated with, lowest first. The trouble is that the keys represent cost so there are often duplicates, I don't care about this because if two have the same cost I'll just grab the first as it makes no difference, the problem is that the compiler complains. Is there a data structure that behaves in the same way but allows duplicate keys? EDIT - I still need the duplicates though because if one turns out to be a dead-end