//
// main.c
// DataStructure
//
// Created by 仁和 on 16/8/30.
// Copyright © 2016年 wfw. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <malloc/malloc.h>
typedef int TElemType;
typedef int Status;
typedef struct BiTNode
{
TElemType data;
struct BiTNode * leftchild,*rightchild;
}BiTNode,*BiTree;//二叉树结点结构体
typedef struct stack {
BiTree data[100];
int flag[100];
int top;
}stack;//二叉树结点栈 先进后出
typedef struct queue
{
BiTree data[30];
int front;
int rear;
}queue;//二叉树结点队列 先进先出
void Creat_binTre(BiTree * root)//按照前序遍历建立二叉树
{
int a;
scanf("%d",&a);
if(a==0)
{
*root = NULL;
}
else
{
*root = (BiTNode*)malloc(sizeof(BiTNode));
(*root)->data = a;
printf("输入%d的左子节点:", a);
Creat_binTre(&(*root)->leftchild);
printf("输入%d的右子节点:", a);
Creat_binTre(&(*root)->rightchild);
}
}
void Perorder1(BiTree t)//按照前序遍历二叉树
{
if(t!= NULL)
{
printf("%d\n",t->data);
Perorder1(t->leftchild);
Perorder1(t->rightchild);
}
}
void InOrder(BiTree t)//按照中序递归遍历二叉树
{
if(t!=NULL)
{
InOrder(t->leftchild);
printf("%d\n",t->data);
InOrder(t->rightchild);
}
}
void PosOrder(BiTree t)//按照后序递归遍历二叉树
{
if(t!=NULL)
{
PosOrder(t->leftchild);
PosOrder(t->rightchild);
printf("%d\n",t->data);
}
}
void PreOrder2(BiTree t)//按照前序非递归遍历二叉树
{
BiTree pre = t;
stack s;
s.top = 0;
printf("输出前序遍历序列:\n");
while (pre||s.top>0) {
if(pre)
{
printf("%d",pre->data);
s.data[s.top++] = pre;
pre = pre->leftchild;
}
else
{
pre = s.data[--s.top]->rightchild;
}
}
printf("\n\n");
}
void InOrder2(BiTree t)
{
BiTree pre = t;
stack s;
s.top = 0;
printf("输出中序遍历序列:\n");
while (pre||s.top>0) {
if(pre)
{
s.data[s.top++] = pre;
pre = pre->leftchild;
}
else
{
pre = s.data[--s.top];
printf("%d",pre->data);
pre = pre->rightchild;
}
}
printf("\n\n");
}
void PosOrder2(BiTree t)//按照后序非递归遍历二叉树
{
stack s;
s.top = -1;
printf("输出后序遍历序列");
while (t!=NULL||s.top!=-1) {
while(t)
{
s.top++;
s.flag[s.top]=0;
s.data[s.top]=t;
t=t->leftchild;
}
while (s.top>=0&&s.flag[s.top]==1) {
t = s.data[s.top];
printf("%d",t->data);
s.top--;
}
if(s.top>=0)
{
t = s.data[s.top];
s.flag[s.top]=1;
t=t->rightchild;
}
else
{
t = NULL;
}
}
printf("\n\n");
}
void LevelOrder(BiTree t)//按照层次遍历二叉树
{
queue q;
q.data[0] = t;
q.front = 0;
q.rear = 1;
printf("层次遍历二叉树结果");
while (q.front<q.rear) {
if(q.data[q.front])
{
printf("%d",q.data[q.front]->data);
q.data[q.rear++]=q.data[q.front]->leftchild;
q.data[q.rear++]=q.data[q.front]->rightchild;
q.front++;
}
else
{
q.front++;
}
}
printf("\n\n");
}
void Exchange1(BiTree t)//递归法将二叉树的左右子树互换
{
BiTree temp;
if(t)
{
Exchange1(t->leftchild);
Exchange1(t->rightchild);
temp=t->leftchild;
t->leftchild = t->rightchild;
t->rightchild = temp;
}
}
void Exchange2(BiTree t)//非递归法将二叉树的左右子树互换
{
BiTree temp;
stack s;
s.top = 0;
while (t||s.top) {
if(t)
{
s.data[s.top++]=t;
temp=t->leftchild;
t->leftchild = t->rightchild;
t->rightchild = temp;
t = t->leftchild;
}
else
{
t = s.data[--s.top]->rightchild;
}
}
}
int Leavels_Num1(BiTree t)//递归法求叶子结点个数
{
if(t)
{
if(t->leftchild == NULL&&t->rightchild == NULL)
{
return 1;
}
return Leavels_Num1(t->leftchild)+Leavels_Num1(t->rightchild);
}
return 0;
}
int Leavels_Num2(BiTree t)//非递归法求叶子结点个数
{
int count = 0;
stack s;
s.top = 0;
while (t||s.top>0) {
if(t)
{
s.data[s.top++]=t;
if(t->leftchild==NULL&&t->rightchild == NULL)
{
count++;
}
t= t->leftchild;
}
else
{
t = s.data[--s.top]->rightchild;
}
}
return count;
}
//左节点插入
void inseartLeftNode(BiTree t,int a)
{
BiTree p ,newNode;
if(t == NULL)
return;
p = t->leftchild;
newNode = (BiTree)malloc(sizeof(BiTNode));
newNode->data = a;
newNode->rightchild = NULL;
newNode->leftchild=p;
t->leftchild = newNode;
}
//----------------------------------------//
//----------------排序二叉树相关函数---------//
//---------------------------------------//
//递归实现搜索二叉树的查找
BiTree Find(int a,BiTree t)
{
if(!t) return NULL;
if(a>t->data) return Find(a,t->rightchild);
else if (a<t->data) return Find(a,t->leftchild);
else return t;
}
//非递归实现搜索二叉树的查找
BiTree IterFind(int a,BiTree t)
{
while (t) {
if(a > t->data) t = t->rightchild;
else if (a < t->data) t = t->leftchild;
else return t;
}
return NULL;
}
//递归实现搜索树的最小值 最大值同理
BiTree FindMin(BiTree t)
{
if(!t) return NULL;
else if (!t->leftchild) return t;
else return FindMin(t->leftchild);
}
//非递归实现搜索树的最大值 最小值同理
BiTree FindMax_Iter(BiTree t)
{
if(t)
{
while (t->rightchild) {
t = t->rightchild;
}
return t;
}
return NULL;
}
//二叉搜索树的插入
BiTree Insert(BiTree t,int a)
{
if(!t)
{
t = (BiTree)malloc(sizeof(BiTNode));
t->data = a;
t->leftchild = t->rightchild = NULL;
}
else
{
if(a<t->data)
{
t->leftchild = Insert(t->leftchild, a);
}
else if (a>t->data)
{
t->rightchild = Insert(t->rightchild, a);
}
}
return t;
}
//二叉搜索树的删除
//三种情况 1 作为叶子节点 2 只有左子树或者只有右字树 3 既有左子树和右子树
BiTree Delete(int a,BiTree t)
{
BiTree tmp;
if(!t) printf("要删除的结点未找到");
else if (a<t->data) t->leftchild = Delete(a,t->leftchild);
else if (a>t->data) t->rightchild = Delete(a,t->rightchild);
else
{
if(t->leftchild&&t->rightchild)
{
tmp = FindMin(t->rightchild);
t->data = tmp->data;
t->rightchild = Delete(t->data,t->rightchild);
}
else
{
tmp = t;
if(!t->leftchild) t = t->rightchild;
else if(!t->rightchild) t = t->leftchild;
else free(tmp);
}
}
return t;
}
int main(int argc, const char * argv[]) {
BiTree t;
Creat_binTre(&t);
Perorder1(t);
InOrder(t);
PosOrder(t);
int a[10] = {1,4,12,45,67,89,123,678,987,1001};
int low = 0;
int high = 9;
int key = 90;
while (low<=high) {
int mid = (low+high)/2;
if(key == a[mid])
{
printf("ok");
low = INT8_MAX;
}
else if(key<a[mid])
{
high = mid-1;
}
else
{
low = mid+1;
}
}
printf("Hello, World!\n");
return 0;
}
c语言二叉树和二叉搜索树的实现
最新推荐文章于 2024-08-08 20:11:03 发布