二叉树各种求法

// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//

 

#include "stdafx.h"
#include <stdlib.h>

 


int  btdata[] = { 6,3,8,5,2,7,9,15,17 };

 

typedef struct BTNode
{
 int Data;

 

 BTNode * pleft;
 BTNode * pright;
}Node;

 

//插入数据  来组织一颗二叉树 
void Insert(Node **root, int InsertValue)
{
 Node * node = new Node();
 node->Data = InsertValue;
 node->pleft = NULL;
 node->pright = NULL;

 

 if (* root == NULL)
 {
  * root = node;
 }
 else
 {
  Node *temp = *root;
  while (temp != NULL)
  {
   if (InsertValue > temp->Data)
   {
    if (temp->pright == NULL)
    {
     temp->pright = node;
     return;
    }
    else
    {
     temp = temp->pright;
    }
   }

 

   if (InsertValue < temp->Data)
   {
    if (temp->pleft == NULL)
    {
     temp->pleft = node;
     return;
    }
    else
    {
     temp = temp->pleft;
    }
   }
     }
 }
}
//获得最大数  利用搜素二叉树的性质
int getMax(Node * root)
{
 int max = 0;
 
 if (root == NULL)
 {
  return -1;
 }
 else
 {
  Node * temp = root;
  int max = root->Data;
  while (temp->pright != NULL)
  {
   temp = temp->pright;
  }
  return temp->Data;

 

 }

 

}

 

//获得最小数  利用搜素二叉树的性质
int getMin(Node * root)
{
 int min = 0;

 

 if (root == NULL)
 {
  return -1;
 }
 else
 {
  Node * temp = root;
  int min = root->Data;
  while (temp->pleft != NULL)
  {
   temp = temp->pleft;
  }
  return temp->Data;

 

 }

 

}

 

//获得树高度  利用二叉树的性质
int GetHeight(Node * root)
{
 if (root == NULL)
 {
  return 0;
 }
 else
 {
  int right_height = GetHeight(root->pright);
  int left_height = GetHeight(root->pleft);

 

  return ((right_height) > (left_height) ? (right_height + 1) : (left_height + 1));
 }
}
void preorder(Node * node)
{
 if (node != NULL)
 {
  printf("%d\n",node->Data);
  preorder(node->pleft);
  preorder(node->pright);
 }
}

 

void inorder(Node * node)
{
 if (node != NULL)
 {
  inorder(node->pleft);
  printf("%d\n", node->Data); 
  inorder(node->pright);
 }
}

 

void postorder(Node * node)
{
 if (node != NULL)
 {
  postorder(node->pleft);
  postorder(node->pright);
  printf("%d\n", node->Data); 
 }
}

 

//获得最大数  递归实现  不利用搜素二叉树的性质
int getmax(Node * root)
{
 if (root == NULL)
 {
  return -1;
 }
 else
 {
  int m1 = getmax(root->pleft);
  int m2 = getmax(root->pright);
  int m3 = root->Data;
  int max = m3;
  if (m1 > max) { max = m1; }
  if (m2 > max) { max = m2; }
  return max;

 

 }

 

}

 

//获得最小数  递归实现  不利用搜素二叉树的性质
int getmin(Node * root,int *num)
{
 if (root == NULL)
 {
  return -1;//这里初始化
 }
 else
 {
  int num1 = 1000; int num2 = 1000;
  int m1 = getmin(root->pleft,&num1);
  int m2 = getmin(root->pright,&num2);
  int m3 = root->Data;
  int min = m3;
  if (num1 < min) { min = num1; }
  if (num2 < min) { min = num2; }
  *num = min;
  return 0;

 

 }
}
//BTNode  *BTnode;
void BuildBT()
{
 

 

 

 

}

 

int main()
{

 

 Node * Rootnode = NULL;
 for (int i = 0; i < sizeof(btdata)/sizeof(btdata[0]); i++)
 {
  Insert(&Rootnode, btdata[i]);
 }

 

 preorder(Rootnode);
 inorder(Rootnode);
 postorder(Rootnode);
 //

 

 int k = getMax(Rootnode);
 printf("max : %d\n", k);
    k = getMin(Rootnode);
 printf("min : %d\n", k);

 

 int height = GetHeight(Rootnode);
 printf("height : %d\n", height);

 

 int number = 1234;
 k = getmin(Rootnode,&number);
 printf("getmin : %d\n", number);
 system("pause");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值