Data Structure and Algorithm Analysis Study Notes (4) & mdash; tree ADT, data structure adt

Source: Internet
Author: User

Data Structure and Algorithm Analysis Study Notes (4)-tree ADT and data structure adt

I. Binary Tree

1. Definition

A binary tree is a tree. Each node cannot have more than two sons.

2. Implementation

typedef struct TreeNode *PtrToNode;typedef PtrToNode Tree;typedef char ElementType;struct TreeNode{    ElementType Element;    Tree Left;    Tree Right;};

3. Construct an expression tree using a forward expression

void bianli(Tree t){    if (t)    {        bianli(t->Left);        cout << t->Element << " ";        bianli(t->Right);    }}int main(){    char c;    stack<Tree> treeStack;        while (1)    {        cout << "Please enter a suffix expression!(the last must is ';')" << endl;        cin >> c;        if (c == ';')        {            break;        }        else        {            if (c == '+' || c == '-' || c == '*' || c == '/')            {                auto t2 = treeStack.top();                treeStack.pop();                auto t1 = treeStack.top();                treeStack.pop();                PtrToNode temp2 = new(struct TreeNode);                temp2->Element = c;                temp2->Left = t1;                temp2->Right = t2;                treeStack.push(temp2);            }            else            {                PtrToNode temp;                temp = new(struct TreeNode);                temp->Element = c;                temp->Left = temp->Right = nullptr;                treeStack.push(temp);            }        }    }    auto t = treeStack.top();    bianli(t);        return 0;}

2. Search Tree ADT

1. Definition

For each node X in the tree, all the keyword values in the left subtree are less than the key value of X, and all the keyword values in the right subtree are greater than the key value of X.

2. Implementation

# Ifndef _ Tree_H # define _ Tree_H // node Structure declaration ~ Struct TreeNode; typedef struct TreeNode * Position; typedef struct TreeNode * SearchTree; typedef int ElementType; // type of the keyword
// Method declaration: SearchTree MakeEmpty (SearchTree T); Position Find (SearchTree, ElementType X); Position FindMin (SearchTree T); Position FindMxa (SearchTree T ); searchTree Insert (ElementType X, SearchTree T); SearchTree Delete (ElementType X, SearchTree T); ElementType Retrieve (Position P); # endif //! _ Tree_H
Struct TreeNode // node structure implementation {ElementType Element; SearchTree Left; SearchTree Right ;};

3. Implementation of related methods

(1) Clear ~

To clear the sub-tree, remember to use the post-order traversal method, because the post-order traversal can ensure that all sub-trees are cleared.

SearchTree MakeEmpty(SearchTree T){    if (T)    {        MakeEmpty(T->Left);        MakeEmpty(T->Right);        delete(T);    }    return nullptr;}

(2) Search

If it is smaller than the node, the left subtree is searched recursively, And the right subtree is larger.

Position Find(SearchTree T, ElementType X){    if (T)    {        if (T->Element == X)        {            return T;        }        else if (T->Element>X)        {            return Find(T->Left, X);        }        else        {            return Find(T->Right, X);        }    }            return nullptr;}

(3) Find the smallest

One way is to recursively find its left subtree until the last node.

Position FindMin(SearchTree T){    if (T)    {        if (T->Left == nullptr)        {            return T;        }        else        {            return FindMin(T);        }    }    else    {        return nullptr;    }}

(3) Find the largest

Of course, this simple recursion can be done with a for loop.

Position FindMax(SearchTree T){    if (T != nullptr)    {        while (T->Right != nullptr)        {            T = T->Right;        }    }    return T;}

(4) Insert

SearchTree Insert (ElementType X, SearchTree T) {if (T = nullptr) // empty tree, Insert {T = new (struct TreeNode); if (T = nullptr) {cout <"Out of Space! "<Endl;} else {T-> Element = X; T-> Left = T-> Right = nullptr;} else if (T-> Element <X) // if it is smaller than the current node, insert it to the left. {T-> Right = Insert (X, T-> Right);} else if (T-> Element> X) // if it is larger than the current node, It is inserted at the back side. {T-> Left = Insert (T-> Right);} the same else // indicates that {cout <"It has existed! "<Endl;} return T; // remember to return under limit}

(5) Delete

SearchTree Delete (ElementType X, SearchTree T) {if (T = nullptr) // locate the empty tree and stop {cout <"Element not found! "<Endl;} else if (X <T-> Element) {Delete (X, T-> Left);} else if (X> T-> Element) {Delete (X, T-> Right);} When else if (T-> Left & T-> Right) // the subtree exists, replace it with the smallest of the right subtree. {Auto t = FindMin (T-> Right); T-> Element = t-> Element; T-> Right = Delete (T-> Element, t-> Right);} else // if there is only one subtree or none, it is very simple to directly replace or delete {auto t = T; if (T-> Left = nullptr) {T = T-> Right;} else if (T-> Right = nullptr) {T = T-> Left ;} else {delete (T );}}}

What is the basis for junior high school students' self-learning data structure?

I. Different definitions of data structures
Data structures are used by computers to store and organize data. A Data Structure refers to a set of data elements with one or more specific relationships between each other. Generally, a well-selected data structure can improve the running or storage efficiency. Data structures are often related to efficient search algorithms and indexing technologies.
Data structures have no standard definitions in the computer scientific community. Individuals have different expressions based on their understanding:
In his book "data structures, algorithms, and applications", Sartaj Sahni said: "data structures are data objects, and various connections between the instances of the object and the data elements that constitute the instance. These links can be provided by defining related functions ." It defines a data object as a collection of instances or values ".
The definition of Clifford A. Shaffer in "Data structure and algorithm analysis" is: "Data structure is the physical implementation of ADT (Abstract Data Type ."
Lobert L. Kruse divides the design process of a data structure into the abstraction layer, data structure layer, and Implementation Layer in data structure and program design. The abstraction layer refers to the abstraction data type layer, which discusses the logical structure of data and its operations, the data structure layer and Implementation Layer discuss the representation of a data structure, the storage details of a computer, and the implementation of operations.
Ii. Significance of Data Structure
Generally, a data structure is organized by data elements based on certain logical connections. The description of the logical relationship between data elements is called the logical structure of data. Data must be stored in a computer, and the data storage structure is the implementation form of the data structure, which is expressed in a computer; in addition, to discuss a data structure, we must also discuss the operations performed on this type of data to make sense.
In the design of many types of programs, data structure selection is a basic design consideration. The construction experience of many large systems shows that the difficulty of system implementation and the quality of system construction depend heavily on whether the optimal data structure is selected. Many times, after the data structure is determined, the algorithm is easy to get. Sometimes things will turn around. We choose the data structure and adapt to the specific algorithm. In either case, it is very important to select an appropriate data structure.
The data structure is selected, and the algorithm is determined accordingly. data rather than algorithms are the key factors in system construction. This insight leads to the emergence of many software design methods and programming languages. object-oriented programming languages are one of them.
Iii. Data Structure

In computer science, data structure is a discipline that studies the operating objects (data elements) of computers and their relationships and operations in non-numerical computing programming, make sure that the new structure obtained after these operations is still the original structure type.
"Data Structure", as an independent course, was established in foreign countries since 1968. In 1968, Professor Tang ou knot created the initial system of data structure, his first volume of computer programming skills, basic algorithms, is the first book that systematically describes the logical structure, storage structure, and operations of data. "Data Structure" is a comprehensive basic professional course in computer science. Data structure is a core course between mathematics, computer hardware, and computer software. Data structure is not only the basis of general programming (especially non-numerical programming, it is also an important foundation for designing and implementing compilation programs, operating systems, database systems, and other system programs.
Computer is a science that studies Information Representation and processing using computers. There are two problems involved: Information Representation and information processing.
The Information Representation and organization are directly related to the efficiency of the information processing procedure. With the popularization of computers, the increase of Information and the widening of information scope, many system programs and applications are large in size, and the structure is quite complex. Therefore, in order to compile a "good" program, we must analyze the features of the objects to be processed and the relationships between the objects. This is the problem to be studied in the course of data structure. As we all know, computer programs process information. In most cases, these are... the remaining full text>


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.