Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent a non binary tree and how to do LCA on that tree?

How are non binary trees typically represented? Trees where there is no limit to the number of children a node can have. Is it best to use a Adjacency Matrix or Adjacency List and just assume there will be no cycles, or do something similar to this question ->

How to implement a Non-Binary tree

and follow up question, when you have a n-ary tree (is that the correct name for them?) What's a good way to find the Least Common Ancestor for two given nodes/data values in that tree? All I can find are algorithms that deal with binary trees, like this one ->

static Node lca(Node root,int v1,int v2)
{
  if (root == null || root.data == v1 || root.data == v2) {
    return root;
  }

  Node left = lca(root.left, v1, v2);
  Node right = lca(root.right, v1, v2);

  if (left != null && right != null) {
    return root;
  }

  return (left != null) ? left : right;
}
like image 986
drop27 Avatar asked Aug 03 '15 03:08

drop27


People also ask

How do you calculate LCA of a tree?

Application of Lowest Common Ancestor(LCA): To determine the distance between pairs of nodes in a tree: the distance from n1 to n2 can be computed as the distance from the root to n1, plus the distance from the root to n2, minus twice the distance from the root to their lowest common ancestor.

Can trees be Nonbinary?

4.1 Non-Binary Trees. A non-binary, or multifurcating, tree is a tree in which at least one node has more than two children. Such nodes are referred to as polytomies, or non-binary nodes. A polytomy can have several meanings [9].

What is LCA in binary tree?

What is “Lowest Common Ancestor (LCA)”? It is the lowest level parent shared by two nodes within a tree. Within the above binary tree, nodes 8 and 10 are highlighted.

What are the two ways of representing binary tree explain with example?

There are two different methods for representing. These are using array and using linked list. The index 1 is holding the root, it has two children 5 and 16, they are placed at location 2 and 3.


1 Answers

Adjacency matrix sounds like a bad idea, it will be very sparse (most cells will be empty). Usually for n-ary trees (yes that's how they are called) you just follow the same strategy as with the binary tree, the difference is that a binary tree would have 2 fields representing the left and right children:

class Node<T> {
   T value;
   Node<T> left;
   Node<T> right;
}

Here you change those fields into a data structure like an array (static or dynamic):

class Node<T> {
   T value;
   List<Node<T>> children;
}

As for the LCA, are you planning on storing the parent pointer in the nodes? Are the values supposed to be a tree with unique values? Will the values be ordered in any way?

If no, but you can assume that the nodes are in the tree (although handling the other case is not that hard) then the LCA is very similar to what you've shown above. You just need to change the part where you get the Node left and Node right so that it traverses all children:

int count = 0;
Node<T> temp = null;
for(Node<T> child : root.children) {
    Node<T> result = lca(child, v1, v2);
    if(result != null) {
        count++;
        temp = result;
    }
}

if(count == 2) {
    return root;
}

return temp;

With parent pointers and/or storing the dept in each node we can do better but at a storage cost.

like image 105
Mateusz Dymczyk Avatar answered Sep 28 '22 07:09

Mateusz Dymczyk