Code
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
typedef struct TreeNode
{
char chDate;
TreeNode *lChild;
TreeNode *rChild;
}TreeNode, *BiTree;
typedef struct ListNode
{
BiTree chDate;
struct ListNode *pNext;
}Node, *QueueList;
int count = 0;
char a[15] =
{'a', 'b', 'd', '@', '@', 'e', '@', '@',
'c', 'f', '@', '@', 'g', '@', '@'};
class newQueue
{
public:
newQueue()
{
front = rear = NULL;
curIndex = 0;
};
~newQueue()
{
}
int IsEmpty()
{
if(curIndex > 0)
return 1;
else
return 0;
}
void EnQueue(BiTree node)
{
Node* newNode = new Node;
newNode->chDate = node;
newNode->pNext = NULL;
if(curIndex != 0)
{
rear->pNext = newNode;
rear = rear->pNext;
}
else
{
front = rear = newNode;
}
curIndex++;
}
BiTree DeQueue()
{
BiTree cTemp;
if(curIndex != 0)
{
Node* temp = front;
front = front->pNext;
cTemp = temp->chDate;
curIndex--;
delete temp;
return cTemp;
}
else
{
return NULL;
}
}
Node* Front()
{
return front;
}
Node* Rear()
{
return rear;
}
private:
Node* front, *rear;
int curIndex;
};
void CreatTreeNode(BiTree &other)
{
char c;
//cout<<"Input the date of the Node: ";
//cin>>c;
c = a[count];
count++;
if(c == '@')
{
other = NULL;
}
else
{
other = new TreeNode;
other->chDate = c;
CreatTreeNode(other->lChild);
CreatTreeNode(other->rChild);
}
}
void Visit(BiTree Node)
{
if(Node)
cout<<Node->chDate<<endl;
}
void LevelOrder(BiTree node)
{
newQueue *myQueue = new newQueue;
BiTree Temp = node;
if(node)
{
myQueue->EnQueue(Temp);
}
while(myQueue->IsEmpty())
{
Temp = myQueue->Front()->chDate;
Visit(Temp);
myQueue->DeQueue();
if(Temp->lChild)
{
myQueue->EnQueue(Temp->lChild);
}
if(Temp->rChild)
{
myQueue->EnQueue(Temp->rChild);
}
}
delete myQueue;
}
void main()
{
BiTree myBiTree;
CreatTreeNode(myBiTree);
cout<<"LevelOrder:"<<endl;
LevelOrder(myBiTree);
int z;
cin>>z;
}
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
typedef struct TreeNode
{
char chDate;
TreeNode *lChild;
TreeNode *rChild;
}TreeNode, *BiTree;
typedef struct ListNode
{
BiTree chDate;
struct ListNode *pNext;
}Node, *QueueList;
int count = 0;
char a[15] =
{'a', 'b', 'd', '@', '@', 'e', '@', '@',
'c', 'f', '@', '@', 'g', '@', '@'};
class newQueue
{
public:
newQueue()
{
front = rear = NULL;
curIndex = 0;
};
~newQueue()
{
}
int IsEmpty()
{
if(curIndex > 0)
return 1;
else
return 0;
}
void EnQueue(BiTree node)
{
Node* newNode = new Node;
newNode->chDate = node;
newNode->pNext = NULL;
if(curIndex != 0)
{
rear->pNext = newNode;
rear = rear->pNext;
}
else
{
front = rear = newNode;
}
curIndex++;
}
BiTree DeQueue()
{
BiTree cTemp;
if(curIndex != 0)
{
Node* temp = front;
front = front->pNext;
cTemp = temp->chDate;
curIndex--;
delete temp;
return cTemp;
}
else
{
return NULL;
}
}
Node* Front()
{
return front;
}
Node* Rear()
{
return rear;
}
private:
Node* front, *rear;
int curIndex;
};
void CreatTreeNode(BiTree &other)
{
char c;
//cout<<"Input the date of the Node: ";
//cin>>c;
c = a[count];
count++;
if(c == '@')
{
other = NULL;
}
else
{
other = new TreeNode;
other->chDate = c;
CreatTreeNode(other->lChild);
CreatTreeNode(other->rChild);
}
}
void Visit(BiTree Node)
{
if(Node)
cout<<Node->chDate<<endl;
}
void LevelOrder(BiTree node)
{
newQueue *myQueue = new newQueue;
BiTree Temp = node;
if(node)
{
myQueue->EnQueue(Temp);
}
while(myQueue->IsEmpty())
{
Temp = myQueue->Front()->chDate;
Visit(Temp);
myQueue->DeQueue();
if(Temp->lChild)
{
myQueue->EnQueue(Temp->lChild);
}
if(Temp->rChild)
{
myQueue->EnQueue(Temp->rChild);
}
}
delete myQueue;
}
void main()
{
BiTree myBiTree;
CreatTreeNode(myBiTree);
cout<<"LevelOrder:"<<endl;
LevelOrder(myBiTree);
int z;
cin>>z;
}