爲求方便節點存儲數據類型默認爲char,未以模板形式實現
#pragma once
#include<iostream>
#include<Stack>
#include<Queue>
using namespace std;
struct BNode {
char data;
BNode *lChild, *rChild;
BNode(char a) {
data = a;
}
};
class BinaryTree {
BNode *root;
int refuseValue;
public:
BinaryTree() {
refuseValue = '#';
root = NULL;
}
~BinaryTree() {
destroy(root);
root = NULL;
}
void CreateBinTree_UsePreFlag(){
cout << "please input preOrder number with refuse value: " << endl;
CreateBinTree_UsePreFlag(root);
}
void CreateBinTree_UseGenTable() {
cout << "please input Generalized table: " << endl;
CreateBinTree_UseGenTable(root);
}
void CreateBinTree_Pre_mid() {
char pre[50];
char mid[50];
cout << "please input preOrder: " << endl;
cin >> pre;
cout << "please input midOrder: " << endl;
cin >> mid;
string s1(pre);
string s2(mid);
if (s1.length() != s2.length()) {
cout << "error input!" << endl;
return;
}
int n = s1.length();
CreateBinTree_Pre_mid(root,pre,mid,n);
}
void CreateBinTree_post_mid() {
char post[50];
char mid[50];
cout << "please input postOrder: " << endl;
cin >> post;
cout << "please input midOrder: " << endl;
cin >> mid;
string s1(post);
string s2(mid);
if (s1.length() != s2.length()) {
cout << "error input!" << endl;
return;
}
int n = s1.length();
CreateBinTree_post_mid(root, post, mid, n);
}
void preOrder() {
cout << "preOrder num: ";
preOrderPrint_UseStack2();
cout << endl;
}
void preOrder_GenTable() {
cout << "preOrder num with generalize table: ";
GenTablePrint(root);
cout << endl;
}
void midOrder() {
cout << "midOrder num: ";
midOrderPrint_UseRecursion(root);
cout << endl;
}
void postOrder() {
cout << "postOrder num: ";
postOrderPrint_UseStack();
cout << endl;
}
void levelOrderPrint() {
BNode* p = root;
queue<BNode*> Queue;
Queue.push(p);
while (1) {
if(p->lChild!=NULL)
Queue.push(p->lChild);
if (p->rChild != NULL)
Queue.push(p->rChild);
cout << p->data << " ";
Queue.pop();
if (Queue.empty())
break;
else
p = Queue.front();
}
}
BNode* getRoot() {
return root;
}
int size() {
return size(root);
}
int height() {
return height_UseRecursion(root);
}
BNode* parent(BNode* subTree, BNode* current) {
if (subTree == NULL)
return NULL;
if (subTree->lChild == current || subTree->rChild == current)
return subTree;
BNode* p;
if ((p = parent(subTree->lChild, current)) != NULL)
return p;
else
return parent(subTree->rChild, current);
}
void destroy(BNode *p) {
if (p == NULL)
return;
else {
destroy(p->lChild);
destroy(p->rChild);
delete p;
p = NULL;
}
}
static bool equal(BNode* a, BNode *b) {
if (a == NULL&&b == NULL)
return true;
if (a != NULL&&b != NULL && (a->data == b->data) && equal(a->rChild, b->rChild) && equal(a->lChild, b->lChild))
return true;
else
return false;
}
bool isFullBinaryTree_1() {
BNode *T = root;
if (!T)
return true;
int front = -1, rear = -1;
int last = 0, level = 0;
BNode* a[100];
stack<int> frontHistory;
a[++rear] = T;
BNode* p;
while (front < rear) {
p = a[++front];
if (p->lChild != NULL)
a[++rear] = p->lChild;
if (p->rChild != NULL)
a[++rear] = p->rChild;
if (front == last) {
last = rear;
level++;
frontHistory.push(front);
}
}
frontHistory.pop();
return (frontHistory.top()+1) == (pow(2, level - 1) - 1);
}
bool isFullBinaryTree_2() {
BNode* p = root;
queue<BNode*> que;
que.push(p);
while (!que.empty()) {
p = que.front();
que.pop();
if (p) {
que.push(p->lChild);
que.push(p->rChild);
}
else {
while (!que.empty())
{
p = que.front();
que.pop();
if (p)
return false;
}
}
}
return true;
}
void swapLeftAndRight(BNode* p) {
if (!p)
return;
queue<BNode*> que;
que.push(p);
while (!que.empty()) {
p = que.front();
que.pop();
if (p->lChild != NULL)
que.push(p->lChild);
if (p->rChild != NULL)
que.push(p->rChild);
swap(p);
}
}
void valueOfMidOrderNo(int i) {
int No = 0;
recursiveMidOrderTemp(root, i,No);
}
void printWayToX(char x) {
printWayToX_UseStack(root, x);
cout << endl;
}
char ClosestAncestorNode(char x, char y) {
if (root == NULL)
return '#';
stack<BNode*> s1,s2;
AncestorsNodeStack(x,s1);
AncestorsNodeStack(y,s2);
if (s1.empty() || s2.empty())
return '#';
int n = 0;
if (s1.size() >= s2.size())
{
n = s1.size() - s2.size();
for (int i = 0; i < n; i++)
s1.pop();
}
else {
n = s2.size() - s1.size();
for (int i = 0; i < n; i++)
s2.pop();
}
while (!s1.empty()) {
if (s1.top()->data == s2.top()->data)
return s1.top()->data;
s1.pop();
s2.pop();
}
return '#';
}
void AncestorsNodeStack(char x, stack<BNode*> &s) {
if (root == NULL)
return;
BNode* lastPos = NULL;
s.push(root);
while (!s.empty()) {
while (s.top()->lChild != NULL)
{
s.push(s.top()->lChild);
if (s.top()->data == x)
return;
}
while (!s.empty())
{
if (lastPos == s.top()->rChild || s.top()->rChild == NULL)
{
lastPos = s.top();
s.pop();
}
else if (s.top() != NULL)
{
s.push(s.top()->rChild);
if (s.top()->data == x)
return;
break;
}
}
}
}
int WidthOfBTree() {
if (root == NULL)
return 0;
int maxWidth = 0;
int front, rear;
front = rear = -1;
int last = 0;
BNode* p = root;
BNode* a[100];
a[++rear] = p;
while (front < rear) {
p=a[++front];
if (p->lChild != NULL)
a[++rear] = p->lChild;
if (p->rChild != NULL)
a[++rear] = p->rChild;
if (front == last)
{
last = rear;
if (rear - front > maxWidth)
maxWidth = rear - front;
}
}
return maxWidth;
}
private:
void preOrderPrint_UseRecursion(BNode* subTree) {
if (subTree != NULL) {
cout << subTree->data << " ";
preOrderPrint_UseRecursion(subTree->lChild);
preOrderPrint_UseRecursion(subTree->rChild);
}
}
void preOrderPrint_UseStack1() {
stack<BNode*> s;
BNode* p=root;
s.push(NULL);
while (p != NULL) {
cout << p->data << " ";
if (p->rChild != NULL)
s.push(p->rChild);
if (p->lChild != NULL)
{
p = p->lChild;
}
else {
p = s.top();
s.pop();
}
}
cout << endl;
}
void preOrderPrint_UseStack2() {
BNode* p = root;
stack<BNode*> s;
s.push(p);
while (!s.empty()) {
p = s.top();
s.pop();
cout << p->data << " ";
if (p->rChild != NULL)
s.push(p->rChild);
if(p->lChild!=NULL)
s.push(p->lChild);
}
cout << endl;
}
void midOrderPrint_UseRecursion(BNode* subTree) {
if (subTree != NULL) {
midOrderPrint_UseRecursion(subTree->lChild);
cout << subTree->data << " ";
midOrderPrint_UseRecursion(subTree->rChild);
}
}
void midOrderPrint_UseStack() {
BNode* p = root;
stack<BNode*> s;
do{
while (p != NULL) {
s.push(p);
p = p->lChild;
}
if (!s.empty()) {
p = s.top();
s.pop();
cout << p->data << " ";
p = p->rChild;
}
} while (p != NULL || !s.empty());
}
void postOrderPrint_UseRecursion(BNode* subTree) {
if (subTree != NULL) {
postOrderPrint_UseRecursion(subTree->lChild);
postOrderPrint_UseRecursion(subTree->rChild);
cout << subTree->data << " ";
}
}
void postOrderPrint_UseStack() {
if (root == NULL)
return;
BNode *p = root;
stack<BNode *> s;
s.push(p);
BNode *lastPop = NULL;
while (!s.empty())
{
while (s.top()->lChild != NULL)
s.push(s.top()->lChild);
while (!s.empty())
{
if (lastPop == s.top()->rChild || s.top()->rChild == NULL)
{
cout << s.top()->data << " ";
lastPop = s.top();
s.pop();
}
else if (s.top()->rChild != NULL)
{
s.push(s.top()->rChild);
break;
}
}
}
}
void CreateBinTree_UsePreFlag(BNode* &subTree) {
char item;
if (cin >> item)
{
if (item != refuseValue)
{
subTree = new BNode(item);
CreateBinTree_UsePreFlag(subTree->lChild);
CreateBinTree_UsePreFlag(subTree->rChild);
}
else {
subTree = NULL;
}
}
}
void CreateBinTree_UseGenTable(BNode* &BT) {
stack<BNode*> s;
BT = NULL;
BNode *p, *t;
int k;
char ch;
while (1)
{
cin >> ch;
if (ch == refuseValue)
break;
switch (ch)
{
case '(':
s.push(p);
k = 1;
break;
case ')':
s.pop();
break;
case ',':
k = 2;
break;
default:
p = new BNode(ch);
p->lChild = NULL;
p->rChild = NULL;
if (BT == NULL)
{
BT = p;
}
else {
if (k == 1)
{
t = s.top();
t->lChild = p;
}
else
{
t = s.top();
t->rChild = p;
}
}
}
}
}
void CreateBinTree_Pre_mid(BNode *&cur, const char *pre, const char *mid, int n) {
if (n <= 0)
{
cur = NULL;
return;
}
int k = 0;
while (pre[0] != mid[k]) {
k++;
}
cur = new BNode(mid[k]);
CreateBinTree_Pre_mid(cur->lChild, pre + 1, mid,k);
CreateBinTree_Pre_mid(cur->rChild, pre + k + 1, mid + k + 1,n-k-1);
}
void CreateBinTree_post_mid(BNode* &cur, const char *post, const char *mid, int n) {
if (n <= 0)
{
cur = NULL;
return;
}
int k = 0;
while (post[n - 1] != mid[k]) {
k++;
}
cur = new BNode(mid[k]);
CreateBinTree_post_mid(cur->lChild, post, mid, k);
CreateBinTree_post_mid(cur->rChild, post + k, mid + k+1,n-k-1);
}
int size(BNode* p) {
if (p == NULL)
return 0;
return 1 + size(p->lChild) + size(p->rChild);
}
int height_UseRecursion(BNode *p) {
if (p == NULL)
return 0;
int i = height_UseRecursion(p->lChild);
int j = height_UseRecursion(p->rChild);
return i > j ? i + 1 : j + 1;
}
int height_UseStack(BNode *T) {
if (!T)
return 0;
int front = -1, rear = -1;
int last = 0, level = 0;
BNode* tree[100];
tree[++rear] = T;
BNode* p;
while (front < rear) {
p = tree[++front];
if (p->lChild != 0)
tree[++rear] = p->lChild;
if (p->rChild != NULL)
tree[++rear] = p->rChild;
if (front == last)
{
level++;
last = rear;
}
}
return level;
}
void GenTablePrint(BNode *BT) {
if (BT != NULL)
{
cout << BT->data;
if (BT->lChild != NULL || BT->rChild != NULL)
{
cout << '(';
if (BT->lChild != NULL)
{
GenTablePrint(BT->lChild);
}
cout << ',';
if (BT->rChild != NULL)
{
GenTablePrint(BT->rChild);
}
cout << ')';
}
}
}
void swap(BNode* p) {
BNode* temp = p->lChild;
p->lChild = p->rChild;
p->rChild = temp;
temp = NULL;
}
void recursiveMidOrderTemp(BNode * subTree, int i, int &No) {
if (subTree != NULL) {
recursiveMidOrderTemp(subTree->lChild, i, No);
No++;
if (No == i) {
cout << subTree->data << endl;
return;
}
if (No > i)
return;
recursiveMidOrderTemp(subTree->rChild, i, No);
}
}
bool printWayToX_UseRecursion(BNode* p, char x) {
if (!p)
return false;
if (printWayToX_UseRecursion(p->lChild, x) || printWayToX_UseRecursion(p->rChild, x))
{
cout << p->data;
return true;
}
else if (p->data == x)
{
cout << p->data;
return true;
}
else {
return false;
}
}
void printWayToX_UseStack(BNode* p, char x) {
stack<BNode*> s;
BNode* lastPos = NULL;
s.push(p);
while (!s.empty()) {
while (s.top()->lChild != NULL)
{
s.push(s.top()->lChild);
if (s.top()->data == x) {
while (!s.empty()) {
cout << s.top()->data << " ";
s.pop();
}
return;
}
}
while (!s.empty())
{
if (lastPos == s.top()->rChild || s.top()->rChild == NULL)
{
lastPos = s.top();
s.pop();
}
else if (s.top() != NULL)
{
s.push(s.top()->rChild);
if (s.top()->data == x) {
while (!s.empty()) {
cout << s.top()->data << " ";
s.pop();
}
return;
}
break;
}
}
}
}
};
複製代碼