Binary tree creation and correlation algorithm

Source: Internet
Author: User
Tags assert sca

Binary tree is a very important data structure, it is the basis of the branch structure, today I will write a blog to describe its related algorithms and two tree creation process!

1: Two fork Tree creation:

There are several ways to create sequence, sequence, order and sequence, among which the first three are built on the basis of the binary tree traversal method.

(1): First order creation

First order creation is to create the root node, and then create its Saozi right subtree, we can use recursive method to achieve, because the binary tree itself is built on the basis of recursive algorithm.

(2): Preface and post-order creation:

Understand the former order to create a two-tree way, the sequence and follow-up we can also analogy, the so-called pre-order, is to create the root node sequence.

(3): Sequence Traversal and creation:


The process of sequence traversal, reflected in slices, sequence traversal is built on the basis of the queue, first if a binary tree is not empty, let the root node queue, if the root node of the left subtree after the right subtree is not empty, then the root node out of the team and print its data field value, left and right sub-tree queue (who is not empty who team), followed by a recursive process.

Visible sequence traversal is a traversal process from top to bottom, left to right.

So we can also create a binary tree: the root node is created first, and if the left child and right child pointers of the root node do not point to the virtual node, the Saozi right subtree is created and is created recursively.


Binary Tree Code:


function file bittree.c:

#include "Bittree.h" #include <stdio.h> #include <stdlib.h> #include <assert.h>tree *creat (tree* root , tree* (*ptree) (tree *root)) {root = Ptree (root); return root;}       Tree *tier_creat (tree* root)//sequence Create two fork tree root for root node {tree *s = Null;datatype data = 0;tree *tree_arr[max] = {0};                     Create a queue that stores binary tree nodes int front = 1;                      Front is the initial value of the queue pointer int rear = 0; Rear for the end of the queue pointer to the initial value of printf ("The number of layers creates a two fork tree, 1 represents the virtual node,-2 means the end!") \ n "); scanf ("%d ", &data), while (data! =-2) {s = null;if (data! = 1) {s = (tree*) malloc (sizeof (tree) * 1); s->data = da    Ta;s->lchild = Null;s->rchild = NULL; }rear++;           Tree_arr[rear] = s;      The parent node is enqueued if (rear = = 1) root = s; If rear is a team head pointer, the root node of the two fork tree is stored in its queue else{if (Tree_arr[rear] && s)//child and both parents are not virtual nodes {if (rear% 2 = 0) Tree_arr[front]        ->lchild = s;    The new node is the left child if (rear% 2 = = 1) {Tree_arr[front]->rchild = s;                        New node is right child front++;    If this is the time rear refers to the right child, then out of the team, the next node into the team}}}scanf ("%d", &data); }rEturn Root;} Tree *dlr_creat (tree* root)///First order Create two fork tree {printf ("with 1 indicates that the pointer field is empty, and 2 means the end of the creation process \ n"); root = Null;int data = 0;printf ("Please enter the information:"); SCA    NF ("%d", &data);    if (data = =-1) return NULL; Root = (tree*) malloc (sizeof (tree) * 1); if (root = = NULL) {printf ("Create failed! \ n "); exit (Exit_failure); } root->data = data;     Root->lchild=dlr_creat (Root->lchild);     Root->rchild = Dlr_creat (root->rchild); return root;} Tree *ldr_creat (tree* root)//In order to create a two-fork tree {printf ("with 1 indicates that the pointer field is empty, and 2 means the end of the creation process \ n"); root = Null;int data = 0;printf ("Please enter the information:"); SCA NF ("%d", &data), if (data = =-1) return null;root->lchild = Dlr_creat (root->lchild); root = (tree*) malloc (sizeof (tree) * 1); if (root = NULL) {printf ("Create failed! \ n "); exit (exit_failure);} Root->data = Data;root->rchild = Dlr_creat (Root->rchild); return root;} Tree *rld_creat (tree* root)//post-order Create a two-fork tree {printf ("with 1 indicates that the pointer field is empty, and 2 means the end of the creation process \ n"); root = Null;int data = 0;printf ("Please enter the information:"); SCA    NF ("%d", &data); if (data = =-1) return NULL; Root->lchild = DLR_CREat (root->lchild); root->rchild = Dlr_creat (root->rchild); root = (tree*) malloc (sizeof (tree) * 1); if (root = = NULL) {printf ("Create failed! \ n "); exit (exit_failure);} Root->data = Data;return root;} void Print_tree (Tree *root, Void (*ptree) (tree *root))//Traversal binary tree {assert (root), if (root = NULL) {printf ("Tree is empty tree! \ n "); return;} Else{ptree (Root);p rintf ("\ n");}}    void Dlr_print (tree *root)///sequence traversal binary tree {if (root! = NULL) {printf ("%d", root->data);    Dlr_print (Root->lchild); Dlr_print (root->rchild);} return;} void Ldr_print (tree *root)//middle order traversal binary tree {if (root! = NULL) {dlr_print (root->lchild);p rintf ("%d", Root->data);D lr_p Rint (root->rchild);} return;} void Rld_print (tree *root)//post-traversal binary tree {if (root! = NULL) {dlr_print (root->lchild);D lr_print (root->rchild);p rintf ( "%d", Root->data);} return;} void Tie_print (tree *root)//sequence traversal binary tree {tree* Arry[max] = {0};int rear = 1;int Front = 0;if (root = = NULL) {Prin TF ("Binary tree is empty!") \ n "); return;}                    Else{arry[0] = root; //If the binary tree is not empty, the root node is first queued. while (front < rear)//Cyclic condition queue non-empty {if (Arry[front]) {printf ("%d", arry[front]->data); arry[rear++] = AR ry[front]->lchild;arry[rear++] = arry[front]->rchild;front++;} else{front++;}}}} int Deep_tree (tree *root)//Find binary tree Depth {assert (root); int left = 0;int right = 0;int deep = 0;if (root = NULL) {left=deep_t             Ree (root->lchild);          Find left subtree depth right = Deep_tree (Root->rchild); Seek right subtree depth deep = left > R? (left + 1): (right + 1);} return deep;} int Node_du (tree *root)//The degree {assert (root) of a node, int ret = 0;if (root = null) return 0;else{if (root->lchild! = null ) ret++;if (Root->rchild! = NULL) ret++;return ret;}} int Tree_node (tree *root) {assert (root); int ret = 0;if (root = NULL) return 0;if (Root->lchild = = Null&&root-&gt ; Rchild = = NULL)//leaf node return 1;ret =1+ Tree_node (root->lchild) + Tree_node (root->rchild); return ret;} tree* Search_node (tree *root, datatype data)//Find node with value of data {assert (Root); tree *p=null;if (root = null) {printf ("The tree is an empty tree!") \ n "); return NULL;} Else{if (Root->data = = data) return root;if (root->lchild! = NULL) {p = Search_node (root->lchild,data); if (P! = NUL L) return p;} if (root->rchild! = null) {p = Search_node (root->rchild,data); if (P! = null) return p;}   return NULL; Not found return empty}}void deal_tree (tree *root)//release memory {assert (root), if (root = NULL) Return;deal_tree (root->lchild);D Eal_ Tree (root->rchild); free (root); root = NULL;}
header File Bittree.h

#ifndef __bittree_h__#define __bittree_h__#define _crt_secure_no_warnings#define MAX 100typedef int datatype;typedef struct Bittree{datatype data;struct bittree *lchild;struct bittree *rchild;} Tree;typedef struct{datatype elem_data[max];int length;}   Tree_arry;tree *tier_creat (tree* root);    The sequence creates a two-fork tree root for the root node tree *dlr_creat (tree* root);    The first order creates a two-fork tree *ldr_creat (tree* root);    The middle order creates a two-fork tree *rld_creat (tree* root);     Post-creation two tree *creat (tree* root,tree* (*ptree) (tree *root));  Create two fork tree void Print_tree (tree *root, Void (*ptree) (tree *root));      Traverse the binary tree void Dlr_print (tree *root);      The first sequence traverses the binary tree void Ldr_print (tree *root);      The middle sequence traverses the binary tree void Rld_print (tree *root);      Sequential traversal of binary tree void Tie_print (tree *root);       Sequence traversal binary tree int deep_tree (tree *root);          Find the depth of the binary tree int node_du (tree *root);       the degree int tree_node (tree *root) of a node is asked;                The number of nodes in the tree tree* search_node (tree *root, datatype data);       Find a node with value of data void Deal_tree (tree *root); Release Memory #endif __bittree_h__

Test file test.c:

#include "Bittree.h" #include <stdio.h> #include <stdlib.h>void Init () {printf ("1: two fork tree creation \ n");p rintf ("2: Binary tree traversal \ n ");p rintf (" 3: two tree depth query \ n ");p rintf (" 4: Two the query of the degree of a node of the fork tree \ n ");p rintf (" 5: Empty the binary tree \ n ");p rintf (" 0: Exit \ n ");} void Init_print () {printf ("0: Sequence traversal binary tree. \ n");p rintf ("1: Sequential traversal of binary tree. \ n");p rintf ("2: Post-order traversal of binary tree. \ n");p rintf ("3: Middle sequence traversal binary tree. \ n");} void Init_creat () {printf ("0: Sequence creation two fork tree. \ n");p rintf ("1: First order to create two fork tree. \ n");p rintf ("2: Post-sequence create two-fork tree. \ n");p rintf ("3: middle order Create two fork tree. \ n");} void Test () {int ret;int key=0;int choose;datatype data=0;tree *root = Null;tree *node = null;tree* (*tree_c[4]) (Tree *roo   T) = {0};   A function pointer array holds a two-fork tree created in Void (*tree_p[4]) (tree *root) = {0}; The function pointer array holds the traversal mode of the two-fork tree tree_c[0] = tier_creat; TREE_C[1] = dlr_creat; TREE_C[2] = ldr_creat; TREE_C[3] = rld_creat; Tree_p[0] = Tie_print; TREE_P[1] = Dlr_print; TREE_P[2] = Ldr_print; TREE_P[3] = dlr_print;while (1) {printf ("Please enter your choice:"), scanf ("%d", &key), switch (key) {Case 0:printf ("program over!! \ n "), exit (Exit_failure), Break;case 1:init_creat ();d o{printf (" Please enter your choice: "); scanf ("%d ", &choose);} while (choose <= 0 && Choose >= 3), Root = creat (root, Tree_c[choose]), Break;case 2:init_print ();d o{printf (" Please enter your choice: "); scanf ("%d ", &choose);} while (choose <= 0 && Choose >= 3); Print_tree (Root, Tree_p[choose]), break;case 3:ret = Deep_tree (root);p rintf ("Binary Tree Depth:%d\n", ret); Break;case 4:printf ( "Please enter the data you want to query the node for:"), scanf ("%d", &data), Node=search_node (root, data), if (node = = NULL) {printf ("The nodes you are querying do not exist!"). \ n "); exit (exit_failure);} ret = NODE_DU (node);p rintf ("The degree of nodes with data%d is:%d\n", data, ret) break;case 5:deal_tree (Root);p rintf ("Empty successfully! \ n "); break;}} int main () {Init (); test (); System ("pause"); return 0;}

If the above description has related questions please can point out, I e-mail: [Email protected]

Binary tree creation and correlation algorithm

Contact Us

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.