/* #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>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