树 的例题



#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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈夫曼是一种特殊的二叉结构,用于编码和解码数据。在哈夫曼中,每个叶子节点都代表一个字符或符号,并且具有一个与之关联的权值,代表该字符或符号出现的频率或概率。根据哈夫曼的概念,我们可以通过给定的叶子节点的权值来构建哈夫曼。 对于给定的叶子节点的权值,构建哈夫曼的步骤如下: 1. 首先,根据叶子节点的权值从小到大进行排序。 2. 选取权值最小的两个叶子节点,并将它们作为两个子节点创建一个新的父节点。新父节点的权值等于这两个子节点的权值之和。 3. 将这个新的父节点插入到叶子节点中,同时删除原来的两个子节点。 4. 重复步骤2和步骤3,直到只剩下一个节点,即根节点,这个节点就是哈夫曼的根节点。 根据题目提供的例子,我们可以看到一种不是建的方法,只使用数组来模拟哈夫曼的构造过程。这种方法是通过数组来存储节点的信息,并通过一些特定的计算方式来模拟构建哈夫曼的过程。 根据题目的描述,我们需要根据叶子节点的个数和权值来生成哈夫曼,并计算所有节点的值与权值的乘积之和。这个问题可以通过构建哈夫曼的步骤来解决。首先,我们需要将叶子节点根据权值进行排序。然后,按照步骤2和步骤3构建哈夫曼,直到只剩下一个节点。最后,计算所有节点的值与权值的乘积之和。 综上所述,数据结构哈夫曼例题是通过给定叶子节点的权值来构建哈夫曼,并计算所有节点的值与权值的乘积之和。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [超好理解的哈夫曼(最优二叉)与例题](https://blog.csdn.net/weixin_45720782/article/details/109316157)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值