#include <iostream>
#include <malloc.h>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
//二叉树
#if 1
typedef char ElmType;
typedef struct Tree
{
ElmType date;
struct Tree *lchild, *rchild;
}TreeNode;
//创建 先序
void CreatTreeFromPre(TreeNode **root)
{
ElmType ch;
cin >> ch;
if (ch != '*')
{
*root = new TreeNode;
(*root)->date = ch;
CreatTreeFromPre(&(*root)->lchild);
CreatTreeFromPre(&(*root)->rchild);
}
else
{
*root = NULL;
}
}
//创建 前序中序 //????????????????????????/
TreeNode* CreatTree(ElmType *pre, ElmType *mid, int len)
{
if (len == NULL)
{
return NULL;
}
ElmType *p = mid;
int k = 0;
while (*p != *pre)
{
p++;
k++;
}
TreeNode *root = new TreeNode;
root->date = *pre;
//左半部分
root->lchild = CreatTree(pre+1,mid,k); //调试 奔溃怀疑参数问题
//右半部分
root->rchild = CreatTree(pre+k+1,mid+k+1,len-k-1);
return root;
}
//递归遍历
// 先序
void PrintTreeFromPre(TreeNode *root)
{
if (root != NULL)
{
cout<< root->date << " ";
PrintTreeFromPre(root->lchild);
PrintTreeFromPre(root->rchild);
}
}
// 中序
void PrintTreeFromMid(TreeNode *root)
{
if (root != NULL)
{
PrintTreeFromMid(root->lchild);
cout << root->date << " ";
PrintTreeFromMid(root->rchild);
}
}
// 后序
void PrintTreeFromEnd(TreeNode *root)
{
if (root != NULL)
{
PrintTreeFromEnd(root->lchild);
PrintTreeFromEnd(root->rchild);
cout << root->date << " ";
}
}
//非递归
// 先序
void PrintTreeFromPre1(TreeNode *root)
{
stack<TreeNode *> s;
TreeNode *p = root;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
cout << p->date << " ";
s.push(p);
p = p->lchild;
}
p = s.top();
p = p->rchild;
s.pop();
}
}
// 中序
void PrintTreeFromMid1(TreeNode *root)
{
stack<TreeNode*> s;
TreeNode *p = root;
cout << "非递归中序遍历:";
while (p || !s.empty())
{
while (p != NULL)
{
s.push(p);
p = p->lchild;
}
p = s.top();
cout << p->date << " ";
s.pop();
p = p->rchild;
}
cout << endl;
}
// 后序 ?????????????????????????
void PrintTreeFromEnd1(TreeNode *root)
{
}
//层次遍历
void LayerOrder(TreeNode *root)
{
queue<TreeNode *> q;
TreeNode *p = root;
if (p == NULL)
return;
q.push(p);
while (!q.empty())
{
p = q.front();
cout<< p->date<<" ";
q.pop();
if (p->lchild != NULL)
q.push(p->lchild);
if (p->rchild != NULL)
q.push(p->rchild);
}
}
//叶子节点的个数
//递归不能在函数内打印输出结果 每次都会打印出来 而不是只打印一次
int LeafCount(TreeNode *root)
{
static int count = 0;
if (root != NULL)
{
LeafCount(root->lchild);
LeafCount(root->rchild);
if (root->lchild == NULL && root->rchild == NULL)
count++;
}
else
{
return 0;
}
return count;
}
int LeafCount1(TreeNode *root) //不会走
{
if (root == NULL)
{
return 0;
}
int lcount = 0;
int rcount = 0;
lcount = LeafCount1(root->lchild);
rcount = LeafCount1(root->rchild);
if (lcount + rcount == 0)
{
return 1;
}
else
{
return lcount + rcount;
}
}
//二叉树拷贝
void CopyTree(TreeNode **des, TreeNode **src)
{
if (*src == NULL)
{
*des = NULL; //cout<<"数为空"<<enld; 错误!!!!!
}
else
{
*des = new TreeNode;
(*des)->date = (*src)->date;
CopyTree(&(*des)->lchild, &(*src)->lchild);
CopyTree(&(*des)->rchild, &(*src)->rchild);
}
}
void main()
{
TreeNode *root = NULL;
//CreatTreeFromPre(&root);
//PrintTreeFromPre1(root);
//PrintTreeFromEnd(root);
ElmType str1[] = "ABHFDECKG";
ElmType str2[] = "HBDFAEKCG";
root = CreatTree(str1,str2,8);
PrintTreeFromEnd(root);
//PrintTreeFromPre1(root);
//PrintTreeFromMid1(root);
//LayerOrder(root);
//cout << LeafCount(root) << endl;;
//cout << LeafCount1(root) << endl;
/*TreeNode *root2 = NULL;
CopyTree(&root2, &root);
PrintTreeFromPre(root2);*/
}
#endif
//哈弗曼
#if 0
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define Max 1000
typedef struct
{
int weight;
int parent;
int lchild;
int rchild;
int flag;
}HaffTree;
void Create_HaffTree(HaffTree *pHaffTree, int *weight, int n)
{
int i, j;
int min1, min2;
int x1, x2;
//初始化
for (i = 0; i<2 * n - 1; i++)
{
if (i<n)
{
pHaffTree[i].weight = weight[i];
}
else
{
pHaffTree[i].weight = 0;
}
pHaffTree[i].parent = -1;
pHaffTree[i].lchild = -1;
pHaffTree[i].rchild = -1;
pHaffTree[i].flag = 0; //0没有遍历 1遍历多
}
for (i = 0; i<n - 1; i++)
{
min1 = min2 = Max;
x1 = x2 = -1;
//找最小和次小值
for (j = 0; j<n + i; j++)
{
if (pHaffTree[j].flag == 0 && pHaffTree[j].weight<min1)
{
min2 = min1; //次小得值
min1 = pHaffTree[j].weight;
x2 = x1; //次小值得位置
x1 = j;
}
else if (pHaffTree[j].flag == 0 && pHaffTree[j].weight<min2)
{
min2 = pHaffTree[j].weight;
x2 = j;
}
}
//修改对应数组值
pHaffTree[n + i].weight = min1 + min2;
pHaffTree[n + i].lchild = x1;
pHaffTree[n + i].rchild = x2;
pHaffTree[x1].flag = 1;
pHaffTree[x2].flag = 1;
pHaffTree[x1].parent = n + i;
pHaffTree[x2].parent = n + i;
}
}
void HaffCode(HaffTree *pHaffTree, int n)
{
char ch[10] = "";
//申请二位数组,存储字符串
char **code = (char**)malloc(sizeof(char*)*n);
for (int i = 0; i<n; i++)
{
code[i] = (char*)malloc(sizeof(char)*n);
}
int parent, child;
for (int i = 0; i<n; i++)
{
parent = pHaffTree[i].parent;
child = i;
int start = n;
while (parent != -1)
{
if (child == pHaffTree[parent].lchild)
{
ch[start--] = '0';
}
else
{
ch[start--] = '1';
}
child = parent;
parent = pHaffTree[parent].parent;
}
strcpy(code[i], ch + start + 1);
}
for (int i = 0; i<n; i++)
{
printf("%s\n", code[i]);
}
}
void main()
{
int weight[] = { 7,5,2,4};
int n = sizeof(weight) / sizeof(weight[0]);
HaffTree *pHaffTree = (HaffTree*)malloc(sizeof(HaffTree)*(2 * n - 1));
Create_HaffTree(pHaffTree, weight, n);
for (int i = 0; i<2 * n - 1; i++)
{
printf("%d ", pHaffTree[i].weight);
}
printf("\n");
HaffCode(pHaffTree, n);
}
#endif
//二叉排序树
#if 0
typedef int ElmType;
typedef struct node
{
ElmType date;
struct node *lchild, *rchild;
}BTree;
//插入
void InsertTree(BTree **root, int key)
{
if (*root == NULL)
{
BTree *s = new BTree;
s->date = key;
s->lchild = NULL;
s->rchild = NULL;
*root = s;
}
else if ((*root)->date > key )
InsertTree(&(*root)->lchild, key);
else if ((*root)->date < key)
InsertTree(&(*root)->rchild, key);
}
//遍历 先序
void PrintTree(BTree *root)
{
if (root)
{
cout << root->date << " ";
PrintTree(root->lchild);
PrintTree(root->rchild);
}
}
//查找
BTree* SearchTree(BTree *root, int key)
{
if (root == NULL)
{
cout << "二叉排序树为空,不能查找" << endl;
return NULL;
}
else
{
if (root->date == key)
{
return root;
}
else if (root->date > key)
{
return SearchTree((root)->lchild, key);
}
else
{
return SearchTree((root)->rchild, key);
}
}
}
//删除
void DeleteTree(BTree **root, int key)
{
BTree *p = *root;
BTree *f = NULL;
while (p)
{
if (p->date == key)
break;
else
{
f = p;
if (p->date > key)
p = p->lchild;
else
p = p->rchild;
}
}
if (p == NULL) //没找到
return;
else
{
if (p->lchild == NULL) //无左
{
if (f == NULL)
{
*root = p->rchild;
}
else if (f->lchild == p)
{
f->lchild = p->rchild;
}
else
{
f->rchild = p->rchild;
}
}
else //有左
{
BTree *s = p->lchild;
BTree *q = p;
while (s->rchild != NULL) //找到左子树的最大值
{
q = s;
s = s->rchild;
}
if (q == p) /*将s的左子树链到q上*/
q->lchild = s->lchild;
else
q->rchild = s->lchild;
/*
if (s == p->lchild) //p的左子树无右孩子
{
p->lchild = s->lchild;
}
else
{
q->rchild = s->lchild;
}
*/
p->date = s->date;
free(s);
}
}
}
void main()
{
BTree *root = NULL;
int a[6] = { 122, 99, 250, 110, 300, 280 };
int i = 0;
for (i; i < 6; i++)
{
InsertTree(&root, a[i]);
}
PrintTree(root);
cout << endl;
/*BTree *s = SearchTree(root, 300);
cout << s->date << endl;*/
DeleteTree(&root, 300);
PrintTree(root);
}
#endif
//重建二叉树 根据前,中序
#if 1
typedef struct node
{
int date;
struct node* lchild, *rchild;
}BinTree;
int a[8] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int b[8] = { 4, 7, 2, 1, 5, 3, 8, 6 };
//创建
BinTree* CreatTree(int *pre, int *mid, int n)
{
if (n <= 0)
{
return NULL;
}
int k = 0;
int *p = mid;
while (*p != *pre)
{
p++;
k++;
}
BinTree *s = new BinTree;
s->date = *pre;
s->lchild = CreatTree(pre+1,mid,k);
s->rchild = CreatTree(pre+k+1,mid+k+1,n-k-1);
return s;
}
void PostOrder(BinTree *T)
{
if (T != NULL)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%d ", T->date);
}
}
void main()
{
BinTree *root = new BinTree;
root = CreatTree(a, b, 8);
PostOrder(root);
}
#endif
#if 1
typedef struct node
{
int data;
struct node *lchild, *rchild;
}BinTree;
int a[8] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int b[8] = { 4, 7, 2, 1, 5, 3, 8, 6 };
void PreInCreateTree(BinTree **root, int PreIndex, int InIndex, int subTreeLen)
{
if (subTreeLen <= 0)
{
*root = NULL;
return ;
}
*root = new BinTree;
(*root)->data = a[PreIndex];
int index = 0;
while (a[PreIndex] != b[index])
{
index++;
}
//PreInCreateTree(&((*root)->lchild), PreIndex + 1, InIndex, index);
//PreInCreateTree(&((*root)->rchild), PreIndex + index + 1, index + 1, subTreeLen - index - 1);
//错误 root->lchild 有可能是有右子树的左遍历 长度index 为index-(有半部分的起始地址)
int LenF = index - InIndex;
int LenR = subTreeLen - 1 - LenF;
PreInCreateTree(&((*root)->lchild), PreIndex + 1, InIndex, LenF);
PreInCreateTree(&((*root)->rchild), PreIndex + LenF + 1, index + 1, LenR);
}
void PostOrder(BinTree *T)
{
if (T != NULL)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%d ", T->data);
}
}
void main()
{
BinTree *T = NULL;
PreInCreateTree(&T, 0, 0, 8);
PostOrder(T);
}
#endif
#if 0
typedef char ElmType;
typedef struct Tree
{
ElmType date;
struct Tree *lchild, *rchild;
}TreeNode;
//创建 先序
void CreatTreeFromPre(TreeNode **root)
{
ElmType ch;
cin >> ch;
if (ch != '*')
{
*root = new TreeNode;
(*root)->date = ch;
CreatTreeFromPre(&(*root)->lchild);
CreatTreeFromPre(&(*root)->rchild);
}
else
{
*root = NULL;
}
}
//递归遍历
void PrintTreeFromPre(TreeNode *root)
{
if (root != NULL)
{
cout << root->date << " ";
PrintTreeFromPre(root->lchild);
PrintTreeFromPre(root->rchild);
}
}
//树的子结构
bool HasBinTreFun(TreeNode *root1, TreeNode *root2)
{
if (root1 == NULL)
{
return false;
}
if (root2 == NULL)
{
return true;
}
if (root1->date == root2->date)
{
return true;
}
return (HasBinTreFun(root1->lchild, root2->lchild) &&
HasBinTreFun(root1->rchild, root2->rchild));
}
bool HasBinTree(TreeNode *root1, TreeNode *root2)
{
bool result = false;
if (root1 == NULL || root2 == NULL)
{
return false;
}
else
{
if (root1->date == root2->date)
{
result = HasBinTreFun(root1, root2);
}
if (!result)
result = HasBinTree(root1->lchild, root2);
if (!result)
result = HasBinTree(root1->rchild, root2);
return result;
}
}
//树的镜像
TreeNode* MirrorTree(TreeNode *root)
{
if (root == NULL)
{
return NULL;
}
if ((root->lchild == NULL && root->rchild == NULL))
{
return root;
}
TreeNode *temp = NULL;
temp = root->lchild;
root->lchild = root->rchild;
root->rchild = temp;
if (root->lchild != NULL)
{
MirrorTree(root->lchild);
}
if (root->rchild != NULL)
{
MirrorTree(root->rchild);
}
return root;
}
//从上到下遍历二叉树
void TopToBotto(TreeNode *root)
{
queue<TreeNode *> s;
TreeNode *p = root;
if (p == NULL)
{
return;
}
s.push(p);
while (!s.empty())
{
p = s.front();
cout << p->date << " ";
if (p->lchild != NULL)
{
s.push(p->lchild);
}
if (p->rchild != NULL)
{
s.push(p->rchild);
}
s.pop();
}
}
//二叉搜索树的后序遍历
bool VerifyTree(int sequeuece[],int len)
{
if (len <= 0 || sequeuece == NULL)
{
return false;
}
int i;
int root = sequeuece[len - 1];
for ( i = 0; i < len-1; i++)
{
if (sequeuece[i] > root)
break;
}
int j = i;
for (j; j < len - 1; j++)
{
if (sequeuece[j] < root)
return false;
}
bool left = true;
if (i>0)
{
left = VerifyTree(sequeuece, i);
}
bool right = true;
if (i < len - 1)
{
right = VerifyTree(sequeuece+i,len-i-1);
}
return(left && right);
}
//二叉树某一值得路径
void FindPath1(TreeNode *root, int sum, vector<int>&path, int currentsum)
{
currentsum = currentsum + root->date;
path.push_back(root->date);
//如果是叶子节点 并且路径上的节点和=给定值 打印路径
bool isleaf = ((root->lchild == NULL) &&( root->rchild == NULL));
if (currentsum == sum && isleaf)
{
cout << "路径为";
vector<int>::iterator iter = path.begin();
for (; iter != path.end(); iter++)
{
cout << *iter << " ";
}
cout << endl;
}
//如果不是叶子节点 遍历他的子节点
if (root->lchild != NULL)
{
FindPath1(root->lchild, sum, path, currentsum);
}
if (root->rchild != NULL)
{
FindPath1(root->rchild, sum, path, currentsum);
}
//返回父节点之前 在历经上删除当前节点
path.pop_back();
}
void FindPath(TreeNode *root, int sum)
{
if (root == NULL)
{
return;
}
vector<int> path;
int currebtsum = 0;
FindPath1(root, sum, path, currebtsum);
}
//二叉树的深度
int DeepTree(TreeNode *root)
{
if (root == NULL)
{
return 0;
}
int left = 0; //static int left = 0; //?????????????
int right = 0; //static int right = 0;
if (root->lchild != NULL)
{
left = DeepTree(root->lchild);
}
if (root->rchild != NULL)
{
right = DeepTree(root->rchild);
}
/*int left = DeepTree(root->lchild);
int right = DeepTree(root->rchild);*/
return (left > right) ? (left + 1) : (right + 1);
}
//二叉搜索树与双向链表
TreeNode* Convert(TreeNode*root)
{
TreeNode *plast = NULL; //指向双向链表的尾节点
TreeNode *phead = plast;
//需要返回头结点
while (phead != NULL && phead->lchild != NULL)
{
phead = phead->rchild;
}
return phead;
}
void ConverNode(TreeNode *root, TreeNode **plast)
{
if (root == NULL)
{
return;
}
TreeNode *current = root;
if (current->lchild != NULL)
{
ConverNode(current->lchild, plast);
}
current->lchild = *plast;
if (*plast != NULL)
(*plast)->rchild = current;
*plast = current;
if (current->rchild != NULL)
{
ConverNode(current->rchild, plast);
}
}
void main()
{
TreeNode *root1 = NULL;
CreatTreeFromPre(&root1);
/*TreeNode *root2 = NULL;
CreatTreeFromPre(&root2);
cout << HasBinTree(root1, root2) << endl;;*/
/*MirrorTree(root1);
PrintTreeFromPre(root1);*/
//TopToBotto(root1);
/*int sequeuece[7] = {5,7,6,9,11,10,8};
int len = sizeof(sequeuece) / sizeof(sequeuece[0]);
cout << VerifyTree(sequeuece, len) << endl;*/
//cout << DeepTree(root1) << endl;;
}
#endif