Program that counts all the vertices in a given tree

In summary, the conversation discusses the task of writing a program that counts the vertices in a given tree using any programming language. The person mentions their familiarity with Perl and C++, but admits to not being well-versed in algorithms. They ask for help in understanding the input format for the tree and for recommendations on reference materials. The process of traversing a tree is also briefly mentioned.
  • #1
snickersnee
34
0

Homework Statement



"Write a program that counts all the vertices in a given tree."

Any language can be used. (I've been using Perl mostly but could do C too. C++ would be ok too, if it was significantly easier.)

Homework Equations



I've been trying to find this. I read if a tree has n levels, the number of vertices is 2^{n-1}. And for n edges, the vertices are (n-1). Maybe that will help.

The Attempt at a Solution

I'm not making much progress because I don't even know what the input is supposed to look like. Also, this seems like the kind of thing that there's a formula for.
-What does the input look like? (how is the tree entered into the program?) I've seen the graphical version. But how would that tree be typed in?

I'm a grad student in electrical engineering but software is not my area of specialization. I didn't take the algorithms class. The problem would probably be simple to anyone who had taken the class. If you know a good reference book or website, I'd appreciate that too. Googling hasn't helped much.
 
Physics news on Phys.org
  • #2
This process is usually called a traversal of the tree, visiting each node, using the self-similar nature of trees - each branch is treated as a tree. The routine should accept a pointer to the root node which will have pointers to other parts of the tree. See Binary Tree (Wiki). A typical traversal can be some variation of:
- process left pointer if not null
- act on current node
- process right pointer if not null
 

Related to Program that counts all the vertices in a given tree

1. What is a tree data structure?

A tree is a data structure that consists of nodes connected by edges. It is a hierarchical structure where each node has a parent node and can have any number of child nodes.

2. How does a tree store data?

A tree can store data in each of its nodes. The data can be of any type, such as numbers, strings, or objects. Each node can hold one or more pieces of data.

3. What is a vertex in a tree?

A vertex, also known as a node, is a fundamental part of a tree data structure. It represents a single element or object in the tree and can contain both data and links to other nodes.

4. How does a program count all the vertices in a given tree?

A program can count all the vertices in a given tree by using a recursive function. The function will traverse through the tree and count each vertex it encounters, including the root node and all of its child nodes.

5. What is the time complexity of counting all the vertices in a given tree?

The time complexity of counting all the vertices in a given tree is O(n), where n is the number of nodes in the tree. This is because the program needs to visit each node in the tree to count it, and the time taken will increase linearly with the number of nodes in the tree.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top