Comments on: Build binary tree from ancestor matrics https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/ Coding / System Design Interviews Sat, 11 Apr 2020 09:14:48 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Kamal Rawat https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1915 Wed, 05 Aug 2015 06:00:02 +0000 http://www.ritambhara.in/?p=310#comment-1915 In reply to PrasunM.

Hi PrasunM, First of all the ancestor matrix is part of the question. So we can’t change it.
Usually graphs are represented using either Adjacency List or matrix.. So, we can apply the same here as well (tree is a acyclic-connected graph), but there are few points:
– The constant factor of list is big (because we are also storing one pointer for each node). So it’s not that we are storing very less memory. For a tree with few noted, the list may end up taking more memory.
– Since we are storing just 0’s and 1’s in the matrix, it can be further optimized to store one BIT (rather than sizeof(int)) memory for each cell.
– Another advantage is as you pointed out. The time.

]]>
By: PrasunM https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1914 Mon, 03 Aug 2015 09:48:02 +0000 http://www.ritambhara.in/?p=310#comment-1914 Hi Kamal ,
Is it not possible to have a adjacency list having space complexity O(V+E) instead of adjacency(ancestor) matrix of
space 0(V^2) .In adjacency list , the look up function to check whether there is a edge from u to v requires O(V) time ,
but in adjacency(ancestor) matrix the same can be done in O(1) constant time .
Please provide your suggestion

]]>
By: Kamal Rawat https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1913 Wed, 17 Jun 2015 10:45:23 +0000 http://www.ritambhara.in/?p=310#comment-1913 In reply to chinky4u.

Hi, the post is updated with code now. thanks!

]]>
By: Kamal Rawat https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1912 Wed, 17 Jun 2015 10:44:00 +0000 http://www.ritambhara.in/?p=310#comment-1912 In reply to Jammy Gn.

The post is updated with code. Thanks!

]]>
By: Kamal Rawat https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1910 Wed, 17 Jun 2015 10:41:18 +0000 http://www.ritambhara.in/?p=310#comment-1910 In reply to Akhlak Ansari.

The post is updated with code. Thanks.

]]>
By: Ritambhara https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1911 Wed, 17 Jun 2015 10:35:10 +0000 http://www.ritambhara.in/?p=310#comment-1911 Hi Akhlak, Please find the code now. http://www.ritambhara.in/build-binary-tree-from-ancestor-matrics/

]]>
By: Kamal Rawat https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1909 Tue, 16 Jun 2015 05:00:10 +0000 http://www.ritambhara.in/?p=310#comment-1909 In reply to gen-y-s.

This is write.. when we write code, we don’t actually delete the row/column (because that is a very expensive operation).. If array is on the stack then it may not be possible also.. But it is easy to understand when we explain things like this.. good point anyway..

]]>
By: gen-y-s https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1908 Wed, 03 Jun 2015 08:35:41 +0000 http://www.ritambhara.in/?p=310#comment-1908 Algorithm is correct but the performs some useless things.
Specifically, in steps 4b and 4c, you don’t really need to delete rows or columns from the matrix. All that is needed (instead of steps 4b & 4c) is to check which nodes have the currect node (“temp”) as their ancestor by checking the ancestor column for each row, and decrementing the sum column (which is actually just a separate array) accordingly. When a sum is decremented to zero (meaning that the current “temp” node is its last remaining ancestor, so it is the parent), then we add the node to the queue.
The loop is repeated once for each node, and in each loop iteration we scan one column of the matrix, so the complexity is O(N^2) which is optimal since this is the size of the input that needs to processed.

]]>
By: Akhlak Ansari https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1907 Wed, 22 Apr 2015 13:41:03 +0000 http://www.ritambhara.in/?p=310#comment-1907 Can anybody provide me code for this. at the following email imtiazahmad211@gmail.com

]]>
By: Jammy Gn https://demo.ritambhara.in/build-binary-tree-from-ancestor-matrics/#comment-1906 Mon, 23 Dec 2013 06:45:27 +0000 http://www.ritambhara.in/?p=310#comment-1906 can you provide me code for this ?

]]>