8608 various algorithms to achieve binary sort tree
Description
Realize a binary sort tree algorithm functions: (1) Insert a new node (2) the preamble, in sequence, after the binary tree traversal Non-recursive algorithm (3) in preorder (4) level binary tree traversal (5) Find a binary tree for a given keyword (function returns a value of success 1, failure 0) (6) each switching node left and right subtrees (7) binary tree of depth (8) leaf node points
Input
The first line: the number of nodes ready contribution n The second line: the input n integers separated by spaces Third row: Enter a keyword to be looking for Fourth row: Enter a keyword to be looking for Fifth row: Enter a keyword to be inserted
Output
First line: binary tree traversal sequence preorder Second line: in order binary tree traversal sequence Third row: The sequence of binary tree traversal sequence Fourth row: Search Results Fifth row: Search Results Sixth to eighth row lines: the first binary tree after inserting new node, in sequence preorder Ninth line: the sequence of binary tree after inserting new node traversal sequence (non-recursive algorithm) Tenth row: level binary tree after inserting new node traversal sequence Thirteenth eleventh line ~ line: first switching node to the respective left and right sub-tree after, during and after the sequence preorder Line 14 ~ 16th line: first second switching node after the respective left and right sub-tree, in the sequence preorder Seventeenth line: binary tree of depth Eighteenth-line: the number of nodes leaves
Sample Input
7 40 20 60 18 50 56 90 18 35 30
Sample Output
40 20 18 60 50 56 90 18 20 40 50 56 60 90 18 20 56 50 90 60 40 1 0 40 20 18 30 60 50 56 90 18 20 30 40 50 56 60 90 18 30 20 56 50 90 60 40 18 20 30 40 50 56 60 90 40 20 60 18 30 50 90 56 40 60 90 50 56 20 30 18 90 60 56 50 40 30 20 18 90 56 50 60 30 18 20 40 40 20 18 30 60 50 56 90 18 20 30 40 50 56 60 90 18 30 20 56 50 90 60 40 4 4
// The following is the code AC
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define Maxsize 100
typedef int TElemType;
typedef int status;
typedef struct BTree
{
TElemType data;
struct BTree *lchild,*rchild;
}BTnode,*BTpoint;
typedef struct stack
{
BTpoint *base,*top;
int stacksize;
}Stack;
typedef struct quence
{
BTpoint *front,*rear;
int quencesize;
}Quence;
status Creat_stack(Stack &S)
{
if(!(S.base=(BTpoint *)malloc(Maxsize * sizeof(BTpoint)))) return ERROR;
S.top=S.base;
S.stacksize=Maxsize;
return OK;
}
status Creat_quence(Quence &Q)
{
if(!(Q.front = (BTpoint *)malloc(Maxsize * sizeof(BTpoint)))) return ERROR;
Q.rear=Q.front;
Q.quencesize=Maxsize;
return OK;
}
status Creat_and_insert(BTpoint &T,TElemType x)
{
if(T == NULL)
{
if(!(T = (BTpoint)malloc(sizeof(BTnode)))) return ERROR;
else
{
T->data = x;
T->lchild = T->rchild = NULL;
}
}
else
{
if(x<T->data)
return Creat_and_insert(T->lchild,x);
else return Creat_and_insert(T->rchild,x);
}
return OK;
}
status Print_tree_data(TElemType e)
{
printf("%d ",e);
return OK;
}
status Firt_view_root (BTpoint T, status (* view) (TElemType e)) // preorder
{
if(T!=NULL)
{
if(Print_tree_data(T->data))
if(Firt_view_root(T->lchild,view))
if(Firt_view_root(T->rchild,view)) return OK;
return ERROR;
}
else return OK;
}
status Mid_view_root (BTpoint T, status (* view) (TElemType e)) // preorder
{
if(T!=NULL)
{
if(Mid_view_root(T->lchild,view))
if(Print_tree_data(T->data))
if(Mid_view_root(T->rchild,view)) return OK;
return ERROR;
}
else return OK;
}
status Last_view_root (BTpoint T, status (* view) (TElemType e)) // After preorder
{
if(T!=NULL)
{
if(Last_view_root(T->lchild,view))
if(Last_view_root(T->rchild,view))
if(Print_tree_data(T->data)) return OK;
return ERROR;
}
else return OK;
}
status Find_data(BTpoint T,TElemType findit) //查找
{
if(T!=NULL)
{
if(findit == T->data) return 1;
else if(findit < T->data) return Find_data(T->lchild,findit);
else return Find_data(T->rchild,findit);
}
else return 0;
}
void viewall(BTpoint T,status (*view)(TElemType e))
{
Firt_view_root(T,Print_tree_data);
printf("\n");
Mid_view_root(T,Print_tree_data);
printf("\n");
Last_view_root(T,Print_tree_data);
printf("\n");
}
status M_nonrecursive (BTpoint T, Stack S) // sequence preorder (non-recursive algorithm)
{
while(T!=NULL||S.base!=S.top)
{
while(T!=NULL)
{
*S.top++=T;
T=T->lchild;
}
T=*--S.top;
Print_tree_data(T->data);
T=T->rchild;
}
return OK;
}
status Level_view (BTpoint T, Quence Q) // traverse the level
{
if(T!=NULL)
{
*Q.rear++=T;
while(Q.front!=Q.rear)
{
if(T->lchild!=NULL) *Q.rear++=T->lchild;
if(T->rchild!=NULL) *Q.rear++=T->rchild;
T=*Q.front++;
printf("%d ",T->data);
T=*Q.front;
}
}
return OK;
}
status swap_tree(BTpoint &T)
{
BTpoint temp;
if(T!=NULL)
{
temp = T->lchild;
T->lchild = T->rchild;
T->rchild = temp;
swap_tree(T->lchild);
swap_tree(T->rchild);
}
return OK;
}
status tree_deep (BTpoint T) // binary tree depth
{
int ld=0,rd=0;
if(T!=NULL)
{
ld = tree_deep(T->lchild);
rd = tree_deep(T->rchild);
}
else return 0;
return ld>rd?ld+1:rd+1;
}
status leaf_number (BTpoint T, int & num) // find the total number of leaves
{
if(T)
{
if(T->rchild==NULL && T->lchild==NULL) num++;
else
{
leaf_number(T->lchild,num);
leaf_number(T->rchild,num);
}
}
return OK;
}
int main ()
{
BTpoint BT=NULL;
Stack S;
Quence Q;
int n,i,fnb1,fnb2,isnb;
int num=0,deep;
int a[Maxsize];
scanf ( "% d", & n); // The first line: the number of input nodes preparing contribution n
for (i = 0; i <n; i ++) // Second row: the input n integers separated by spaces
{
scanf("%d",&a[i]);
Creat_and_insert(BT,a[i]);
}
scanf ( "% d", & fnb1); // Third row: Enter a keyword to be looking for
scanf ( "% d", & fnb2); // Fourth row: Enter a keyword to be looking for
scanf ( "% d", & isnb); // fifth row: the input key to be inserted
viewall(BT,Print_tree_data);
printf("%d\n",Find_data(BT,fnb1));
printf("%d\n",Find_data(BT,fnb2));
// insert and insert after
Creat_and_insert(BT,isnb);
viewall(BT,Print_tree_data);
// 9 and output on line 10
Creat_stack(S);
M_nonrecursive(BT,S);
printf("\n");
Creat_quence(Q);
Level_view(BT,Q);
printf("\n");
// first exchange
swap_tree(BT);
@ 11 13 ~ line: first exchange to the left and right subtrees of each node, in the sequence preorder
viewall(BT,Print_tree_data);
// second exchange
swap_tree(BT);
// lines 14 to 16: first the left and right subtrees of each second switching node, in the sequence preorder
viewall(BT,Print_tree_data);
// line 17, binary tree of depth
deep = tree_deep(BT);
printf("%d\n",deep);
// Line 18, the total number of leaf node point
leaf_number(BT,num);
printf("%d\n",num);
return 0;
}
Reproduced in: https: //www.cnblogs.com/arcfat/archive/2012/10/20/2732150.html