اخوانى اخواتى
هدا كود بلغة السى واجهتنى صعوبة فى رسم الشجرة كما بالصورة
////////////////////////////////////////////////////////////////////////////////
//
//
//
// -----------------------------------------------------------------------------
//
// File : main.cpp
// Project :
// Role :
// Date :
// Author(s) : Adil C (3AM copyright © 2010) //
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
// Defines & Enumerations & structer
////////////////////////////////////////////////////////////////////////////////
struct node {
int data;
struct node* left;
struct node* right;
};
#define clrscr() system("CLS");
#define pause() system("PAUSE");
////////////////////////////////////////////////////////////////////////////////
// Global variables
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Prototypes
////////////////////////////////////////////////////////////////////////////////
struct node* NewNode(int);
int maxDepth(struct node*);
int size(struct node*);
int minValue(struct node*);
struct node* insert(struct node*, int);
void print(struct node *pnode);
/*=============================================================================*/
using namespace std;
////////////////////////////////////////////////////////////////////////////////
//
// M A I N F U N C T I O N
//
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
struct node * root = '\0';
char choice;
int data, continu = 1;
do
{
clrscr();
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, -3);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 8);
root = insert(root, 1);
root = insert(root, 0);
root = insert(root, -1);
cout << "\nB) Max depth for tree";
cout << "\nC) Size of tree";
cout << "\nF) Print Tree";
cout << "\nD) Min value in tree";
cout << "\nE) Exit";
cout << "\nplease enter your choice: [ ]\b\b";
cin >> choice;
switch(choice)
{
case 'a' :
case 'A' : cout<<"\nplease enter your data for insert: ";
cin>>data;
root = insert(root, data);
break;
case 'b' :
case 'B' : if(root == '\0')
cout<< "\nsorry ! no tree was found";
else{
int maxD = maxDepth(root);
cout<< "\nmax Depth ="<< maxD;
}
break;
case 'c' :
case 'C' : if(root == '\0')
cout<< "\nsorry ! no tree was found";
else{
int Size = size(root);
cout<< "\nthe size of the tree = " << Size;
}
break;
case 'd' :
case 'D' : if(root == '\0')
cout<< "\nsorry ! no tree was found";
else{
int Min = minValue(root);
cout<< "\nthe min value ot the tree = " << Min;
}
break;
case 'e':
case 'E':
continu = 0;
break;
case 'f':
case 'F': if(root == '\0')
cout<< "\nsorry ! no tree was found";
else
{
print(root);
}
break;
default:
cout << "Input Error!!!" << endl;
break;
}
if(choice != 'e' && choice != 'E')
getch();
}while(continu);//اصافة
}
//==============================================================================
//
// F U N C T I O N S
//
//==============================================================================
struct node* NewNode(int data)
{
struct node* node = new(struct node); // "new" is like "malloc"
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int maxDepth(struct node* node) {
if (node==NULL)
return(0);
else {
// compute the depth of each subtree
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
// use the larger one
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
}
}
struct node* insert(struct node* node, int data) {
// 1. If the tree is empty, return a new, single node
if (node == NULL) {
return(NewNode(data));
}
else {
// 2. Otherwise, recur down the tree
if (data <= node->data )
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return(node); // return the (unchanged) node pointer
}
}
int size(struct node* node) {
if (node==NULL)
return(0);
else
return(size(node->left) + 1 + size(node->right));
}
int minValue(struct node* node) {
struct node* current = node;
// loop down to find the leftmost leaf
while (current->left != NULL) {
current = current->left;
}
return(current->data);
}
void print(struct node *pnode)
{
int pdata = 0;
if(pnode != NULL)
{
print(pnode->left);
print(pnode->right);
if(pnode->left != NULL)
{
if(pnode->data > pnode->left->data)
cout << pnode->data << endl;
else
cout << pnode->data;
}
else if(pnode->right != NULL)
{
if(pnode->data < pnode->right->data)
cout << pnode->data << endl;
else
cout << pnode->data;
}
else
{
cout << pnode->data;
}
}
}
