An improved _c language for binary tree root (first order) traversal

Source: Internet
Author: User

Binary tree features: The maximum degree of each node can not exceed 2, and the left and right subtree can not be reversed

Binary tree Storage structure: The following using chain storage for elaboration, heap sorting algorithm (rapid sorting improved) using the sequential storage structure of the two-fork tree, first look at the following structure storage mode

Sequential storage:

Copy Code code as follows:

/* Two-fork Tree sequential Storage * *
#define MAX_TREE_SIZE 100
typedef telemtype SQBITREE[MAX_TREE_SIZE];

Chained storage:

Copy Code code as follows:

/* Two-fork tree chain Storage * *
typedef struct BITNODE
{
Telemtype data;
Bitnode *lchild,*rchild;
}
Bitnode, *bitree;

The type of Telemtype here is as follows: Here I use the definition of int.

Copy Code code as follows:

#define Int_type
#ifdef Int_type
typedef int TELEMTYPE;
#elif defined Float_type
typedef float TELEMTYPE
#elif defined String_type
typedef char *TELEMTYPE
#endif

Binary Tree creation: Here you need to create recursively, as follows

Copy Code code as follows:

int Createbitree (Bitree &t)
{
int ndata;

printf ("Please Enter bitree Node data:\n");
scanf_s ("%d", &ndata);

if (ndata = 0)
{
T = NULL;
return OK;
}
else if (ndata > 0 && ndata < 100)
{
T = (bitnode*) malloc (sizeof (Bitnode));
if (! T
{
return btoverflow;
}
T->data = Ndata;
Createbitree (T->lchild);
Createbitree (T->rchild);
return OK;
}
#ifdef _DEBUG
printf ("Enter Data error! \ n ");
#endif//_DEBUG

return ERROR;
}

Here I have limited the data to allow for testing, here only accept 0~100 data, if it is 0 indicates that there is no child currently node (left child or right child), if there is a node to create, fill data
Traversing the binary tree: After creation, I must test the data in the two-fork tree that is created is correct, here is the root traversal, as follows

Copy Code code as follows:

/* Traverse the binary tree * *
int Preordertraverse (bitree T, int (*visitnode) (Telemtype))
{
if (T)
{
if (Visitnode (T->data))
{
if (Preordertraverse (T->lchild, Visitnode))
{
if (Preordertraverse (T->rchild, Visitnode))
{
return OK;
}
}
}
return ERROR;
}
return OK; If you don't have children, you should backtrack, to see that the right child must be true
}

This is the time to traverse the use of a function, note that the passing of the parameters are function pointers, but only for simple node data output, as follows:

Copy Code code as follows:

int Visitnode (Telemtype data)
{
#if defined (int_type) | | Defined (Float_type)
printf ("%d", data);
#elif defined (string_type)
printf ("%s", data);
#endif
return OK;
}

But at the time of traversal, why T is null, return or OK (1), this is mainly the reason for the above traversal function, because this must be the first traversal of the left child and the return value is true, in order to traverse the right child, so can not return error (0), feel the return value is a bit strange, modify the following

Copy Code code as follows:

int Preordertraverseex (bitree T, int (*visitnode) (Telemtype))
{
if (T)
{
if (Visitnode (T->data))
{
Preordertraverse (T->lchild, Visitnode);
Preordertraverse (T->rchild, Visitnode);
return OK;
}
}
return ERROR; If you don't have children, you should backtrack.
}

This looks more comfortable, in fact, you can not use any return value, mainly through the left and right subtree do not have to do anything else, if there are other, you can not return the value, here returns OK actually do not matter, because I did not judge
Test case: as follows

Copy Code code as follows:

Bitree T;
if (Createbitree (T))
{
Preordertraverseex (T, Visitnode);
printf ("\ n");
}

Here to test the data input in fact has a certain requirement, assuming that the root for 12 children node is 34 78, this should be the input data 12 34 0 0 78 0 0, the only way to normal exit, the following test results:
Please Enter bitree Node data:
12
Please Enter bitree Node data:
34
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
Please Enter bitree Node data:
78
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
12 34 78

I can see from the front that this is actually a recursive algorithm, we all know that the biggest drawback of recursion is that each time you dive down the next level, be sure to save the current level of data, which data is actually managed by the operating system OS, but like every time the formal parameter T will be in the stack, It is easy to use when the level exit returns to the previous layer, so this can be done in a non recursive way to modify the algorithm: How to do?

Please refer to the non-recursive algorithm for binary tree first-order traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.