Hi,
I have two questions, if someone can help me please
I need it urgently!!

Thanks to all!

1. Tree class
Make a function that counts the number of nodes with two children in a binary tree.

2. List class
Make a function that prints the elements of the list with first element (START), that do not repeat.

This is part of the first question:

#include<iostream.h>


struct tree
{
char key;
tree *left;
tree *right;
}*root=NULL;


void main(){
char c;
cout<<"add elements\n";
while(cin>>c)
add(c,root);
cout<<"Tree:";
cout<<"nodes with 2 children:"<<count(root);
}


void add(int n,tree *&t)
{
if(t==NULL)
{
t=new tree;
t->key=n;
t->left=NULL;
t->right=NULL;
}
else
{
if(t->key<n)
add(n,t->right);
else
{
add(n,t->left);
}
}
}
//this function counts all nodes,but i need a function that counts ONLY nodes with 2 children.Help?
int count(tree *t){
if (t==NULL) return 0;
return count(t->left)+count(t->right)+1;
}

Thanks everyone!

Edited 3 Years Ago by happygeek: fixed formatting

This is a piece of the second one...

#include<iostream.h>
#include<conio.h>


struct elem {
  int key; 
  elem *next;
  elem *prev;
} *start=NULL;

int main(){
    int num,i,n;
	cout<<"Add numbers: ";
    cin>>n;
    for(i=0;i<n;i++) {
      cout<<"NUM: ";
      cin>>num;
      add(num);
      }
	list();
	print();
}



void add (int n)
  {
  elem *p=start;
  start=new elem;
  start ->key=n;
  start->next=p;
  start->prev=NULL;
  if(p!=NULL)
    p->prev=start;
  }

void list () {
  elem *p=start;
  while(p!=NULL) {
    cout<<p->key<<"\t";
    p=p->next;
    }
  cout<<endl;
  }

//Now I need the function for printing...

For the list problem---

1) the compiler/linker need to have seen at least the prototype, if not the whole definition, of a function before it is called. Since the compiler/linker start at the top and work down, that means the prototype, if not the whole definition needs to occur before you call main().

2) The function you've called list() should print the contents of the list in the order they appear in the list.

3) If you don't want to print duplicate versions of the same value of key, then you need some mechanism to know if you've already printed the value of the current p->key or not. If the list is sorted in order, then you can check to see if the current p->key is the same as the prior p->key. If it is then don't print it, if it isn't then do print it. If the list isn't ordered then you could store each unique key in a second container and search the container with each key in the list to see if it's in the container. If it is in the container then don't print it, if it isn't then print it and enter the current p->key value to the container.

3) the add() function needs some work. When working with lists it helps me to write down exactly what I am trying to do and to draw cute little pictures on a sheet of paper to demonstrate to myself how I plan to do it before I try to write any code. Give it a try and see if you can figure out what to do on your own.

4) Since trees are generalized lists, I'd put that problem off until the list one is done. Basically you could traverse the treee with whatever protocol (there are several traditional protocols available) you want and at each node see if either child is NULL. If neither child is NULL then increment a counter and go to the next node in the list.

This article has been dead for over six months. Start a new discussion instead.