Data structure on the machine "to create a two-fork tree, and the first post-order traversal, output tree height, the number of degrees is 0 1 2 nodes"

Source: Internet
Author: User
Tags define null

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include < malloc.h> #define NULL 0using namespace std;typedef struct Node {int data;//nodes *lchild,*rchild;} Node,*tree;typedef struct{tree top, base;} Stack;int Sum0,sum1,sum2,height; Stack s;void getstack (stack &s)//constructs an empty stack {s.base = (Tree) malloc (100*sizeof (node)); s.top = S.base;} int Stackempty (stack s)//Determine if the stack is empty {if (s.top==s.base) return 0;elsereturn 1;} void push (Stack &s, Tree e) {//Stack *s.top++ = *e;} void Gettree (tree &t)//Create two fork tree {int point;scanf ("%d", &point), if (point==null) t=null;else{t= (tree) malloc ( sizeof (node)); T->data=point;gettree (T->lchild); Gettree (T->rchild); }}void output (int e) {printf ("%d", e);} void Preordertraverse (Tree T)//first-order traversal {if (T) {printf ("%d", t->data); Preordertraverse (T->lchild); Preordertraverse (T->rchild);}}  void Inordertraverse (Tree T)//middle sequence traversal {getstack (s); Tree p=t; Tree q= malloc (sizeof (node)); while (p| | Stackempty (s)) {if (p) { Push (S,P); p=p->lchild; } else {*q = *--s.top; output (q->data); p=q->rchild;}}} void degree (Tree T)//For the number of nodes with a degree of 0 1 2, {if (t==null) return; Else{if (t->lchild&&t->rchild) sum2++; else if ((t->lchild&&! T->rchild) | | (! T->lchild&&t->rchild)) Sum1++;else if (! t->lchild&&!    T->rchild) sum0++;D egree (t->lchild); degree (t->rchild);}} int high (tree T)//ask for tree height {if (t==null) return 0;int Num1=high (t->lchild); int Num2=high (t->rchild); return Max (Num1,nu m2) +1;}        void Postordertraverse (Tree T)//post-traversal {if (T) {postordertraverse (t->lchild);        Postordertraverse (T->rchild); printf ("%d", T->data);}} void Checktree (tree T)//traverse entire tree {degree (t); Height=high (T);} int main () {Tree t;printf ("Enter the values of the nodes in the binary tree in order, input 0 indicates that the node is empty, input example: 1 2 0 0 3 0 0\n"), Gettree (T);p rintf ("Sequential recursive traversal of the binary tree: \ n ");    Preordertraverse (T);p rintf ("\ n");    printf ("Middle order recursive traversal of binary tree: \ n");    Inordertraverse (T);    printf ("\ n"); printf ("post-post recursionTraverse binary tree: \ n ");    Postordertraverse (T);    printf ("\ n");    sum1=sum0=sum2=0;    Checktree (T);    printf ("The number of nodes in this tree is 0%d\n\n", SUM0);    printf ("The number of nodes in this tree is 1%d\n\n", sum1);    printf ("The number of nodes in this tree is 2%d\n\n", sum2); printf ("The height of this tree is%d\n", height); return 0;}

  

Data structure on the machine "to create a two-fork tree, and the first post-order traversal, output tree height, the number of degrees is 0 1 2 nodes"

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.