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 the traversal method of binary Tree in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

In this algorithm, the tree is created by preorder traversal, and the recursive algorithm is used to make the algorithm simple and easy to operate. there is no statement of printf ("left / right subtree of% c:", ch), but because the computer needs to input space characters to judge the left and right subtree, in order to reduce the error of human input, this statement is added to ensure the accuracy.

# include#include#define OK 1#define ERROR 0#define OVERFLOW 3 typedef int Status; typedef int Boolean; typedef char TElemType; typedef struct BiTNode {TElemType data; struct BiTNode * lchild, * rchild;} BiTNode, * BiTree; / / create binary tree function Status CreateBiTree (BiTree & T) {TElemType ch; scanf ("% c", & ch); getchar (); if (ch = =') {T = NULL } else {if (! (T = (BiTree) malloc (sizeof (BiTNode) (exit (OVERFLOW)); T-> data = ch; printf ("left subtree of% c:", ch); CreateBiTree (T-> lchild) Printf ("right subtree of% c:", ch); CreateBiTree (T-> rchild);} return OK } / / preorder traversal function Status PreOrderTraverse (BiTree T, Status (* Visit) (TElemType e)) {if (T) {if (Visit (T-> data)) {if (PreOrderTraverse (T-> lchild, Visit)) {if (PreOrderTraverse (T-> rchild, Visit)) {return OK } return ERROR;} else {return OK }} / / the order traversal function Status InOrderTraverse (BiTree T, Status (* Visit) (TElemType e)) {if (T) {if (PreOrderTraverse (T-> lchild, Visit)) {if (Visit (T-> data)) {if (PreOrderTraverse (T-> rchild, Visit)) {return OK } return ERROR;} else {return OK }} / / Post-order ergodic function Status PosOrderTraverse (BiTree T, Status (* Visit) (TElemType e)) {if (T) {if (PreOrderTraverse (T-> lchild, Visit)) {if (PreOrderTraverse (T-> rchild, Visit)) {if (Visit (T-> data)) {return OK } return ERROR;} else {return OK;}} / / output binary tree function Status PrintElement (TElemType e) {printf ("% c", e); return OK;} / / main function int main () {BiTree T; printf ("input root node:"); CreateBiTree (T) Printf ("preorder traversal:\ n"); PreOrderTraverse (T, PrintElement); printf ("\ n"); printf ("medium order traversal:\ n"); InOrderTraverse (T, PrintElement); printf ("\ n"); printf ("postorder traversal:\ n"); PosOrderTraverse (T, PrintElement); return 0;}

There are four kinds of traversal operations, and the difference is that the access order to the root node is different. In the preorder traversal, the root node is visited first, and then the left subtree is recursively traversed, and then the right subtree is recursively traversed. In the middle order traversal, the left subtree is iterated recursively, the root node is accessed, and the right subtree is iterated recursively. In the post-order traversal, the left and right subtrees are iterated recursively, and then the root node is accessed. First-order, middle-order, and post-order traversal is the order of access to the root node.

But no matter which way of traversal, the recursive method is the simplest, most direct and simplest algorithm.

Run the screenshot:

This is the end of the content about "how to realize the traversal method of C language binary tree". 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