Data Structure-Binary Tree

Source: Internet
Author: User
// Binary tree traversal/* sequential storage full Binary Tree: this does not waste space, but can be calculated based on the formula to determine the location of the left and right children of each node. Chain Storage Structure: Suitable for Binary Trees with fewer nodes. * // The node structure is as follows: typedef struct node {datatype data; struct node * lchild; struct node * rchild;} bitnode, * bitree;/* binary tree traversal 1. root-left subtree-right subtree (DLR) * First traverse 2. root-right subtree-left subtree (DRL) 3. left subtree-root-right subtree (LDR) * sequential traversal 4. left subtree-right subtree-root (LRD) * post-order traversal 5. right subtree-root-left subtree (RDL) 6. right subtree-left subtree-root (RLD) * // * traverse recursive process 1. traverse the access root node first, traverse the left subtree, and traverse the right subtree 2. traverse the left subtree in the middle order and access the root node to traverse the right subtree 3. post-sequential traversal: traverses the left subtree and the right subtree to access the root node * // The non-recursive algorithm concept of sequential traversal: as long as the current node exists or the stack is not empty, repeat the following operations. If the current node exists, go to the stack and go to the left subtree to perform rollback and access. Then, go to the right subtree. */Void inorder (bitree root) {inistack (& S); P = root; while (P! = NULL | isempty (s) {If (P! = NULL) {push (& S, P); P = p-> left;} else {Pop (& S, & P); visit (p-> data ); P = p-> right ;}}/ * The non-recursive algorithm thought of traversing a binary tree in descending order: repeat the following operations as long as the current node exists or the stack is not empty. 1. from the current node, go to the stack and enter the left subtree until the left subtree is empty. if the right subtree of the top node of the stack is empty, or the right child of the top node of the stack is a node that has just been accessed, roll back the stack and access the node, and set the pointer of the current node to null. 3. Otherwise, go to the right subtree */void inorder (bitree root) {initstack (& S); P = root; q = NULL; while (P! = NULL |! Isempty (s) {While (P! = NULL) // always access the last left subtree {pushstack (p); P = p-> left;} If (! Isempty (s) {popstack (p); // gets the top element of the stack if (p-> right = NULL | p-> right = q) // No right child, or the right child has been traversed {// access the root node visit (p-> data); q = P; P = NULL; // This is to stop accessing the second while loop function} else {P = p-> right; // access the right child }}/// switch the tree to a binary tree/* very simple, left child, right brother * // convert the forest to a binary tree/* multiple trees are in the forest together, So multiple trees are brothers, and then use the left child, the right brother indicates * // tree traversal/* 1. first, traverse the root node and access the subtree from left to right. 2. after root traversal, access the subtree from left to right to access the root node * // hafman tree (also called the most Binary Tree) // The binary tree with the shortest length in all the Binary Trees composed of N leaf nodes with the weight/* Constructor (Greedy Algorithm) ① regard n leaf nodes as N trees (a binary tree with only one node) and regard them as a forest. ② In the forest, combine the two trees with the minimum and minimum weights into a tree. the weights of the root node are the sum of the weights of the two trees. There are n-1 trees in the forest. ③ Repeat Step 2 until there is only one tree in the forest. This tree is the Harman tree. * // * I did a pen test yesterday to find the minimum time complexity of the maximum and minimum values in an array. I can't think of such a simple idea. BS will do it on its own) let's say that we can use numbers 1, 2, 3, 4, 5, 6. First, the first number is the maximum value, and the minimum value is compared from the second number. if the next number is greater than the maximum value and the maximum value is directly changed to this number, you do not need to compare the minimum value 2. if the next number is smaller than the maximum value, compare it with the minimum value. According to the above theory, the minimum time complexity is n-1 (the given sequence is 1, 2, 3, 4, 5, 6, 7 ,... n) the worst time complexity is 2 (n-1) (the given sequence is N, n-1, N-2... 1 )*/

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: 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.