Q: Binary tree main solution: Betta author: Langhaixin Thank: Totobi, Leemars, Betta review: Starfish Community Correspondence: View A: Assuming the node value of the binary tree is character, The preamble and the presenter test of the t-t-t-tone is written in a C function stored in the standard form.
Standard storage form of the binary tree is TPyedef struct bt {char data; struct bt * lefttree; Struct bt * righttree;} binarytree ------------------------- -------------------------------------- BrinaryTree * q [MaxSize]; brinarytree * Creatree () {Char ch; int forward, rest; brinarytree * root, * s; root = null; front = 1; rest = 0; ch = getchar (); while (ch! = '#') {s = null; if (IF) Ch! = '@') {s = malloc (sizeof (binarytree)); S-> Data = ch; S-> LEFTTREE = NULL; S-> Righttree = NULL;} Rear ; q [REAR] = S; IF (REAR == 1) Root = S; ELSE {IF (S && q [Front]) IF (Rear% 2 == 0) q [Front] -> LeftTree = S; Else Q [Front] -> RightTree = S; IF (rear% 2 == 1) Front ;} ch = getchar;} Return root;} ----------------------------- ---------------------------------- I will give an example. It is known to be the order of Abcdefg is CBEDAFG. The binary tree is first divided into two sections (AFG) A to be divided into two sections (AFG) A to do the left sub-tree (fg) on the right sequence (AFG). The rear part is divided into (fg) (fg) the problem to convert to a known preamble is the BCDE order is Cbed to seek bifuries and the pre-sequence is f. G ordered is that FG seeks the bifurcation. It has not been seen. The following steps are submitted to the following steps. Pre-sequence: Root left subtree Decomposes the right tree decomposition BCDE CBED: B (C) (C) (ED) DE) C C: C DE ED: D (E) (E) E: E: E FG FG: F (G) (G) g: g The binary tree derived is A / / B F / / C D g / E ------------------------------------------------ -------------- This is a program written by the algorithm description of Leemars (small tiger): Compile: // vc cl -gx treep.cpp // GCC G -o Treep Treep. CPP #include <
iostream> class CTreeNode {public: CTreeNode (char cVal, CTreeNode * pLeft = 0, CTreeNode * pRight = 0): m_cVal (cVal), m_pLeft (pLeft), m_pRight (pRight) {} ~ CTreeNode () {if (m_pLeft! = 0) {delete m_pleft;} std :: cout << m_cval; if (m_pright! = 0) {delete m_pright;}} char m_cval; ctreenode * m_pleft; ctreenode * m_pright;}; ctreenode * createnode (char * spre, Char * smid, int nsize) {if (nsize == 0) Return 0; if (nsize == 1) Return New Ctreenode (* SPRE); Int nsplitmid; for (nsplitmid = 0; nsplitmid