-4

Please tell me the complexity of below tree. Please explanation the procedure of calculating it also.

The tree structure:

Below is the tree structure

root->left->right and root->right->left are pointing to same node.

Algorithm: If we traverse the tree with normal inorder traversal, then we will visit some nodes more than 1 time.

like

node4(2 times) 0->1->4 and 0->2->4

node7(3 times) 0->1->3->7, 0->1->4->7, 0->2->4->7

What will be the order of complexity of above algo?

Geek
  • 11
  • 1
  • 2
    Well, the storage complexity is O(N)… but if you want to know the complexity of an algorithm on this data structure, you'll have to describe the algorithm. – rob mayoff Nov 29 '15 at 09:00
  • This is basically useless in practice, as a search tree. If you have any particular use in mind, you have to specify that to have any hope of getting an answer. – yzt Nov 29 '15 at 09:05
  • @Rob: algorithm updated in question – Geek Nov 29 '15 at 09:24
  • @yzt: question was asked in an interview. So uses of this data structure doesn't matter, want to know the complexity only. – Geek Nov 29 '15 at 09:27

2 Answers2

2

In your diagrammed structure (which is a directed acyclic graph or “DAG”, not a tree), a traversal will visit two children for every node except the leaf nodes. Therefore it will visit a total of 2h-1 nodes (counting duplicates), where h is the height of the DAG. (The height is 5 in your diagram.)

Assuming the DAG is fully populated, then n (the number of nodes in the DAG) must be h(h+1)/2. (This is Gauss's formula for summing the integers from 1 through h.)

Solving n = h(h+1)/2 for h gives us h = (sqrt(8n + 1) - 1)/2, so the total number of nodes visited, in terms of n, is 2(sqrt(8n + 1) - 1)/2 - 1.

UPDATE

The traversal function would look like this:

def traverse(node):
    if node.left is not None:
        traverse(node.left)
    print(node.data)
    if node.right is not None:
        traverse(node.right)

Notice that traverse doesn't look at a node's parent, only its children. From traverse's point of view, it is irrelevant that some nodes are shared (and therefore have two parents). It cannot tell the difference between your DAG and a normal binary tree.

Every one of your DAG's interior nodes has exactly two children. So, from traverse's point of view, your DAG is identical to a fully-populated binary tree of height h. A fully-populated binary tree of height h has 2h-1 nodes.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

If we check level wise that how many times a level are getting traverse, then we will reach to below output

level 1 - 1times

level 2 - 2times

level 3 - 4times

level 4 - 8times

level 5 - 14times

upto

level h

total traversal is : 1 + 2 + 4 + 8 + 14 + ... upto h level

split series into 2 series:

(1+0) + (2+0) + (3+1) + (4+4) + (5+9) + ... h+sqOf(h-2)

means

1+2+3+4+ ... +h

+

sqOf(1) + sqOf(2) + sqOf(3) + ... +sqOf(h-2)

=>h(h+1)/2 + (h-2)(h-1)(2h-3)/6

so order of complexity become cubeOf(h) where h is (sqrt(8n + 1) - 1)/2 (as rob mayoff explained)

Geek
  • 11
  • 1