樹的基本演算法

來源:互聯網
上載者:User

#include "stdio.h"
#include "malloc.h"
#define MAXSIZE 100    //初始配置單位為100
//-------------------stack.h的定義----------------------------------------
//樹的結點定義
typedef int DataType_tree;  //樹結點的資料域類型

typedef struct nodetree{
 DataType_tree data;
 struct nodetree *lchild;
 struct nodetree *rchild;
}*BiTree,BiTNode;
//--------------------------------------------------------------
typedef BiTree DataType;   //棧結點的資料域的類型為樹結點的指標類型

typedef struct node     //棧的表示
{
 DataType *base;     //棧底指標始終指向棧底的位置
         //在棧構造之前和銷毀之後,base的值為NULL
 DataType *top;     //棧頂指標
 int stacksize;     //當前已指派的儲存空間,以元素為單位
}SqStack,Stack;

void InitStack(SqStack *st)    //初始化棧
{
 st->base=(DataType*)malloc(MAXSIZE*sizeof(DataType));
 if(!st->base) return;    //儲存分配失敗
 st->top=st->base;     //設定棧為空白
 st->stacksize=MAXSIZE;
}
int IsEmpty(SqStack st){    //判斷棧是否為空白
 if(st.top==st.base)
  return 1;
 else
  return 0;
}

void GetStack(SqStack st,DataType *dataElem){ //擷取棧頂元素
 if(st.top==st.base){
  printf("錯誤,棧空");
  return ;
 }
 *dataElem=*(st.top-1);     //擷取棧頂的值返回給參數指標指向的值
}
void PopStack(SqStack *st){     //出棧
 if(st->top==st->base){
   printf("棧空不能出棧");
  return ;
 }
 st->top=--(st->top);     //刪除棧頂元素
}
void PushStack(SqStack *st,DataType dataElem){ //進棧
 if(st->top-st->base>=st->stacksize){  //棧滿不能進棧
   printf("棧空不能出棧");
  return ;
 }
 *st->top=dataElem;
 st->top++;
}

///----------------------stack.h的定義結束-----------------------------------------

//------------------------樹的操作定義------------------------------
void ProTree(BiTree root){
//前序走訪樹遞迴演算法
 if(root!=NULL){
  printf("%5d",root->data);
  ProTree(root->lchild);
  ProTree(root->rchild);
 }
}
void Inorder(BiTree root){
//非遞迴演算法的中序遍曆
 BiTNode *p;
 SqStack s;
 InitStack(&s); 
 p=root;
 do
 {
  while(p!=NULL){     //若根節點不為空白則進棧
   PushStack(&s,p);   //p指向左孩子直到為空白
   p=p->lchild;
  }
  if(!IsEmpty(s)){
   GetStack(s,&p);
   PopStack(&s);    //出棧,列印該結點
   printf("%5d",p->data);
   p=p->rchild;    //訪問右孩子結點
  }
 }while(p!=NULL||!IsEmpty(s));  //若p不為空白或棧不為空白則繼續迴圈
}

BiTree CreatTree(){
//用前序遞迴演算法建立樹
 BiTNode *p;
 DataType_tree input_x;
 scanf("%d",&input_x);
 if(input_x==0)  p=NULL;
 else{
  p=(struct nodetree *)malloc(sizeof(BiTNode));
  p->data=input_x;
  p->lchild=CreatTree();
  p->rchild=CreatTree();
 }
 return p;
}
int Tree_size(BiTree root){       //二叉樹中的所有結點個數
 if(root==NULL)return 0;
 return 1+Tree_size(root->lchild)+Tree_size(root->lchild);
}
int Leaves_size(BiTree root){      //二叉樹的分葉節點個數
 if(root==NULL)return 0;
 if(root->rchild==NULL&& root->lchild) return 1;
 return Leaves_size(root->lchild)+Leaves_size(root->rchild);
}
int Tree_Height(BiTree t){      //二叉樹的高度,
 int hl,hr;
 if(t==NULL)return 0;
 hl=Tree_Height(t->lchild);
 hr=Tree_Height(t->rchild);
 if(hl>hr) return hl+1;
 else
  return hr+1;
}
int TreeNode_Level(BiTNode *t,BiTNode *p ,int d){  //返回二叉樹指定結點p在以*t為根的子樹的層次
 int levell;
 if(t==NULL) return 0;
 if(t==p)return (d);
 if((levell=TreeNode_Level(t->lchild,p,d+1))>0)
  return levell;
 else
  return TreeNode_Level(t->rchild,p,d+1);
}
void Tree_reflect(BiTNode *t){       //交換以*t為根的二叉樹中每個結點的2個子女
 BiTNode *p;
 if(t==NULL)return ;
 Tree_reflect(t->lchild);
 Tree_reflect(t->rchild);
 p=t->lchild;t->lchild=t->rchild;
 t->rchild=p;
}
void Tree_Defoliate(BiTNode *&t){     //從以*t為根的二叉樹中刪去所有的結點
 if(t==NULL) return ;
 if(t->rchild==NULL && t->lchild==NULL){
  free(t);t=NULL;
 }
 else{
  Tree_Defoliate(t->lchild);
  Tree_Defoliate(t->rchild);
 }
}
 
 

  
//--------------樹的操作定義結束--------------------------------
void main(){
 BiTree root;
 printf("\n\t\t請輸入正整數以0作為結束標誌\n");
 root=CreatTree();
 printf("\n\t前序輸出樹的結點資料域\n");
 Inorder(root); 
 printf("\n");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.