Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the operations of binary tree?

Shulou Source: shulou.com Published: 2022-06-02 10:51:17 10月03日 Update

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.

Tags: Number nodes content leaves skills depth knowledge height a tree recursion conciseness brevity code that is articles more articles industries information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Microsoft OPPO Reno Linux