Binary trees

This is a discussion on Binary trees within the C++ Programming forums, part of the General Programming Boards category; Hello, I just got this assignment on binary trees, its just tracing through and seeing what numbers i end up ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Binary trees

    Hello, I just got this assignment on binary trees, its just tracing through and seeing what numbers i end up with. I dont know how to read this and get the acctual answers. I can put it into visual studio and get it but would it be possible for any of you to explain to me of how this program specificly works?? thanks
    Code:
       1. Consider the program:
    
          #include <iostream>
          using namespace std;
    
          void p(int x)
          {
            if (x < 0) return;
            p(x-1);
            cout << x << endl;
            p(x-2);
          }
    
          main()
          {
            int y;
            while (cin >> y, y >= 0) {
              p(y);
              cout << "*************" << endl;
            }
          }
    
          Trace the program by hand to get the output of p(0), p(1), p(2), p(3) and p(4).
    
       2. Conisder the program:
    
          #include <iostream>
          using namespace std;
    
          void p(char A[], int m, int n)
          {
          	if (m == n) {
          		for (int i = 0; i < n; i++) cout << A[i];
          		cout << endl;
          		return;
          	}
          	for (char c = 'A'; c <= 'C'; c++) {
          		A[m] = c;
          		p(A,m+1,n);
          	}
          }
    
          main()
          {
          	char B[10];
          	int k;
          	cin >> k;
          	p(B,0,k);
          }
    
          Trace this program by hand and determine for nonnegative integer k, what is the output of p(B,0,k).

  2. #2
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I don't have the knowledge to explain, but here are some really great explanations:
    http://www.eternallyconfuzzled.com/t..._tut_bst1.aspx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 10:41 PM
  2. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 01:12 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21