提交 cfe44b0a 编写于 作者: 东方怂天's avatar 东方怂天

Current files

上级 7a018afd
......@@ -7,6 +7,7 @@
"ostream": "cpp",
"vector": "cpp",
"sstream": "cpp",
"cmath": "cpp"
"cmath": "cpp",
"stack": "cpp"
}
}
\ No newline at end of file
/**
* Introduce:
* 1, We use char '#' to behalf the null node.
*/
//Include some library.
//Include Liberary.
#include <iostream>
#include <string>
#include <stack>
//Imprt the namespace to this file.
//Import the namespace.
using namespace std;
//Marco define.
#define MAXSIZE 100
//Define a struct to behalf the BinaryTree.
//Define the binary tree struction.
typedef struct BiNode
{
char data;
struct BiNode *LeftChild;
char Data;
struct BiNode *RightChild;
} BiNode, *BiTree;
//Expanding the first ordered binary tree
//Creat BiNode.
//recieves:
// BiNode *&BN : The node u want to open.
// char c : The data u want to push in.
//returns:
// None
void CreatBiNode(BiNode *&BN, char c)
//Pre-order creat binary tree.
void CreatBiTree_Rec(BiTree &root)
{
//Open a new node.
BN = new BiNode;
cout << "Now we new a new node." << c << endl;
//Push the char into data.
BN->Data = c;
cout << "Push:\t" << BN->Data << endl;
}
//Generating binary tree by recursion.
//recieves:
// BiTree &B : The node we should allocate memory space.
//returns:
// None
void CreatBiTree_Recursion(BiTree &B)
{
//Get the new node tag.
char s;
cin >> s;
//Get the input.
char c;
cin >> c;
//Determine the char entered.
switch (s)
switch (c)
{
//The node will be null.
case '#':
B = NULL;
root = NULL;
break;
//The node is not null.
default:
//Creat a new node which data is s.
CreatBiNode(B, s);
//Start recursion.
CreatBiTree_Recursion(B->LeftChild);
CreatBiTree_Recursion(B->RightChild);
//Creat a new root.
root = new BiNode;
//Set its left child and right child be null.
root->LeftChild = root->RightChild = NULL;
//Set the data value.
root->data = c;
//Recursively generated child tree.
CreatBiTree_Rec(root->LeftChild);
CreatBiTree_Rec(root->RightChild);
break;
}
}
//Define a stack of BiTree.
typedef struct BiTreeStack
{
BiNode *Stack[MAXSIZE];
int Top;
int Size;
} BiTreeStack;
//Init a BiTreeStack.
void InitBiTreeStack(BiTreeStack &BTS)
//Loop to creat binary tree.
void CreatBiTree_Loop(BiTree &root)
{
BTS.Top = 0;
BTS.Size = MAXSIZE;
}
stack<BiTree> BiStack;
//Push to BiTreeStakc.
void Push(BiTreeStack &BTS, BiNode *c)
{
//Safety inspection.
if (BTS.Top >= BTS.Size)
while (true)
{
cout << "The stakc is full" << endl;
return;
}
//Push in.
//Creat a new node which data is s
BTS.Stack[BTS.Top++] = c;
return;
}
//Pop from BiTreeStakc.
void Pop(BiTreeStack &BTS, BiNode *&c)
enum Type
{
Pre,
In,
Post
};
void DisplayBiTree(BiTree root, enum Type t)
{
/*
//Safety inspection.
if (BTS.Top <= 0)
if (!root)
{
cout << "The stakc is full" << endl;
return;
}
*/
//Pop it.
c = BTS.Stack[--BTS.Top];
return;
if (t == Pre)
{
cout << root->data;
}
DisplayBiTree(root->LeftChild, t);
if (t == In)
{
cout << root->data;
}
DisplayBiTree(root->RightChild, t);
if (t == Post)
{
cout << root->data;
}
}
void CreatBiTree_Ergodic(BiTree &B)
void DisplayBiTree_Stack(BiTree root, enum Type t)
{
//Init a stack.
BiTreeStack BTS;
InitBiTreeStack(BTS);
//Define a pointer to do many thing.
B = new BiNode;
BiNode *pB = B;
pB->Data = getchar();
cout << "GET\t" << pB->Data << endl;
//Push in.
Push(BTS, pB);
cout << "PUSH\t" << pB->Data << endl;
//This condition is not well-defined.
char c;
bool isRight = false;
while (c = getchar())
stack<BiTree> BiStack;
BiNode *p = root, *temp = NULL;
do
{
switch (c)
if (!p)
{
case '#':
//If null stack then end.
if (BTS.Top == 0)
p = BiStack.top();
if (t != Post)
{
return;
BiStack.pop();
}
//pB revoke.
Pop(BTS, pB);
cout << "POP\t" << pB->Data << endl;
isRight = true;
break;
default:
if (!isRight)
if (t == In)
{
pB->LeftChild = new BiNode;
pB = pB->LeftChild;
cout << p->data;
}
else
if (t == Post)
{
pB->RightChild = new BiNode;
pB = pB->RightChild;
if (!p->RightChild || p->RightChild == temp)
{
cout << p->data;
BiStack.pop();
temp = p;
p = NULL; //This is important.
continue;
}
}
//Tag the pB.
pB->Data = c;
pB->LeftChild = pB->RightChild = NULL;
cout << "GET\t" << c << endl;
//Push in.
Push(BTS, pB);
cout << "PUSH\t" << pB->Data << endl;
cout << "DIR\t" << (isRight ? "Right" : "Left") << endl;
isRight = false;
break;
p = p->RightChild;
continue;
}
}
return;
}
void ShowBiTree(BiTree &B)
{
if (!B)
{
return;
}
cout << B->Data << " ";
ShowBiTree(B->LeftChild);
ShowBiTree(B->RightChild);
return;
}
void ShowTree_Ergodic(BiTree B)
{
//Init a stack.
BiTreeStack BTS;
InitBiTreeStack(BTS);
//左可左入栈,tag初始0
//无根出栈一,tag++
//tag==1,转向看入栈
//tag==2,出栈继续走
//cout << p->data;
BiStack.push(p);
if (t == Pre)
{
cout << p->data;
}
p = p->LeftChild;
//因为要维护一个Stack,所以我不写了
} while (!BiStack.empty() || p);
}
\ No newline at end of file
#include <iostream>
#include <stack>
#include <string>
using namespace std;
class BiNode
{
public:
BiNode(){};
BiNode(char c) : val(c){};
char val;
BiNode *left = NULL;
BiNode *right = NULL;
};
BiNode *addTree(string str)
{
BiNode *root = new BiNode(str[0]);
stack<BiNode *> stk;
stk.push(root);
int id = 1;
bool A = false;
while (!stk.empty())
{
if (stk.top()->left == NULL && str[id - 1] != '#')
{
if (str[id] != '#')
{
BiNode *left = new BiNode(str[id]);
stk.top()->left = left;
stk.push(left);
}
id++;
}
else
{
if (stk.top()->right == NULL)
{
if (str[id] != '#')
{
BiNode *right = new BiNode(str[id]);
stk.top()->right = right;
stk.push(right);
}
else
{
stk.pop();
}
id++;
}
else
{
stk.pop();
}
}
}
return root;
}
void printTree(BiNode *root)
{
if (root != NULL)
{
cout << root->val;
if (root->left != NULL)
{
printTree(root->left);
}
if (root->right != NULL)
{
printTree(root->right);
}
}
}
int main()
{
}
\ No newline at end of file
//Include some library.
#include "BinaryTree.h"
//Function entrance.
int main()
{
/**
* Introduce:
* 1, Test input: AB#C##DE#F##G##
*/
BiTree root;
//State a BiTree.
BiTree b;
//ABD#E###CFG###H##
//Pre order:ABDECFGH
//In order:DEBAGFCH
//Post order:EDBGFHCA
cout << "Input ebt:" << endl;
CreatBiTree_Rec(root);
//Recursion type.
//CreatBiTree_Recursion(b);
//ShowBiTree(b);
//cout << endl;
cout << "Pre order:" << endl;
DisplayBiTree_Stack(root, Pre);
cout << endl;
DisplayBiTree(root, Pre);
cout << endl;
//Ergodic type.
CreatBiTree_Ergodic(b);
ShowBiTree(b);
cout << "In order:" << endl;
DisplayBiTree_Stack(root, In);
cout << endl;
DisplayBiTree(root, In);
cout << endl;
cout << "Post order:" << endl;
DisplayBiTree_Stack(root, Post);
cout << endl;
DisplayBiTree(root, Post);
cout << endl;
return 0;
}
\ No newline at end of file
ABD#E###CFG###H##
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册