Get the App
SLTechnology News&Howtos  ›  Development  › 

How to construct a static binary tree in C language

Shulou Source: shulou.com Published: 2022-06-01 01:44:03 09月16日 Update

This article mainly introduces the relevant knowledge of "how to build a static binary tree in c language". The editor shows you the operation process through an actual case, the operation method is simple, fast and practical. I hope this article "how to build a static binary tree in c language" can help you solve the problem.

First, the construction of trees.

Define the tree structure

Struct BTNode {char data; struct BTNode* pLChild; struct BTNode* pRChild;}

Create a simple binary tree statically

Struct BTNode* create_list () {struct BTNode* pA = (struct BTNode*) malloc (sizeof (BTNode)); struct BTNode* pB = (struct BTNode*) malloc (sizeof (BTNode)); struct BTNode* pC = (struct BTNode*) malloc (sizeof (BTNode)); struct BTNode* pD = (struct BTNode*) malloc (sizeof (BTNode)); struct BTNode* pE = (struct BTNode*) malloc (sizeof (BTNode)); pA- > data = 'Agar; pB- > data =' B' PC- > data = 'Clearing; pD- > data =' Dairy; pE- > data = 'eyed; pA- > pLChild = pB; pA- > pRChild = pC; pB- > pLChild = pB- > pRChild = NULL; pC- > pLChild = pD; pC- > pRChild = NULL; pD- > pLChild = NULL; pD- > pRChild = pE; pE- > pLChild = pE- > pRChild = NULL; return pA;}

Second, three kinds of traversal of trees

1. Preorder traversal

/ / pre-output void PreTravense (struct BTNode* pHead) {if (null = pHead) {printf ("% c", pHead- > data); if (null = pHead- > pLChild) {PreTravense (pHead- > pLChild);} if (null = pHead- > pRChild) {PreTravense (pHead- > pRChild);}

two。 Mid-order traversal

/ / output void InTravense (struct BTNode* pHead) {if (NULL! = pHead) {if (NULL! = pHead- > pLChild) {PreTravense (pHead- > pLChild);} printf ("% c", pHead- > data); if (NULL! = pHead- > pRChild) {PreTravense (pHead- > pRChild);}

3. Subsequent traversal

/ output void PostTravense (struct BTNode* pHead) {if (NULL! = pHead) {if (NULL! = pHead- > pLChild) {PreTravense (pHead- > pLChild);} if (NULL! = pHead- > pRChild) {PreTravense (pHead- > pRChild);} printf ("% c", pHead- > data);}}

Third, finally run the test

Int main () {printf ("create sequence\ n"); struct BTNode* pHead = create_list (); printf ("first order output\ n"); PreTravense (pHead); printf ("medium order output\ n"); InTravense (pHead); printf ("Post order output\ n"); PostTravense (pHead); return 0;}

This is the end of the introduction to "how to build a static binary tree in c language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Output static language knowledge industry different practical content practical practical sequence article way method more case knowledge point article structure information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Redmi Apple Huawei