SCUT data structure The second problem of the two-fork tree operation depth node number order the sequence hierarchy of the ancestors

Source: Internet
Author: User

/* #include <iostream> #include <windows.h>using namespace std; struct Btnode {char data; Btnode *left; Btnode *right; Btnode () {left = null; right = NULL;}} int main () {cout << "the two-fork tree given in parentheses is expressed as: A (B (D,e (H () (J,k ())), C (F,g (, I)))" <<endl;//input a string of parentheses notation; cout << "The depth of the two-fork tree given is:";//output depth; cout <<endl;cout << "The number of nodes given by the two-fork tree is:";//output number cout <<endl;cout << " The number of leaf nodes given by the two-fork tree is: "Number of <<endl;//leaf nodes//sequential traversal of binary tree://sequence;//level; cout <<" All ancestors of H in the two-forked tree given are: "<<endl; System ("Pause");} */#include <iostream> #include <windows.h> #include <stack>using namespace std; struct Bitnode{char c; Bitnode* left; bitnode* right;}  ;//char s[] = "A (b (c,d), E (f,g))", char s[] = "A (b (D,e (N))), C (J,k (, I)))"; void L,m (F,g createbtnode, Char *s) {Bitnode *st[20], *p = NULL; int top =-1, k, j=0; char ch; b = NULL; ch = s[j]; while (ch!= ') {switch (c h) {case ' (': top++; St[top] = p; k=1; break, Case ') ': top--, break; CASe ', ': K = 2; break;d efault:p = new Bitnode;p->c = ch; P->left = P->right = null, if (b = = null) b = P, Else{switch (k) {case 1:st[top]->left = p; ; right = p; break;}}} J++;ch = S[j];}} void Priorder (Bitnode *p) {if (P! = NULL) {cout <<p->c << "";p Riorder (p->left);p Riorder (p->right);} }//pre-order void post (Bitnode *p) {if (P! = NULL) {post (P->left);p Ost (p->right); cout <<p->c << "";}}     Post-post void Inorder (Bitnode *t) {stack<bitnode*> s;             while (!s.empty () | | t! = NULL) {while (t! = null) {S.push (t);         t = t->left;             } if (!s.empty ()) {t = S.top (); S.pop (); cout<<t->c<< "";         t = t->right;    }}}//middle order int front=0,rear=1;void Levelorder (bitnode *t) {Bitnode *q[100];q[0]=t; while (front<rear) {if (Q[front]) {cout<<q[front]->c<< ""; q[rear++]=q[front]->left;q[rear++]=q [Front]->right;front++;} else{front++;}}}  int u =0; int v =0; int height (Bitnode *p) {if (p==null) return 1;  U=height (P->left);   V=height (P->right); if (U&GT;V) return (u+1); Elsereturn (v+1);} int i =0; int node_num (Bitnode *p) {if (p== NULL) return 0; u= node_num (p->left); v =node_num (p->right); i++; Retu RN I;}    int Leaf_num (Bitnode *p) {if (!p) return 0;    else if (!p->left &&!p->right) return 1; else return Leaf_num (p->left) +leaf_num (p->right);}   int main () {struct Bitnode *p;   int len = sizeof (s);   Createbtnode (P,s);   cout << "The two-fork tree given by the topic is: A (B (D,e (H (J,k () (L,m (N)))), C (F,g (, I)))" <<endl;   cout << "The depth of the binary tree is:" <

The method of seeking ancestors: output pre-sequence traversal and post-order traversal, find the location of the desired node, the pre-sequence traversal before it, after the order traversal after him, the intersection is the ancestor



SCUT data structure The second problem of the two-fork tree operation depth node number order the sequence hierarchy of the ancestors

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.