Two-fork Tree

Source: Internet
Author: User

Binary trees (binary tree) is another tree structure, it is characterized by a node at most only two subtrees tree (that is, the binary tree does not exist in the degree of more than 2 nodes), and the binary tree words from the left and right points, the order can not be reversed.

Binary tree Storage structure: 1. Sequential storage structure (for full binary tree only) 2. Chained storage structure

Traversal of a binary tree:

The operations of the first-order traversal binary tree are defined as:

If the binary tree is empty, then empty operation;

(1) Access to root node

(2) First Order traversal Zuozi

(3) First order traversal of right sub-tree

The operation of the middle sequence traversal binary tree is defined as:

If the binary tree is empty, then empty operation;

(1) Middle sequence traversal Zuozi

(2) Access to root node

(3) The middle sequence traverses the right sub-tree

The operation of traversing a binary tree is defined as:

If the binary tree is empty, then empty operation;

(1) post-Zuozi traversal

(2) to traverse the right sub-tree

(3) Access to root node

//two fork tree. CPP: Defines the entry point of the console application. //  #include"stdafx.h"#include<iostream>#include<cstdlib>#include<queue>#include<malloc.h>/** Input a binary tree with a sequential traversal and complete the establishment of a two-fork tree and the creation of the sequence, sequence, order, and level of traversal of the binary tree node height * Reference data Structure C language version (Min)*/#include<string>#include<cstring>#include<algorithm>using namespacestd; //Defining NodestypedefstructBitnode {Chardata; structBitnode *lchild,*Rchild; }bitnode,*Bitree; voidCreatebitree (Bitree &T) {//Two establishment of a fork tree    Charch; //according to the value of the node in the binary tree (one character), ' @ ' indicates that the node is empty .//constructs a two-fork tree represented by a two-fork list TCH =GetChar (); if(ch=='@') T=NULL; Else{T=NewBitnode; T->data =ch; Createbitree (T-lchild); Createbitree (T-rchild); }  }    voidPreordertraverse (Bitree T) {//First Order Traversal    if(T) {cout<< t->data; Preordertraverse (T-lchild); Preordertraverse (T-rchild); }  }    /*how to ask Hovertree.com*/voidInordertraverse (Bitree T) {//Middle Sequence Traversal    if(t) {Inordertraverse (t-lchild); cout<< t->data; Inordertraverse (T-rchild); }  }    voidPostordertraverse (Bitree T) {//Post-post traversal    if(t) {Postordertraverse (t-lchild); Postordertraverse (T-rchild); cout<< t->data; }  }    voidLevelorder (Bitree T) {//Hierarchical TraversalQueue<bitree> Q;//QueueBitree t = t;//to begin a hierarchical traversal from the T node.    if(t) q.push (t);//t non-empty, queue     while(!Q.empty ()) {T=Q.front ();          Q.pop (); cout<< t->data; if(T->lchild!=null)//the left subtree of T is not empty, the queueQ.push (t->lchild); if(T->rchild!=null)//the right subtree of T is not empty, the queueQ.push (t->rchild); }  }        /*how to ask Hovertree.com*/intnodecounthelp (Bitree T) {//returns the number of nodes of a two-fork tree rooted in T    if(t==NULL)return 0;//the number of empty binary tree nodes is 0.    Else returnNodecounthelp (T->lchild) + nodecounthelp (t->rchild) +1 ; //the number of nodes of non-empty binary tree is left dial hand tree node number + Right sub-tree node number +1}    intheighthelp (Bitree T) {//returns the height of a two-fork tree with T as its root    if(t==NULL)return 0;//the height of the empty binary tree is 0    Else{//the height of the non-empty binary tree is the maximum value of the left and right word height +1        intLheight = Heighthelp (t->lchild);//Zuozi Height        intRheight = Heighthelp (t->rchild);//Right Sub-tree height        return(lheight>rheight lheight:rheight) +1;//height for left and right sub-tree Heights Max +1    }  }    intMain () {Bitree root; printf ("Please enter the binary tree in the first order traversal, the empty node is replaced with ' @ ', such as [email protected]@[email protected]@,[email protected]@[email Protected]@@[email protected]@@");      Createbitree (root); //output First Order traversalcout <<"The First order traversal of this binary tree is:" ;      Preordertraverse (root); cout<<Endl; //output middle sequence traversalcout <<"The middle sequence traversal of this binary tree is:" ;      Inordertraverse (root); cout<<Endl; //output post-sequential traversalcout <<"The sequential traversal of this binary tree is:" ;      Postordertraverse (root); cout<<Endl; //Output Hierarchy Traversalcout<<"The hierarchical traversal of this binary tree is:" ;      Levelorder (root); cout<<Endl; //node number of output binary treecout <<"The number of nodes for this binary tree is:"<< nodecounthelp (Root) <<Endl; //the height of the output binary treecout <<"The height of this binary tree is:"<< heighthelp (Root) <<Endl; return 0; }

Recommendation: http://www.cnblogs.com/roucheng/p/wendang.html

Two-fork Tree

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.