二叉树排序

来源:互联网 发布:ubuntu ntp 时间同步 编辑:程序博客网 时间:2024/05/12 14:10

什么是二叉树?

二叉树必须满足三大条件:

1.假如左子树非空的话,那么左子树所有节点的值小于根节点

2.假如其右子树非空的话,那么右子树所有节点的值大于根节点的值

3.根节点的左右子树同时也是一颗二叉树

 

二叉树插入实现

1.基本的数据结构:

节点数据结构

typedef struct inode{
 struct inode *left;
 struct inode *right;
 int value;
} Inode;

树结构的一个封装,其中包含根节点的指针
typedef struct tree{
 Inode *t;
 int len;
} Tree;
2.下面实现的是一个前序遍历树的函数

void PreSearch(Inode *i){
 if(i!=NULL){
  cout<<i->value<<"  ";
  PreSearch(i->left);
  PreSearch(i->right);
 }
}
3.下面就是一个二叉树插入的函数了

i.递归使用

#include <iostream>
using namespace std;
//递归实现
void BST(Inode *tree,Inode *i){
 if(tree->value>i->value){
  if(tree->left==NULL)
   tree->left=i;
   
  else
   BST(tree->left,i);
  
 }
 else {
  if(tree->right==NULL)
   tree->right=i;
  else
   BST(tree->right,i);
 }
}
void BinarySortTree(Tree *tree,Inode *i){
 if(tree->t==NULL){
  tree->t=i;
 }else
  BST(tree->t,i);
 
}
void InitBinarySortTree(Tree *tree,int A[],int len){
 int i;
 tree->len=0;
 tree->t=NULL;
 for(i=0;i<len;i++){
  Inode *insert=(Inode *)malloc(sizeof(Inode));
  insert->left=NULL;
  insert->right=NULL;
  insert->value=A[i];
  //BST(&(tree->t),insert);
  BinarySortTree(tree,insert);
  tree->len++; 
 }
}

 

void print(int A[],int len){
 int i=0;
 for(i=0;i<len;i++)
  cout<<A[i]<<" ";
 cout<<endl;
}
void main(){
 
 int A[15]={12,3,4,6,98,123,3,56,78,11,65,455,324,0,1};
 print(A,15);
 Tree tree;
 InitBinarySortTree(&tree,A,15);
 PreSearch(tree.t);
 }

 II.下面是非递归的实现

/

void BST_NO(Inode *tree,Inode *i){
/* Stack stack;
 initStack(stack);
 push_stack(stack,Inode *i);
  */
 Inode *sign=tree;

 while(true){
  if(sign->value<i->value)
  {
   if(sign->right==NULL)
   {
    sign->right=i;
    break;
   }
   else{
    sign=sign->right;
   }
  }
  else if(sign->value>i->value)
  {
   if(sign->left==NULL){
    sign->left=i;
    break;
   }
   else
    sign=sign->left;
  }
  else
   break;

 }

}

//在此处修改成调用BST_NO(tree->t,i)即可,参数相同
void BinarySortTree(Tree *tree,Inode *i){
 if(tree->t==NULL){
  tree->t=i;
 }else
  //BST(tree->t,i);
  BST_NO(tree->t,i);
 
}

NOTE:

下面是二叉树的前序遍历的非递归实现~~~~~~~~~~~~~~~~~

#include <iostream>
using namespace std;

typedef struct inode{
 struct inode *left;
 struct inode *right;
 int value;
} Inode;
typedef struct tree{
 Inode *t;
 int len;
} Tree;
void PreSearch(Inode *i){
 if(i!=NULL){
  cout<<i->value<<"  ";
  PreSearch(i->left);
  PreSearch(i->right);
 }
}
void BST_Stack(Inode *tree,Inode *i){
 Inode *sign=tree;

 while(true){
  if(sign->value<i->value)
  {
   if(sign->right==NULL)
   {
    sign->right=i;
    break;
   }
   else{
    sign=sign->right;
   }
  }
  else if(sign->value>i->value)
  {
   if(sign->left==NULL){
    sign->left=i;
    break;
   }
   else
    sign=sign->left;
  }
  else
   break;

 }

}
void BinarySortTree(Tree *tree,Inode *i){
 if(tree->t==NULL)
  tree->t=i;
 else
  BST_Stack(tree->t,i);
 
}
void InitBinarySortTree(Tree *tree,int A[],int len){
 int i;
 tree->len=0;
 tree->t=NULL;
 for(i=0;i<len;i++){
  Inode *insert=(Inode *)malloc(sizeof(Inode));
  insert->left=NULL;
  insert->right=NULL;
  insert->value=A[i];
  //BST(&(tree->t),insert);
  BinarySortTree(tree,insert);
  tree->len++; 
 }
}

 

//非递归实现
typedef struct stack{
 Inode *top;
 Inode *down;
 Inode s[1000];
} Stack;

void initStack(Stack *stack){
 stack->down=&stack->s[0];
 stack->top=&stack->s[0];
}
void push_stack(Stack *stack,Inode *i){
 *(stack->top++)=*i;
 //*stack->top=*i;
 //stack->top++;
}
Inode* pull_stack(Stack *stack){

 return --stack->top;
}
void PreSearch_Stack(Inode *i){
 Stack stack;
 Inode *inode=i;
 if(inode==NULL)
  return;
 initStack(&stack);
 while(true){
  while(inode!=NULL){
   cout<<inode->value<<"  ";
   push_stack(&stack,inode);
   inode=inode->left;
  }
  if(stack.top>stack.down){
   inode=pull_stack(&stack);
   inode=inode->right;
  }
  else
   break;
 }
}

void print(int A[],int len){
 int i=0;
 for(i=0;i<len;i++)
  cout<<A[i]<<" ";
 cout<<endl;
}
void main(){
 
 int A[15]={12,3,4,6,98,123,3,56,78,11,65,455,324,0,1};
 print(A,15);
 Tree tree;
 InitBinarySortTree(&tree,A,15);
 PreSearch(tree.t); //递归
 cout<<endl;
 PreSearch_Stack(tree.t);//非递归实现
 cout<<endl;
 }

 

 


 

 

 

原创粉丝点击