非递归实现二叉查找树

1、头文件:

#ifndef  _TREE_NONRECUR_H_
#define  _TREE_NONRECUR_H_


typedef  struct stack_record* stack;
typedef  struct link_node* Node;
typedef  struct treenode* searchtree;

typedef  searchtree elementType;
typedef  int tree_data;


struct stack_record
{
	int size;
	Node node;
};

struct link_node
{
	elementType data;
	Node next;
};

struct treenode
{
	int push_flag;//入栈次数统计
	tree_data data;
	searchtree left;
	searchtree right;
};

searchtree Insert(tree_data x,searchtree t);
searchtree FindMin(searchtree t);
searchtree FindMax(searchtree t);
searchtree Find(tree_data x,searchtree t);
void MakeEmpty(searchtree t);
void pre_visit(searchtree t);
void mid_visit(searchtree t);
void post_visit(searchtree t);

void PushStack(elementType x,stack s);
elementType topAndpop(stack s);

#endif



二、C文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <windows.h>
#include "tree_nonrecur.h"






void PushStack(elementType x,stack s)
{
	if(s->node)
	{
		Node ptr = (Node)malloc(sizeof(link_node));
		if(ptr == NULL)
		{
			printf("in push stack ,Node alloc error\n");
			exit(-2);
		}
		ptr->data = x;
		ptr->next = s->node;
		s->node = ptr;
		s->size++;
	}
	else
	{
		s->node = (Node)malloc(sizeof(link_node));
		if(s->node == NULL)
		{
			printf("in push stack,s->node alloca error\n");
			exit(-2);
		}

		s->node->data = x;
		s->node->next =NULL;
		s->size++;
	}
}

elementType topAndpop(stack s)
{
	if(s->size > 0)
	{
		elementType x = s->node->data;
		s->size--;
		Node ptr = NULL;
		ptr = s->node;
		s->node = s->node->next;
		free(ptr);
		ptr = NULL;
		return x;
	}
	else
		return NULL;
}

/*****************************************************
*插入:
*1、插入数据与插入节点比较,如果大于该节点数据,则插入该节点的左子树,反之,插入该节点的右子树;
*    每一次比较前先保存该节点地址
*2、申请新节点空间,将数据赋予此新节点,并根据1中比较的结果,插入先前保存节点地址的左节点或者右节点
*
*
******************************************************/
searchtree Insert(tree_data x,searchtree t)
{
	static int i = 1;
	if(t)
	{
		searchtree pt = t;
		printf("\n\nthe %d insert is %d\n",i++,x);
		searchtree ptr = NULL;
		while(pt)
		{
			ptr = pt;
			if(x > pt->data)
			{
				printf("\t x:%d > pt->data:%d,so x:%d insert pt->data:%d's right tree\n",x,pt->data,x,pt->data);
				pt = pt->right;
				
			}
			else if(x < pt->data)
			{
				printf("\tx:%d < pt->data:%d,so x:%d insert pt->data:%d's left tree\n",x,pt->data,x,pt->data);
				pt = pt->left;
				
			}
			else
				break;
		}

		if(pt == NULL)
		{
			searchtree pt = (searchtree)malloc(sizeof(treenode));
			if(pt == NULL)
			{
				printf("in Insert ,tree node alloca error\n");
				exit(-3);
			}

			pt->data = x;
			pt->left = NULL;
			pt->right = NULL;
			pt->push_flag = 0;
			if(pt->data < ptr->data)
			{
				ptr->left =pt;
			}
			else
			{
				ptr->right = pt;
			}
			printf("\tthe insert address is %p,the data is %d\n",pt,pt->data);
			return pt;
		}
		else
		{
			return pt;
		}
			

	}
	else
	{
		t = (searchtree)malloc(sizeof(treenode));
		if(t == NULL)
		{
			printf("in Insert,first tree node alloca error\n");
			exit(-3);
		}

		t->data = x;
		t->left = NULL;
		t->right = NULL;
		t->push_flag = 0;
		printf("\n\nthe %d insert is %d,the insert address is %p\n",i++,x,t);

		return t;
	}
}



searchtree FindMin(searchtree t)
{
	if(t)
	{
		searchtree pt_min = t;
		while(pt_min)
		{
			if(pt_min->left)
				pt_min = pt_min->left;
			else
				break;
		}

		return pt_min;
	}
	else
	{
		printf("in FindMin,the t is empty\n");
		return NULL;
	}

}

searchtree FindMax(searchtree t)
{
	if(t)
	{
		searchtree pt_max = t;
		while(pt_max)
		{
			if(pt_max->right)
				pt_max = pt_max->right;
			else
				break;
		}

		return pt_max;
	}
	else
	{
		printf("in FindMax,the tree is empty\n");
		return NULL;
	}
}

//前序遍历描述:对于每一个节点来说:
//第一步:打印自身节点数据
//第二步:如果有右节点,先将右节点入栈
//第三步:如果有左节点,则对左节点进行上述三步;如果没有左节点,则将出栈节点进行上述三步;
void pre_visit(searchtree t)
{
	if(t)
	{
		stack s = (stack)malloc(sizeof(struct stack_record));
		if(s == NULL)
		{
			printf("in FindMin,the stack alloc is error\n");
			exit(-4);
		}
		s->node = NULL;
		s->size = 0;

		searchtree pt = t;
		static int i = 0;
		while(pt)
		{
			printf("the %d data is %d\n",++i,pt->data);
			if(pt->right)
			{
				PushStack(pt->right,s);
				printf("the stack size is %d,push pt->right:%p into stack\n",s->size,pt->right);
			}
				
			if(pt->left)
			{
				pt = pt->left;
			}
			else
			{
				pt = topAndpop(s);
			}
		}
	}
}

//中序遍历:对于每一个节点:
//第一步:现将该节点的左子树中的所有左节点入栈
//第二步:出栈,然后打印出栈节点
//第三步:对出栈节点的右节点进行上述三步
void mid_visit(searchtree t)
{
	if(t)
	{
		stack s = (stack)malloc(sizeof(struct stack_record));
		if(s == NULL)
		{
			printf("in pre visit,the stack alloca error\n");
			exit(-5);
		}
		s->node = NULL;
		s->size = 0;

		searchtree pt = t;
		int i = 0;

		while(pt || s->size)
		{
			while(pt)
			{
				PushStack(pt,s);
				printf("push the pt->data :%d into stack,stack size is %d\n",pt->data,s->size);
				pt = pt->left;
			}

			pt = topAndpop(s);
			printf("the %d data is %d\n",++i,pt->data);
			pt = pt->right;

		}
			

	}

}


//
//后序遍历:对每一个节点
//第一步:先将左子树的所有左节点入栈
//第二步:对于出栈节点:如果无右节点,则打印,并且不停出栈,直到出栈节点具有右节点;如果出栈节点有右节点,则进行判断:
//					如果该节点只进栈一次,可以继续进栈,如果该节点已经进栈两次,则打印输出,然后不停出栈,直到遇到只进栈一次的节点;
//第三步:对出栈节点进行上述两步;
void post_visit(searchtree t)
{
	if(t)
	{
		stack s = (stack)malloc(sizeof(struct stack_record));
		if(s == NULL)
		{
			printf("the stack alloca error\n");
			exit(-6);
		}
		s->node = NULL;
		s->size = 0;

		searchtree pt = t;
		int i = 0;
		while(pt || s->size)
		{
			while(pt)
			{	
			
				PushStack(pt,s);
				pt->push_flag++;
				printf("push the pt->data :%d into stack,and pt->push_flag is %d,stack size is %d\n",pt->data,pt->push_flag,s->size);				
				pt = pt->left;

				
			}

			pt = topAndpop(s);
			while(pt)
			{
				if(pt->right)
				{
					if(pt->push_flag == 2)
					{
						printf("the %d data is :%d\n",++i,pt->data);
						pt = topAndpop(s);
					}
					else
					{
						PushStack(pt,s);
						pt->push_flag++;
						printf("push the pt->data :%d into stack,and pt->push_flag is %d,stack size is %d\n",pt->data,pt->push_flag,s->size);
						pt = pt->right;
						break;
					}
					
				}
				else
				{
					printf("the %d data is :%d\n",++i,pt->data);
					pt = topAndpop(s);
		
				}
			}
		}
	}
}



三:main文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree_nonrecur.h"

int main(int argc,char *argv[])
{
	int a[21] = {500,456,865,986,213,666,333,222,156,423,
                 856,899,966,776,772,399,469,586,398,11,12};
	
	int init_flag = 0;
	int i = 0;
	searchtree head = NULL;
	searchtree t = NULL;
	while(i < 21)
	{
		
		if(init_flag)
		{
			t = Insert(a[i],head);
			printf("the tree node address is %p,insert data is %d\n",t,t->data);
		}
		else
		{
			head = t = Insert(a[i],t);
			printf("the tree head address is %p,the first data is %d\n",t,t->data);
			init_flag = 1;
		}
		i++;

	}

	t = FindMin(head);
	printf("\n\nthe min data is %d\n",t->data);
	t = FindMax(head);
	printf("\n\nthe max data is %d\n",t->data);


	printf("\n\n\nin the pre visit:");
	pre_visit(head);
	printf("\n\n\nin the mid visit:");
	mid_visit(head);

	printf("\n\n\nin the post visit:");
	post_visit(head);
	system("pause");
	exit(1);
}



四:打印输出:

        the insert address is 00264F88,the data is 456
the tree node address is 00264F88,insert data is 456


the 3 insert is 865
         x:865 > pt->data:500,so x:865 insert pt->data:500's right tree
        the insert address is 002622D0,the data is 865
the tree node address is 002622D0,insert data is 865


the 4 insert is 986
         x:986 > pt->data:500,so x:986 insert pt->data:500's right tree
         x:986 > pt->data:865,so x:986 insert pt->data:865's right tree
        the insert address is 00262320,the data is 986
the tree node address is 00262320,insert data is 986


the 5 insert is 213
        x:213 < pt->data:500,so x:213 insert pt->data:500's left tree
        x:213 < pt->data:456,so x:213 insert pt->data:456's left tree
        the insert address is 00262370,the data is 213
the tree node address is 00262370,insert data is 213


the 6 insert is 666
         x:666 > pt->data:500,so x:666 insert pt->data:500's right tree
        x:666 < pt->data:865,so x:666 insert pt->data:865's left tree
        the insert address is 002623C0,the data is 666
the tree node address is 002623C0,insert data is 666


the 7 insert is 333
        x:333 < pt->data:500,so x:333 insert pt->data:500's left tree
        x:333 < pt->data:456,so x:333 insert pt->data:456's left tree
         x:333 > pt->data:213,so x:333 insert pt->data:213's right tree
        the insert address is 00262410,the data is 333
the tree node address is 00262410,insert data is 333


the 8 insert is 222
        x:222 < pt->data:500,so x:222 insert pt->data:500's left tree
        x:222 < pt->data:456,so x:222 insert pt->data:456's left tree
         x:222 > pt->data:213,so x:222 insert pt->data:213's right tree
        x:222 < pt->data:333,so x:222 insert pt->data:333's left tree
        the insert address is 00262460,the data is 222
the tree node address is 00262460,insert data is 222


the 9 insert is 156
        x:156 < pt->data:500,so x:156 insert pt->data:500's left tree
        x:156 < pt->data:456,so x:156 insert pt->data:456's left tree
        x:156 < pt->data:213,so x:156 insert pt->data:213's left tree
        the insert address is 002624B0,the data is 156
the tree node address is 002624B0,insert data is 156


the 10 insert is 423
        x:423 < pt->data:500,so x:423 insert pt->data:500's left tree
        x:423 < pt->data:456,so x:423 insert pt->data:456's left tree
         x:423 > pt->data:213,so x:423 insert pt->data:213's right tree
         x:423 > pt->data:333,so x:423 insert pt->data:333's right tree
        the insert address is 00264FD8,the data is 423
the tree node address is 00264FD8,insert data is 423


the 11 insert is 856
         x:856 > pt->data:500,so x:856 insert pt->data:500's right tree
        x:856 < pt->data:865,so x:856 insert pt->data:865's left tree
         x:856 > pt->data:666,so x:856 insert pt->data:666's right tree
        the insert address is 00265028,the data is 856
the tree node address is 00265028,insert data is 856


the 12 insert is 899
         x:899 > pt->data:500,so x:899 insert pt->data:500's right tree
         x:899 > pt->data:865,so x:899 insert pt->data:865's right tree
        x:899 < pt->data:986,so x:899 insert pt->data:986's left tree
        the insert address is 00265078,the data is 899
the tree node address is 00265078,insert data is 899


the 13 insert is 966
         x:966 > pt->data:500,so x:966 insert pt->data:500's right tree
         x:966 > pt->data:865,so x:966 insert pt->data:865's right tree
        x:966 < pt->data:986,so x:966 insert pt->data:986's left tree
         x:966 > pt->data:899,so x:966 insert pt->data:899's right tree
        the insert address is 002650C8,the data is 966
the tree node address is 002650C8,insert data is 966


the 14 insert is 776
         x:776 > pt->data:500,so x:776 insert pt->data:500's right tree
        x:776 < pt->data:865,so x:776 insert pt->data:865's left tree
         x:776 > pt->data:666,so x:776 insert pt->data:666's right tree
        x:776 < pt->data:856,so x:776 insert pt->data:856's left tree
        the insert address is 00265118,the data is 776
the tree node address is 00265118,insert data is 776


the 15 insert is 772
         x:772 > pt->data:500,so x:772 insert pt->data:500's right tree
        x:772 < pt->data:865,so x:772 insert pt->data:865's left tree
         x:772 > pt->data:666,so x:772 insert pt->data:666's right tree
        x:772 < pt->data:856,so x:772 insert pt->data:856's left tree
        x:772 < pt->data:776,so x:772 insert pt->data:776's left tree
        the insert address is 00265168,the data is 772
the tree node address is 00265168,insert data is 772


the 16 insert is 399
        x:399 < pt->data:500,so x:399 insert pt->data:500's left tree
        x:399 < pt->data:456,so x:399 insert pt->data:456's left tree
         x:399 > pt->data:213,so x:399 insert pt->data:213's right tree
         x:399 > pt->data:333,so x:399 insert pt->data:333's right tree
        x:399 < pt->data:423,so x:399 insert pt->data:423's left tree
        the insert address is 002651B8,the data is 399
the tree node address is 002651B8,insert data is 399


the 17 insert is 469
        x:469 < pt->data:500,so x:469 insert pt->data:500's left tree
         x:469 > pt->data:456,so x:469 insert pt->data:456's right tree
        the insert address is 00265208,the data is 469
the tree node address is 00265208,insert data is 469


the 18 insert is 586
         x:586 > pt->data:500,so x:586 insert pt->data:500's right tree
        x:586 < pt->data:865,so x:586 insert pt->data:865's left tree
        x:586 < pt->data:666,so x:586 insert pt->data:666's left tree
        the insert address is 00265258,the data is 586
the tree node address is 00265258,insert data is 586


the 19 insert is 398
        x:398 < pt->data:500,so x:398 insert pt->data:500's left tree
        x:398 < pt->data:456,so x:398 insert pt->data:456's left tree
         x:398 > pt->data:213,so x:398 insert pt->data:213's right tree
         x:398 > pt->data:333,so x:398 insert pt->data:333's right tree
        x:398 < pt->data:423,so x:398 insert pt->data:423's left tree
        x:398 < pt->data:399,so x:398 insert pt->data:399's left tree
        the insert address is 002652A8,the data is 398
the tree node address is 002652A8,insert data is 398


the 20 insert is 11
        x:11 < pt->data:500,so x:11 insert pt->data:500's left tree
        x:11 < pt->data:456,so x:11 insert pt->data:456's left tree
        x:11 < pt->data:213,so x:11 insert pt->data:213's left tree
        x:11 < pt->data:156,so x:11 insert pt->data:156's left tree
        the insert address is 002652F8,the data is 11
the tree node address is 002652F8,insert data is 11


the 21 insert is 12
        x:12 < pt->data:500,so x:12 insert pt->data:500's left tree
        x:12 < pt->data:456,so x:12 insert pt->data:456's left tree
        x:12 < pt->data:213,so x:12 insert pt->data:213's left tree
        x:12 < pt->data:156,so x:12 insert pt->data:156's left tree
         x:12 > pt->data:11,so x:12 insert pt->data:11's right tree
        the insert address is 00265348,the data is 12
the tree node address is 00265348,insert data is 12


the min data is 11


the max data is 986



in the pre visit:the 1 data is 500
the stack size is 1,push pt->right:002622D0 into stack
the 2 data is 456
the stack size is 2,push pt->right:00265208 into stack
the 3 data is 213
the stack size is 3,push pt->right:00262410 into stack
the 4 data is 156
the 5 data is 11
the stack size is 4,push pt->right:00265348 into stack
the 6 data is 12
the 7 data is 333
the stack size is 3,push pt->right:00264FD8 into stack
the 8 data is 222
the 9 data is 423
the 10 data is 399
the 11 data is 398
the 12 data is 469
the 13 data is 865
the stack size is 1,push pt->right:00262320 into stack
the 14 data is 666
the stack size is 2,push pt->right:00265028 into stack
the 15 data is 586
the 16 data is 856
the 17 data is 776
the 18 data is 772
the 19 data is 986
the 20 data is 899
the stack size is 1,push pt->right:002650C8 into stack
the 21 data is 966



in the mid visit:push the pt->data :500 into stack,stack size is 1
push the pt->data :456 into stack,stack size is 2
push the pt->data :213 into stack,stack size is 3
push the pt->data :156 into stack,stack size is 4
push the pt->data :11 into stack,stack size is 5
the 1 data is 11
push the pt->data :12 into stack,stack size is 5
the 2 data is 12
the 3 data is 156
the 4 data is 213
push the pt->data :333 into stack,stack size is 3
push the pt->data :222 into stack,stack size is 4
the 5 data is 222
the 6 data is 333
push the pt->data :423 into stack,stack size is 3
push the pt->data :399 into stack,stack size is 4
push the pt->data :398 into stack,stack size is 5
the 7 data is 398
the 8 data is 399
the 9 data is 423
the 10 data is 456
push the pt->data :469 into stack,stack size is 2
the 11 data is 469
the 12 data is 500
push the pt->data :865 into stack,stack size is 1
push the pt->data :666 into stack,stack size is 2
push the pt->data :586 into stack,stack size is 3
the 13 data is 586
the 14 data is 666
push the pt->data :856 into stack,stack size is 2
push the pt->data :776 into stack,stack size is 3
push the pt->data :772 into stack,stack size is 4
the 15 data is 772
the 16 data is 776
the 17 data is 856
the 18 data is 865
push the pt->data :986 into stack,stack size is 1
push the pt->data :899 into stack,stack size is 2
the 19 data is 899
push the pt->data :966 into stack,stack size is 2
the 20 data is 966
the 21 data is 986



in the post visit:push the pt->data :500 into stack,and pt->push_flag is 1,stack
 size is 1
push the pt->data :456 into stack,and pt->push_flag is 1,stack size is 2
push the pt->data :213 into stack,and pt->push_flag is 1,stack size is 3
push the pt->data :156 into stack,and pt->push_flag is 1,stack size is 4
push the pt->data :11 into stack,and pt->push_flag is 1,stack size is 5
push the pt->data :11 into stack,and pt->push_flag is 2,stack size is 5
push the pt->data :12 into stack,and pt->push_flag is 1,stack size is 6
the 1 data is :12
the 2 data is :11
the 3 data is :156
push the pt->data :213 into stack,and pt->push_flag is 2,stack size is 3
push the pt->data :333 into stack,and pt->push_flag is 1,stack size is 4
push the pt->data :222 into stack,and pt->push_flag is 1,stack size is 5
the 4 data is :222
push the pt->data :333 into stack,and pt->push_flag is 2,stack size is 4
push the pt->data :423 into stack,and pt->push_flag is 1,stack size is 5
push the pt->data :399 into stack,and pt->push_flag is 1,stack size is 6
push the pt->data :398 into stack,and pt->push_flag is 1,stack size is 7
the 5 data is :398
the 6 data is :399
the 7 data is :423
the 8 data is :333
the 9 data is :213
push the pt->data :456 into stack,and pt->push_flag is 2,stack size is 2
push the pt->data :469 into stack,and pt->push_flag is 1,stack size is 3
the 10 data is :469
the 11 data is :456
push the pt->data :500 into stack,and pt->push_flag is 2,stack size is 1
push the pt->data :865 into stack,and pt->push_flag is 1,stack size is 2
push the pt->data :666 into stack,and pt->push_flag is 1,stack size is 3
push the pt->data :586 into stack,and pt->push_flag is 1,stack size is 4
the 12 data is :586
push the pt->data :666 into stack,and pt->push_flag is 2,stack size is 3
push the pt->data :856 into stack,and pt->push_flag is 1,stack size is 4
push the pt->data :776 into stack,and pt->push_flag is 1,stack size is 5
push the pt->data :772 into stack,and pt->push_flag is 1,stack size is 6
the 13 data is :772
the 14 data is :776
the 15 data is :856
the 16 data is :666
push the pt->data :865 into stack,and pt->push_flag is 2,stack size is 2
push the pt->data :986 into stack,and pt->push_flag is 1,stack size is 3
push the pt->data :899 into stack,and pt->push_flag is 1,stack size is 4
push the pt->data :899 into stack,and pt->push_flag is 2,stack size is 4
push the pt->data :966 into stack,and pt->push_flag is 1,stack size is 5
the 17 data is :966
the 18 data is :899
the 19 data is :986
the 20 data is :865
the 21 data is :500
请按任意键继续. . .



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值