中序遍历
步骤:
1.如果节点有左子树,该节点入栈,否则访问该节点。 2.如果节点有右子树,重复步骤1。 3.如果节点没有右子树,访问完毕,根据栈顶指示退回,访问栈顶元素,并访问右子树,重复步骤1。 4.如果栈为空,遍历结束。
例子(c++版):
# include <iostream>
# include <stack>
using namespace std;
typedef struct Binary_tree
{
int user_data;
Binary_tree* left;
Binary_tree* right;
} binary_tree;
binary_tree* left_push_cpp ( binary_tree* b1, stack< binary_tree* > & s)
{
binary_tree* temp_push = b1;
if ( temp_push == nullptr )
{
printf ( "树为空!!!\n\n" ) ;
return nullptr ;
}
s. push ( temp_push) ;
while ( temp_push-> left != nullptr )
{
s. push ( temp_push-> left) ;
temp_push = temp_push-> left;
}
return temp_push;
}
void show_stack ( binary_tree* b1)
{
stack< binary_tree* > s;
binary_tree* temp_show = left_push_cpp ( b1, s) ;
s. pop ( ) ;
while ( temp_show)
{
printf ( "%d\n\n" , temp_show-> user_data) ;
if ( temp_show-> right)
{
printf ( "左子树有值!!!\n\n" ) ;
temp_show = left_push_cpp ( temp_show-> right, s) ;
s. pop ( ) ;
}
else if ( s. size ( ) > 0 )
{
temp_show = s. top ( ) ;
s. pop ( ) ;
}
else
{
break ;
}
}
}
int main ( )
{
binary_tree* b1 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b2 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b3 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b4 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b5 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b6 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b7 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b8 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
int u1 = 1111 ;
b1-> user_data = u1;
int u2 = 2222 ;
b2-> user_data = u2;
int u3 = 3333 ;
b3-> user_data = u3;
int u4 = 4444 ;
b4-> user_data = u4;
int u5 = 5555 ;
b5-> user_data = u5;
int u6 = 6666 ;
b6-> user_data = u6;
int u7 = 7777 ;
b7-> user_data = u7;
int u8 = 8888 ;
b8-> user_data = u8;
b1-> left = b2;
b1-> right = b3;
b2-> left = b4;
b2-> right = b5;
b3-> left = b6;
b3-> right = b7;
b4-> left = b8;
b4-> right = nullptr ;
b5-> left = nullptr ;
b5-> right = nullptr ;
b6-> left = nullptr ;
b6-> right = nullptr ;
b7-> left = nullptr ;
b7-> right = nullptr ;
b8-> left = nullptr ;
b8-> right = nullptr ;
show_stack ( b1) ;
return 0 ;
}
例子(c语言版):
# include <stdio.h>
# include <algorithm>
# include <malloc.h>
# include <string.h>
# include <stdlib.h>
# include <windows.h>
typedef struct Binary_tree
{
int user_data;
Binary_tree* left;
Binary_tree* right;
} binary_tree;
void show_tree_recursion ( binary_tree* b1)
{
if ( b1 == nullptr )
{
printf ( "本分支结束!!!\n\n" ) ;
return ;
}
printf ( "root_data = %d\n\n" , b1-> user_data) ;
show_tree_recursion ( b1-> left) ;
show_tree_recursion ( b1-> right) ;
}
int deep ( binary_tree* b1)
{
int left = 0 ;
int right = 0 ;
int root = 0 ;
if ( b1 == nullptr )
{
return 0 ;
}
left = deep ( b1-> left) ;
right = deep ( b1-> right) ;
root = 1 + ( left > right ? left : right) ;
return root;
}
void count_leaf ( binary_tree* b1, int * sum)
{
if ( b1-> left == nullptr && b1-> right == nullptr )
{
( * sum) ++ ;
printf ( "找到叶子节点啦!!!sum = %d\n\n" , * sum) ;
}
if ( b1-> left)
{
printf ( "这不是叶子节点,继续寻找!!!\n\n" ) ;
count_leaf ( b1-> left, sum) ;
}
if ( b1-> right)
{
printf ( "这不是叶子节点,继续寻找!!!\n\n" ) ;
count_leaf ( b1-> right, sum) ;
}
}
typedef struct Node
{
Node* next;
} node;
typedef struct Common_list
{
node head;
int length;
} common_list;
common_list* creat_list ( )
{
common_list* temp_creat;
temp_creat = ( common_list* ) malloc ( sizeof ( common_list) ) ;
if ( temp_creat == nullptr )
{
printf ( "空间开辟错误!!!\n" ) ;
return nullptr ;
}
printf ( "已成功创建空列表!!1\n" ) ;
temp_creat-> head. next = nullptr ;
temp_creat-> length = 0 ;
printf ( "空列表初始化成功!!1\n" ) ;
return temp_creat;
}
void insert_list ( common_list* * old, node* input, int pos)
{
common_list* temp_insert = * old;
if ( temp_insert == nullptr )
{
printf ( "传入空间错误!!!\n\n" ) ;
return ;
}
else if ( pos < 0 )
{
printf ( "位置号不能小于0" ) ;
return ;
}
else if ( pos > temp_insert-> length)
{
pos = temp_insert-> length;
}
node* current = & temp_insert-> head;
for ( int i = 0 ; i < pos; i++ )
{
current = current-> next;
}
input-> next = current-> next;
current-> next = input;
temp_insert-> length++ ;
printf ( "号码插入成功!!!\n\n" ) ;
}
node* find_list ( common_list* old, int pos)
{
common_list* temp_find = old;
if ( temp_find == nullptr )
{
printf ( "传入空间错误!!!\n\n" ) ;
return nullptr ;
}
else if ( pos < 0 || pos >= temp_find-> length)
{
printf ( "该位置处无号码!!!\n\n" ) ;
return nullptr ;
}
node* current = temp_find-> head. next;
for ( int i = 0 ; i < pos; i++ )
{
current = current-> next;
}
printf ( "找到号码啦!!!\n\n" ) ;
return current;
}
node* delete_list ( common_list* * old, int pos)
{
common_list* temp_del = * old;
if ( temp_del == nullptr )
{
printf ( "传入空间错误!!!\n\n" ) ;
return NULL ;
}
else if ( pos < 0 || pos >= temp_del-> length)
{
printf ( "该位置处无号码!!!\n\n" ) ;
return NULL ;
}
node* current = & temp_del-> head;
for ( int i = 0 ; i < pos; i++ )
{
current = current-> next;
}
node* del = current-> next;
current-> next = del-> next;
temp_del-> length-- ;
printf ( "数据删除成功!!!\n\n" ) ;
return del;
}
void clear_list ( common_list* * old)
{
common_list* temp_cls = * old;
if ( temp_cls == nullptr )
{
printf ( "传入空间错误!!!\n\n" ) ;
return ;
}
free ( temp_cls) ;
temp_cls = nullptr ;
printf ( "数据已经清空!!!\n\n" ) ;
}
int length_list ( common_list* old)
{
return old-> length;
}
typedef struct Node_stack
{
node head;
void * user_data;
} node_stack;
common_list* creat_stack ( )
{
common_list* old_stack = creat_list ( ) ;
if ( old_stack == nullptr )
{
printf ( "栈为空!!!已退出!!!\n\n" ) ;
return nullptr ;
}
return old_stack;
}
void push_stack ( common_list* * old_stack, void * user_data)
{
if ( old_stack == nullptr || user_data == nullptr )
{
printf ( "栈为空!!!或用户数据为空!!!已退出!!!\n\n" ) ;
return ;
}
common_list* temp_old = * old_stack;
node_stack* user_temp = ( node_stack* ) malloc ( sizeof ( node_stack) ) ;
user_temp-> user_data = user_data;
insert_list ( & temp_old, ( node* ) user_temp, 0 ) ;
printf ( "用户数据插入成功!!!\n\n" ) ;
}
void * get_top ( common_list* old_stack)
{
if ( old_stack == nullptr )
{
printf ( "栈为空!!!已退出!!!\n\n" ) ;
return nullptr ;
}
node_stack* temp_get = ( node_stack* ) find_list ( old_stack, 0 ) ;
if ( temp_get == nullptr )
{
printf ( "用户数据已删除完成!!!\n\n" ) ;
return nullptr ;
}
return temp_get-> user_data;
}
void pop_stack ( common_list* * old_stack)
{
if ( old_stack == nullptr )
{
printf ( "栈为空!!!已退出!!!\n\n" ) ;
return ;
}
common_list* temp_del = * old_stack;
node_stack* buf_del = ( node_stack* ) delete_list ( & temp_del, 0 ) ;
free ( buf_del) ;
buf_del = nullptr ;
printf ( "用户数据删除成功!!!\n\n" ) ;
}
int length_stack ( common_list* old_stack)
{
if ( old_stack == nullptr )
{
printf ( "栈为空!!!已退出!!!\n\n" ) ;
return - 1 ;
}
int temp_len_stack = length_list ( old_stack) ;
return temp_len_stack;
}
void cls_stack ( common_list* * old_stack)
{
if ( old_stack == nullptr )
{
printf ( "栈为空!!!已退出!!!\n\n" ) ;
return ;
}
common_list* temp_cls = * old_stack;
while ( length_stack ( temp_cls) > 0 )
{
pop_stack ( & temp_cls) ;
printf ( "栈中有元素,开始删除!!!\n\n剩余栈的长度是:【%d】\n\n" , length_stack ( temp_cls) ) ;
Sleep ( 800 ) ;
}
printf ( "元素删除完成,栈为空!!!\n\n" ) ;
}
binary_tree* left_push ( binary_tree* b1, common_list* * stack)
{
binary_tree* temp_node = b1;
common_list* temp_stack = * stack;
push_stack ( & temp_stack, temp_node) ;
if ( temp_node == nullptr )
{
printf ( "树为空!!!\n\n" ) ;
return nullptr ;
}
while ( temp_node-> left != nullptr )
{
push_stack ( & temp_stack, temp_node-> left) ;
temp_node = temp_node-> left;
}
return temp_node;
}
void show_tree_user ( binary_tree* b1)
{
if ( b1 == nullptr )
{
printf ( "树为空!!!\n\n" ) ;
return ;
}
common_list* stack = creat_stack ( ) ;
binary_tree* temp_show = left_push ( b1, & stack) ;
pop_stack ( & stack) ;
while ( temp_show)
{
printf ( "%d\n\n" , temp_show-> user_data) ;
if ( temp_show-> right)
{
printf ( "此节点还有右子树!!!\n\n" ) ;
temp_show = left_push ( temp_show-> right, & stack) ;
pop_stack ( & stack) ;
}
else if ( length_stack ( stack) > 0 )
{
temp_show = ( binary_tree* ) get_top ( stack) ;
pop_stack ( & stack) ;
}
else
{
temp_show = nullptr ;
}
}
}
int main ( )
{
binary_tree* b1 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b2 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b3 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b4 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b5 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b6 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b7 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
binary_tree* b8 = ( binary_tree* ) malloc ( sizeof ( binary_tree) ) ;
int u1 = 1111 ;
b1-> user_data = u1;
int u2 = 2222 ;
b2-> user_data = u2;
int u3 = 3333 ;
b3-> user_data = u3;
int u4 = 4444 ;
b4-> user_data = u4;
int u5 = 5555 ;
b5-> user_data = u5;
int u6 = 6666 ;
b6-> user_data = u6;
int u7 = 7777 ;
b7-> user_data = u7;
int u8 = 8888 ;
b8-> user_data = u8;
b1-> left = b2;
b1-> right = b3;
b2-> left = b4;
b2-> right = b5;
b3-> left = b6;
b3-> right = b7;
b4-> left = b8;
b4-> right = nullptr ;
b5-> left = nullptr ;
b5-> right = nullptr ;
b6-> left = nullptr ;
b6-> right = nullptr ;
b7-> left = nullptr ;
b7-> right = nullptr ;
b8-> left = nullptr ;
b8-> right = nullptr ;
show_tree_user ( b1) ;
return 0 ;
}