answersLogoWhite

0


Best Answer

This can be achieved with a recursive-traversal function starting at the root.

Binary tree nodes use the following shorthand form (using tertiary conditional operator):

int Node::GetNodeCount()

{return( 1 +( left ? left.GetNodeCount() : 0 ) +

( right ? right.GetNodeCount() : 0 ));

}

Or in longhand:

int Node::GetNodeCount()

{// Always include self in countint count = 1;

// Is there a left node?if( left )

// Increment the count with a recursive traversal.count += left.GetNodeCount();

// Is there a right node?if( right )

// Increment the count with another recursive traversal.count += right.GetNodeCount();

// Return the total count to the caller.

return( count );

}

Tree nodes with variable child nodes (lists of lists) use the following longhand form:

int Node::GetNodeCount()

{// Always include self in count.

int count = 1;

// Start with this node's head node (the first child node).

Node node = head;

// Iterate through all child nodes.

while( node )

{// Increment the count with a recursive call.count += node.GetNodeCount();

// Traverse to the next child node.node = node.next;}

// Return the total count to the caller.

return( count );

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a programme segment to find out the total number of nodes in a tree?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How does increasing number of nodes affect an LAN?

Depending on the topology, increasing the number of nodes in a LAN or segment will result in higher collisions, more attenuation of the signal, and less throughput of data.


The maximum number of nodes per segment depends on the?

its attentuation not bandwidth as u may think....


What is the difference between a populated segment and an unpopulated segment?

populated segments a network segment that contains ends nodes,such as work stations. unpopulated segments a network segment that does not contain end nodes, such as workstations. Unpopulated segments are also called link segments.


What is inter-node?

An internode is a portion of plant stem between nodes. An internodal segment is a portion of nerve fibre.


Will an Increase in number of nodes slow down LAN?

Yes, depending on the number of nodes added, the topology of the network, and how busy the nodes are.


What is usually composed of a group of nodes that use the same communications channel for all their traffic?

Segment


What method of transmission does a workstation use to send an ARP request?

A broadcast to all the nodes on its segment


What method of transmission does a workstation use to send out an ARP request?

A broadcast to all the nodes on its segment


Determine whether or not the network is traversable?

"The rule to find whether a network is traversable or not is by looking at points called nodes. Nodes are places where two or more lines meet. On these networks, the nodes are clearly shown by the black points in the diagrams. Now you are probably wondering what this has to do with the network being traversable or not. The node either would have an odd or even number of lines connected to it. Do not count the nodes with an even number of lines connected to it. Count the number of nodes with an odd number of lines connected to it. If there are no odd nodes or if there are two odd nodes, that means that the network it traversable. Networks with only two odd nodes are in a traversable path and networks with no odd nodes are in a traversable circuit."


When a network has 10 nodes and 17 branches an all then the number of nodes pair voltages would be?

49


Count the number of nodes of a binary tree having depth n?

Use the following formula: (2^n)-1. E.g., if the depth is 3, the number of nodes is (2^3)-1 = 8-1 = 7. Note that 7 is the maximum number of nodes, not the actual number of nodes. To count the actual nodes you must traverse the tree, updating an accumulator as you go.


What is the number of nodes of degree 2 in binary tree which has n leaf nodes?

Ne=N2+1Here Ne=no. of leaf nodesN2= no. of nodes of degree 2