tree

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** *

find all possible paths in a tree in python

馋奶兔 提交于 2021-02-19 08:33:57
问题 I am trying to create a list with all possible paths in a tree. I have following structure given (subset from DB): text = """ 1,Product1,INVOICE_FEE, 3,Product3,INVOICE_FEE, 7,Product7,DEFAULT, 2,Product2,DEFAULT,7 4,Product4,DEFAULT,7 5,Product5,DEFAULT,2 """ where the columns are: ID, product-name, invoice-type, reference-to-parent-ID. I would like to create list with all possible paths, like in the example: [[Product1],[Product3],[Product7,Product2,Product5],[Product7,Product4]] I do

find all possible paths in a tree in python

ε祈祈猫儿з 提交于 2021-02-19 08:33:25
问题 I am trying to create a list with all possible paths in a tree. I have following structure given (subset from DB): text = """ 1,Product1,INVOICE_FEE, 3,Product3,INVOICE_FEE, 7,Product7,DEFAULT, 2,Product2,DEFAULT,7 4,Product4,DEFAULT,7 5,Product5,DEFAULT,2 """ where the columns are: ID, product-name, invoice-type, reference-to-parent-ID. I would like to create list with all possible paths, like in the example: [[Product1],[Product3],[Product7,Product2,Product5],[Product7,Product4]] I do

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 to load Tree inside JComboBox?

早过忘川 提交于 2021-02-19 05:23:25
问题 How can I show a tree inside a JComboBox popup? Here is example tree: Theoretical computer science Mathematical logic Automata theory Algorithms and data structures Analysis of algorithms Algorithms 回答1: There is no default way to put a tree in a combo box. There are a couple of options: If you can give allowing expansion of nodes, you can achieve a similar effect by adding space before some of the options in a standard JComobBox. Or even space and a dash in front of leaf options. If you need

how to load Tree inside JComboBox?

戏子无情 提交于 2021-02-19 05:21:07
问题 How can I show a tree inside a JComboBox popup? Here is example tree: Theoretical computer science Mathematical logic Automata theory Algorithms and data structures Analysis of algorithms Algorithms 回答1: There is no default way to put a tree in a combo box. There are a couple of options: If you can give allowing expansion of nodes, you can achieve a similar effect by adding space before some of the options in a standard JComobBox. Or even space and a dash in front of leaf options. If you need

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

inserting in binary tree doesn't work using java

风格不统一 提交于 2021-02-17 03:05:21
问题 I'm currently learning about trees using java and i have some errors going on here in the insertion of items in a binary tree I don't why it doesn't work this is the code: tree node: public class TNode { int data; TNode left; TNode right; public TNode(int data) { this.data = data; left = null; right = null; } } tree class: public class Tree { TNode root; public Tree(){ root = null; } public TNode insertNode(TNode item, int d) { if (item == null) { return new TNode(d); } if (d < item.data) {