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

What are the operations of binary tree?

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

Share

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

This article shows you what the operation of binary tree is, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Previously implemented the creation of binary tree, non-recursive traversal and recursive traversal. Now add some other actions, including:

Destroy a tree

Calculate the depth (height) of the tree

. Calculate the number of leaf nodes

Calculate the number of all nodes

Copy binary tree

For more information, see the code:

# include # include typedef struct Node {int data; struct Node* lchild; struct Node* rchild;} Node;// creation tree Node* create_tree () {int _ data; scanf ("% d", & _ data); if (_ data==100) {return NULL;} Node* root= (Node*) malloc (1*sizeof (Node)); if (root==NULL) {return;} root- > data=_data Root- > lchild=create_tree (); root- > rchild=create_tree (); return root;} / / preorder traversal void pre_print (Node* root) {if (root==NULL) {return;} printf ("% d\ t", root- > data); pre_print (root- > lchild); pre_print (root- > rchild) Destroy a tree void free_tree (Node* root) {if (root==NULL) return; free_tree (root- > lchild); free_tree (root- > rchild); free (root); root=NULL } / / calculate the depth (height) of the tree int depth (Node* root) {if (root==NULL) return 0; int l=depth (root- > lchild); int r=depth (root- > rchild); if (l > r) {return lenses 1;} return r nodes;} / / calculate the number of leaf nodes int count_leaf (Node* root) {if (root==NULL) {return 0 } if (root- > lchild==NULL & & root- > rchild==NULL) {return 1;} else {return count_leaf (root- > lchild) + count_leaf (root- > rchild);}} / / calculate the number of all nodes int count_all_node (Node* root) {if (root==NULL) {return 0;} return count_all_node (root- > lchild) + count_all_node (root- > rchild) + 1 } / / copy binary tree Node* copy_tree (Node* root) {if (root==NULL) {return NULL;} Node* lager tree; l_tree=copy_tree (root- > lchild); r_tree=copy_tree (root- > rchild); root_tree=malloc (1*sizeof (Node)); root_tree- > data=root- > data; root_tree- > lchild=l_tree; root_tree- > rchild=r_tree Return root_tree;} int main (int argc, char const * argv []) {Node* root=create_tree (); pre_print (root); printf ("\ n"); printf ("Depth:%d\ n", depth (root)); printf ("count_leaf:%d\ n", count_leaf (root)); printf ("count_all_node:%d\ n", count_all_node (root)) Node* copy_root=copy_tree (root); pre_print (root); printf ("\ n"); return 0;} what are the operations of binary trees? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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