(016) given an ordered array (incrementing), write a program to build a binary tree with the minimum height (keep it up), 016 Binary Tree

Source: Internet
Author: User

(016) given an ordered array (incrementing), write a program to build a binary tree with the minimum height (keep it up), 016 Binary Tree

Given an ordered array (ascending), write programs construct a binary tree with the minimum height.

Because the array is incremental and ordered, each time a node is created in the middle, the minimum tree is similar to the binary search method.

struct TreeNode{int data;TreeNode* leftChild;TreeNode* rightChild;};void newNode(TreeNode*& vNode, int vData){vNode              = new TreeNode;vNode->data        = vData;vNode->leftChild   = NULL;vNode->rightChild  = NULL;}void creatMinBinTree(TreeNode*& vNode, int vData[], int vBgn, int vEnd){if (vBgn <= vEnd){int Mid = vBgn + (vEnd - vBgn)>>1;newNode(vNode, vData[Mid]);creatMinBinTree(vNode->leftChild, vData, vBgn, Mid-1);creatMinBinTree(vNode->rightChild, vData, Mid+1, vEnd);}}



How to convert an array into a binary tree?

Search from the root node down, the largest to the right, the small to the left, in turn, until you can not continue to search. Then the position of the number in the binary tree is found.
Store Binary Trees by row only, and store them from left to right.

A one-dimensional array A (6) has been defined, and the values of each array element from A (1) to A (4) are 1, 2 in sequence, 3, 4, and then defines A two-dimensional array A (2,

And the summary and analysis of the real questions in recent years, the written test section often examines algorithm complexity, data structure concepts, stacks, binary tree traversal, and binary lookup. Readers should focus on this part.
Detailed key learning knowledge points:
1. Concepts of algorithms, time complexity, and space complexity
2. data structure definition, data logical structure and physical structure definition
3. Stack definition and calculation, and storage of linear linked lists
4. Concepts of trees and Binary Trees, basic properties of Binary Trees, concept of full binary trees, and traversal of Binary Trees
5. Binary Search
6. Bubble Sorting

1.1 Algorithm
Test site 1 Basic concepts of Algorithms
Exam link:
Test site 1 has a 30% probability of examination in the test, mainly in the form of blank questions, the score is 2 points, this test site is the recognition content, readers should also understand the basic data operations in algorithms.
A computer is actually implementing an algorithm, which is called a computer algorithm.
1. Basic Features of an algorithm: feasibility, certainty, poverty, and sufficient intelligence.
2. Basic Elements of an algorithm:
(1) Calculation and operation of data in the algorithm
An algorithm consists of two basic elements: calculation and operation of data objects, and control structure of algorithms.
In general computer systems, basic operations and operations include arithmetic operations, logical operations, relational operations, and data transmission.
(2) algorithm control structure: the execution sequence between operations in an algorithm is called the control structure of an algorithm.
Tools used to describe algorithms include traditional flowcharts, N-S structured flowcharts, and Algorithm Description Languages. An algorithm can generally be combined with three basic control structures: sequence, selection, and loop.
Test site 2 algorithm complexity
Exam link:
Test site 2 in the test, is a regular examination content, the probability of appearance in the test is 70%, mainly in the form of choice, the score is 2 points, this test site focuses on memorizing the time and space complexity concepts of the algorithm.
1. time complexity of the algorithm
The time complexity of an algorithm is the computing workload required to execute the algorithm.
The same algorithm is implemented in different languages, or compiled using different programs, or run on different computers, with different efficiency. This indicates that it is inappropriate to use absolute time units to measure the algorithm's efficiency. Aside from these factors related to computer hardware and software, we can think that the size of a specific algorithm "Running workload" depends only on the scale of the problem (usually expressed as an integer n ), it is a function of the problem scale. That is
Algorithm workload = f (n)
2. spatial complexity of Algorithms
The space complexity of an algorithm refers to the memory space required to execute this algorithm.
The storage space occupied by an algorithm includes the space occupied by the algorithm program, the storage space occupied by the input initial data, and the extra space required during Algorithm Execution. The extra space includes the unit of work in the execution of the Algorithm Program and the additional storage space required by a data structure. If the extra space is constant relative to the problem scale, the algorithm works in the same place. In many practical problems, compression storage technology is usually used to reduce the storage space occupied by algorithms, so as to minimize unnecessary extra space.

Q: How can I calculate the workload of an algorithm?
The workload of an algorithm is calculated by the number of basic operations performed by the algorithm, and the number of basic operations performed by the algorithm is a function of the problem scale, that is, the workload of an algorithm = f (n ), n is the scale of the problem.
1.2 Basic concepts of Data Structure
Test site 3 data structure definition
Exam link:
Test site 3 in the test, is a regular examination content, the probability of appearance in the test is 70%, mainly in the form of choice, the score is 2 points, this test site is the recognition content. Readers should also recognize the concept of the Logical Structure and storage structure of data.
As a computer discipline, data structure mainly studies and discusses the following three aspects:
(1) the logical relationship between data elements in a dataset, that is, the logical structure of data;
(2) when processing data elements, the storage relationships of each data element on the computer, that is, the data storage structure;
(3) operations on various data structures.
Data: refers to the symbolic representation of objective things. in computer science, it refers to all symbols that can be input into a computer and processed by computer programs.
Data Element: it is the basic unit of data.
 

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.