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 realize static chain binary tree in C language version

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to realize static chain binary tree in c language version". Many people will encounter such a dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

# include

# include

Typedef struct Node {

Char data

Struct Node * pLchild

Struct Node * pRchild

} NODE,* PNODE

PNODE create_binTree (void)

Void pre_traver (PNODE)

Void in_traver (PNODE)

Void post_traver (PNODE)

Void main (void) {

PNODE pNode = create_binTree ()

/ / preorder traversal

/ / pre_traver (pNode)

/ / traversal in the middle order

/ / in_traver (pNode)

/ / Post-order traversal

Post_traver (pNode)

}

PNODE create_binTree (void) {

PNODE pA = (PNODE) malloc (sizeof (NODE))

PNODE pB = (PNODE) malloc (sizeof (NODE))

PNODE pC = (PNODE) malloc (sizeof (NODE))

PNODE pD = (PNODE) malloc (sizeof (NODE))

PNODE pE = (PNODE) malloc (sizeof (NODE))

PA- > data ='A'

PA- > pLchild = pB

PA- > pRchild = pC

PB- > data ='B'

PB- > pLchild = NULL

PB- > pRchild = NULL

PC- > data ='C'

PC- > pLchild = pD

PC- > pRchild = NULL

PD- > data ='D'

PD- > pLchild = NULL

PD- > pRchild = pE

PE- > data ='E'

PE- > pLchild = NULL

PE- > pRchild = NULL

Return pA

}

/ / preorder traversal

Void pre_traver (PNODE pNode) {

/ *

Idea: 1. Visit the root node first.

2. Traverse the left subtree first

3. Traverse the right subtree first

, /

If (NULL! = pNode) {

Printf ("% c\ n", pNode- > data)

If (NULL! = pNode- > pLchild) {

Pre_traver (pNode- > pLchild)

}

If (NULL! = pNode- > pRchild) {

Pre_traver (pNode- > pRchild)

}

}

}

/ / traversal in the middle order

Void in_traver (PNODE pNode) {

If (NULL! = pNode) {

If (NULL! = pNode- > pLchild) {

In_traver (pNode- > pLchild)

}

Printf ("% c\ n", pNode- > data)

If (NULL! = pNode- > pRchild) {

In_traver (pNode- > pRchild)

}

}

}

/ / Post-order traversal

Void post_traver (PNODE pNode) {

If (NULL! = pNode) {

If (NULL! = pNode- > pLchild) {

Post_traver (pNode- > pLchild)

}

If (NULL! = pNode- > pRchild) {

Post_traver (pNode- > pRchild)

}

Printf ("% c\ n", pNode- > data)

}

}

This is the end of the content of "how to realize the static chain binary tree in C language version". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 212

*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