Binary Tree correlation

Source: Internet
Author: User

 

/* _ The essence of recursion is fully embodied in the various problems of Binary Trees !!! */# Include <stdio. h> # include <iostream> # include <stack> # include <vector> # include <assert. h> using namespace STD; typedef struct node // struct representation of Binary Tree nodes {int data; node * left, * right;} btree; // --------------------------------- _ create a binary tree (pre-order) ------------------------- btree * Create () {int data; scanf ("% d", & data); If (Data = 0) return 0; // _ input 0 indicates no subnode btree * root = new btree; root-> DATA = data; root-> left = create (); root-> right = crea Te (); Return root;} btree * create2 (int * X, int N) {static int I =-1; I ++; if (X [I] = 0) return 0; btree * TMP = new btree; TMP-> DATA = x [I]; TMP-> left = create2 (X, n); TMP-> right = create2 (x, n); Return TMP;} // ------------- _ traverse a binary tree (pre-order) ------------- void preorder (btree * tree) {If (tree = 0) return; printf ("% d", tree-> data); preorder (tree-> left); preorder (tree-> right );} void preorder2 (btree * tree) // stack mode {If (tree = 0) return; stack <btree *> S; S. Push (tree); btree * temp = 0; while (! S. empty () {temp = S. top (); printf ("% d", temp-> data); S. pop (); If (temp-> right) s. push (temp-> right); If (temp-> left) s. push (temp-> left) ;}// --------------- _ traverse a binary tree (in the middle order) ----------- void inorder (btree * tree) {If (tree = 0) return; inorder (tree-> left); printf ("% d", tree-> data); inorder (tree-> right);} void inorder2 (btree * tree) {If (tree = 0) return; stack <btree *> S; btree * temp = 0; S. push (tree); While (! S. empty () {temp = S. top (); S. pop (); // _ Pop-up node while (temp) // _ to the leftmost end {S. push (temp); temp = temp-> left;} If (! S. empty () {temp = S. top (); S. pop (); // _ extract the leftmost End Node printf ("% d", temp-> data); S. push (temp-> right); // _ to store the right brother of the leftmost node, which must be pushed to the right, no matter whether there are any nodes }}// ------------- _ traverse a binary tree (post-order) ----------- void posorder (btree * tree) {If (tree = 0) {return ;} posorder (tree-> left); posorder (tree-> right); printf ("% d", tree-> data);} void posorder2 (btree * tree) {If (! Tree) return; stack <btree *> S; btree * temp = 0, * lastvisit = 0; S. Push (tree); While (! S. empty () {temp = S. top (); S. pop (); While (temp) {S. push (temp); temp = temp-> left;} temp = S. top (); If (temp-> Right & (temp-> right! = Lastvisit) {S. push (temp-> right);} else {lastvisit = temp; printf ("% d", lastvisit-> data); S. pop () ;}}// your _ binary tree image ---------------------------------- void getimage (btree * tree) // _ calculate the binary tree image {If (tree = 0) return; getimage (tree-> left); getimage (tree-> right); // swaps left _ and rightbtree * TMP = tree-> left; tree-> left = tree-> right; tree-> right = TMP;} bool isimage (btree * tree1, btree * tree2) // _ determine whether the binary tree is an image. {If (tree1 = 0 & tree2 = 0) return true; If (! Tree1 |! Tree2) return false; If (tree1-> data! = Tree2-> data) return false; bool same1 = isimage (tree1-> left, tree2-> right); bool same2 = isimage (tree1-> right, tree2-> left); Return same1 & same2;} // -------------------------------------- _ path-related problem --------------------------- btree * findnode (btree * tree, int key) // _ Find the node whose key is a specific value. 0 {If (! Tree) return 0; If (tree-> DATA = Key) return tree; btree * TMP = findnode (tree-> left, key); If (TMP) return TMP; TMP = findnode (tree-> right, key); If (TMP) return TMP; return 0;} bool findnode1 (btree * tree, int key, btree * & node) // _ this method is more concise and clear {If (! Tree) return false; If (tree-> DATA = Key) {node = tree; return true;} return findnode1 (tree-> left, key, node) | findnode1 (tree-> right, key, node);} bool findpath (btree * tree, btree * P, stack <btree *> & St) // _ path from the root node to a node {If (! Tree |! P) return 0; If (tree = P) {St. push (tree); return 1;} If (findpath (tree-> left, P, St) | findpath (tree-> right, P, St )) {// _ If the left and right subtree with tree as the root contains the desired node, the root node must be in the path St. push (tree); return 1;} elsereturn 0;} void findsumpath (btree * tree, int expectedsum, Int & currentsum, vector <btree *> & V) // _ path for output and a value {If (! Tree) return; V. push_back (tree); currentsum + = tree-> data; If (expectedsum = currentsum) {// _ print output vector <btree *>: iterator it = v. begin (); While (it! = V. end () {cout <(* It)-> data <''; it ++;} cout <Endl;} If (tree-> left) findsumpath (tree-> left, expectedsum, currentsum, V); If (tree-> right) findsumpath (tree-> right, expectedsum, currentsum, V ); // _ Delete the current node currentsum-= v. back ()-> data; V. pop_back ();} btree * lowestcommonancestor (btree * tree, btree * a, btree * B) // _ recent public parent node {/* very simple: For node I, if A and B are in the left and right subtree of the node, the node is the same, or the node is one of the two known nodes, the other is exactly under it _ */If (! Tree) return 0; If (tree = A) return a; // _ locate the node and return if (tree = B) return B; btree * tmp1 = lowestcommonancestor (tree-> left, a, B); btree * tmp2 = lowestcommonancestor (tree-> right, a, B); If (tmp1 & tmp2) return tree; // _ both left and right are found. The parent node is the tree. If (tmp1) return tmp1; If (tmp2) return tmp2 ;} // another method for the nearest public parent node: Save the two paths, _ finding the last same node // ---------------------------------------- _ information related to the binary tree --------------------------- // depth (or height) starts from 1. The benefit is that, if the tree is empty, the depth is 0 Int. Depth (btree * tree) // _ Binary Tree depth {If (tree = 0) return 0; return 1 + max (depth (tree-> left ), depth (tree-> right);} void levelnum (btree * tree, int depth, int current) // _ output node of a layer {If (tree = 0) return; If (current = depth) {cout <tree-> data <''; return;} levelnum (tree-> left, depth, current + 1 ); levelnum (tree-> right, depth, current + 1 );} // ================================================ ========================================================== === void main5 () {// Btree * tree = create (); int X [15] = {, 0 }; /* 1/second 2 3/second 4 5 6/7 */btree * tree = create2 (x, 15); // printf ("Forward traversal :"); preorder (tree); printf ("\ n"); // getimage (tree); // printf ("preorder traversal:"); preorder (tree ); printf ("\ n"); // printf ("sequential traversal:"); inorder2 (tree); printf ("\ n "); // printf ("sequential traversal:"); posorder (tree); printf ("\ n");/* test: path from the root node to a node: btree * node; findnode1 (tree, 7, node); stack <btree *> st; findpath (Tree, node, St); While (! St. empty () {cout <St. top ()-> data <"; ST. pop () ;}** test: And for a specific value path int currentsum = 0; vector <btree *> V; findsumpath (tree, 10, currentsum, V ); ** test: _ recent public parent node btree * a, * B, * C; findnode1 (tree, 4, a); findnode1 (tree, 2, B ); C = lowestcommonancestor (tree, a, B); cout <c-> data <Endl; ** test: _ deep cout of the tree <depth (tree) <Endl; return; ** test: _ levelnum (tree, 4, 1) of the output tree );*/}

 

Binary Tree correlation

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.