Program that counts all the vertices in a given tree

  • Thread starter Thread starter snickersnee
  • Start date Start date
  • Tags Tags
    Program Tree
Click For Summary
SUMMARY

The discussion centers on writing a program to count all vertices in a given tree structure, with participants suggesting various programming languages including Perl, C, and C++. Key insights include the formulas for calculating vertices based on tree levels and edges, specifically that for n edges, the number of vertices is (n-1). The conversation highlights the importance of understanding tree traversal techniques, particularly the method of visiting each node using pointers to navigate through the tree structure.

PREREQUISITES
  • Understanding of tree data structures
  • Familiarity with programming languages such as Perl, C, or C++
  • Knowledge of tree traversal algorithms
  • Basic concepts of pointers and memory management in programming
NEXT STEPS
  • Research tree traversal algorithms, specifically depth-first and breadth-first traversal
  • Learn about binary trees and their properties
  • Explore programming techniques for handling pointers in C and C++
  • Study data structure courses or resources that cover tree algorithms
USEFUL FOR

This discussion is beneficial for computer science students, software developers, and anyone interested in understanding tree data structures and their traversal methods.

snickersnee
Messages
30
Reaction score
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
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
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K