Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to construct a static binary tree in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report