# Several operations on Binary Trees

Source: Internet
Author: User

Binary Tree, classic data structure, widely used

Generally, the focus of a binary tree is traversal, because almost all operations on a binary tree can be solved through traversal, while traversing a binary tree (first root, middle root, and back root) it also has strong applicability (such as mathematical expression evaluation)

The following describes the conventional recursive method, which has obvious advantages,CodeIt is easy to understand. The depth, number of nodes, and number of leaf nodes in the Code also use recursive methods.

`/*************************************** * ****** Common Binary Tree operations: traversal (previous and later, and layer-by-layer traversal) is the key of Binary Trees, other operations can be completed through traversal ******************************** * *************/# include <stdio. h> # include <malloc. h> struct btreenode {char data; struct btreenode * left; struct btreenode * Right ;}; typedef struct btreenode btnode, * btree; /*************************************** it is equivalent to defining (struct btreenode) and (struct btreenode *). Tree (here typedef plays a role in hiding pointer operations) the struct declaration can also be written like this: typedef struct btreenode {char data; struct btreenode * left; struct btreenode * right;} btnode, * btree; **************************************** /// initialize the binary tree void initbtree (btree * btree) {* btree = NULL; return;} // construct a binary tree void createbtree (btree & root) {char data; data = getchar (); If (Data = '0 ') {root = NULL;} else {root = (btree) malloc (sizeof (btnode); root-> DATA = Data; createbtree (root-> left); createbtree (root-> right); }}// determines whether the binary tree is empty. Int emptybtree (btree) {If (btree = NULL) return 1; return 0 ;}// Binary Tree depth int btreedepth (btree) {If (btree = NULL) {return 0 ;} else {int leftdepth = btreedepth (btree-> left); int rightdepth = btreedepth (btree-> right); If (leftdepth> rightdepth) return leftdepth + 1; else return rightdepth + 1 ;}// Number of Binary Tree nodes int btreenode (btree BT REE) {If (btree = NULL) return 0; else return btreenode (btree-> left) + btreenode (btree-> right) + 1 ;} // Number of leaf nodes in a binary tree int btreeleafnode (btree) {If (btree = NULL) return 0; else if (btree-> left = NULL & btree-> right = NULL) return 1; else return btreeleafnode (btree-> left) + btreeleafnode (btree-> right);} // first traverse void preorder (btree) by root // void preorder (struct btreenode ** btree) {If (btree! = NULL) {printf ("% C", btree-> data); preorder (btree-> left); preorder (btree-> right);} return ;} // The root traversal void inorder (btree) {If (btree! = NULL) {inorder (btree-> left); printf ("% C", btree-> data); inorder (btree-> right);} return ;} // The back root traverses void postorder (btree) {If (btree! = NULL) {postorder (btree-> left); postorder (btree-> right); printf ("% C", btree-> data);} return ;} int main () {btree; initbtree (& btree); // puts ("input: abd000ce00f00"); puts ("input: 12300400500 "); /******************** 1/\ 25/\ 34 *********** * *********/createbtree (btree ); int nodes = btreenode (btree); printf ("Number of Binary Tree nodes: % d \ n", nodes); int leafnodes = btreeleafnode (btree); printf ("Number of binary tree leaf nodes: % d \ n ", leafnodes); puts (" "); puts (" first root traversal: "); preorder (btree); puts (""); puts (""); puts ("root traversal:"); inorder (btree); puts (""); puts (""); puts ("root traversal: "); postorder (btree); puts (" "); puts (" "); Return 0 ;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.