السلام عليكم و رحمة الله و بركاته
أخواني كيف حالكم جميعا
انا ارغب في بناء شجرة ثنائية (Binary tree) و عندي (linkedList) هذه اللست تكون مرتبة كالآتي :
% 21
% 15
a 6
b 5
c 4
اريد أن اجعل % الذي قيمته 21 هو الجذر (root) ثم % الذي قيمته 15 هو الذي على يمين (root) و a يكون على يسار (root)
و b يكون على يمين % الذي قيمته 15 و c على يساره .
(ملاحظة هامة ... إذا كان الحرف "%" يكون له أوراق و إذا كان غير ذالك لا يكون له أوراق يكون هو نفسه الورقة
و هذا الكود الذي عملته:
#include<iostream>
using namespace std;
struct Node{
char letter;
int repeating;
char * code;
Node *left;
Node *right;
Node(){
left=NULL;
right=NULL;
}
~Node(){}
};
class IntBinaryTree{
public:
Node *root;
IntBinaryTree(){
root=NULL;
}
void insert(Node * newNode){
//root=new Node();
Node *node_ptr=new Node();
//newNode->left=newNode->right=NULL;
if(root==NULL){
root=newNode;
cout<<"+--------------------------------+"<<endl;
cout<<"root is NULL"<<endl;
cout<<"root is "<<newNode->letter<<endl;
cout<<"root is "<<newNode->repeating<<endl;
cout<<"+--------------------------------+"<<endl;
}
else
{
node_ptr=root;
while(node_ptr !=NULL){
if(node_ptr->letter == '%'){
if(node_ptr->right)
{
cout<<"+-----------------------------------------------+"<<endl;
cout<<"In right node is "<<node_ptr->right->letter<<endl;
cout<<"In right node is "<<node_ptr->right->repeating<<endl;
cout<<"BUT YOU DONOT ADD ANY THING"<<endl;
cout<<"+-----------------------------------------------+"<<endl;
node_ptr=node_ptr->right;
}
else if(node_ptr->left)
{
node_ptr=node_ptr->left;
cout<<"+-----------------------------------------------+"<<endl;
cout<<"In left node is "<<node_ptr->left->letter<<endl;
cout<<"In left node is "<<node_ptr->left->repeating<<endl;
cout<<"BUT YOU DONOT ADD ANY THING"<<endl;
cout<<"+-----------------------------------------------+"<<endl;
}
else if(!node_ptr->right){
cout<<"NO right node"<<endl;
node_ptr->right=newNode;
cout<<"+-----------------------------------------------+"<<endl;
cout<<"The left node is "<<node_ptr->right->letter<<endl;
cout<<"The left node is "<<node_ptr->right->repeating<<endl;
cout<<"+-----------------------------------------------+"<<endl;
break;
}
else if(!node_ptr->left){
cout<<"NO left node"<<endl;
node_ptr->left=newNode;
cout<<"+-----------------------------------------------+"<<endl;
cout<<"The left node is "<<node_ptr->left->letter<<endl;
cout<<"The left node is "<<node_ptr->left->repeating<<endl;
cout<<"+-----------------------------------------------+"<<endl;
break;
}
}
}
}
}
void display(Node *t1){
if (t1){
cout<<"in display() function"<<endl;
cout<<"+-----------------------------------------+"<<endl;
cout<<t1->letter<<endl;
cout<<"You in right"<<endl;
display(t1->right);
cout<<"You in left"<<endl;
display(t1->left);
}
}
};
int main(){
IntBinaryTree t1,t2;
Node *n1=new Node();
n1->letter='%';
n1->repeating=21;
n1->code="";
Node *n2=new Node();
n2->letter='%';
n2->repeating=15;
n2->code="";
Node *n3=new Node();
n3->letter='a';
n3->repeating=6;
n3->code="";
Node *n4=new Node();
n4->letter='b';
n4->repeating=5;
n4->code="";
Node *n5=new Node();
n5->letter='c';
n5->repeating=4;
n5->code="";
Node *n6=new Node();
n6->letter='%';
n6->repeating=3;
n6->code="";
Node *n7=new Node();
n7->letter='d';
n7->repeating=3;
n7->code="";
Node *n8=new Node();
n8->letter='e';
n8->repeating=2;
n8->code="";
Node *n9=new Node();
n9->letter='f';
n9->repeating=1;
n9->code="";
t1.insert(n1);
t1.insert(n2);
t1.insert(n3);
t1.insert(n4);
return 0;
}و لكم خالص الشكر والتقدير