Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


Lecture notes, cheat sheets
Free library / Directory / Lecture notes, cheat sheets

Computer science and information technology. Graphs (lecture notes)

Lecture notes, cheat sheets

Directory / Lecture notes, cheat sheets

Comments on the article Comments on the article

Table of contents (expand)

LECTURE No. 9. Tree-like data structures

1. Tree Data Structures

A tree-like data structure is a finite set of elements-nodes between which there are relations - the connection between the source and the generated.

If we use the recursive definition proposed by N. Wirth, then a tree data structure with base type t is either an empty structure or a node of type t, with which a finite set of tree structures with base type t, called subtrees, is associated.

Next, we give the definitions used when operating with tree structures.

If node y is located directly below node x, then node y is called the immediate descendant of node x, and x is the immediate ancestor of node y, i.e., if node x is at the i-th level, then node y is accordingly at (i + 1) - th level.

The maximum level of a tree node is called the height or depth of the tree. An ancestor does not have only one node of the tree - its root.

Tree nodes that have no children are called leaf nodes (or leaves of the tree). All other nodes are called internal nodes. The number of immediate children of a node determines the degree of that node, and the maximum possible degree of a node in a given tree determines the degree of the tree.

Ancestors and descendants cannot be interchanged, i.e., the connection between the original and the generated acts only in one direction.

If you go from the root of the tree to some particular node, then the number of branches of the tree that will be traversed in this case is called the length of the path for this node. If all branches (nodes) of a tree are ordered, then the tree is said to be ordered.

Binary trees are a special case of tree structures. These are trees in which each child has at most two children, called the left and right subtrees. Thus, a binary tree is a tree structure whose degree is two.

The ordering of a binary tree is determined by the following rule: each node has its own key field, and for each node the key value is greater than all keys in its left subtree and less than all keys in its right subtree.

A tree whose degree is greater than two is called strongly branched.

2. Operations on trees

Further, we will consider all operations in relation to binary trees.

I. Tree construction

We present an algorithm for constructing an ordered tree.

1. If the tree is empty, then the data is transferred to the root of the tree. If the tree is not empty, then one of its branches is descended in such a way that the order of the tree is not violated. As a result, the new node becomes the next leaf of the tree.

2. To add a node to an already existing tree, you can use the above algorithm.

3. When deleting a node from the tree, you should be careful. If the node to be removed is a leaf, or has only one child, then the operation is simple. If the node to be deleted has two descendants, then it will be necessary to find a node among its descendants that can be put in its place. This is necessary due to the requirement that the tree be ordered.

You can do this: swap the node to be removed with the node with the largest key value in the left subtree, or with the node with the smallest key value in the right subtree, and then delete the desired node as a leaf.

II. Finding a node with a given key field value

When performing this operation, it is necessary to traverse the tree. It is necessary to take into account different forms of tree notation: prefix, infix and postfix.

The question arises: how to represent the nodes of the tree so that it is most convenient to work with them? It is possible to represent a tree using an array, where each node is described by a combined type value, which has a character-type information field and two reference-type fields. But this is not very convenient, since the trees have a large number of nodes that are not predetermined. Therefore, it is best to use dynamic variables when describing a tree. Then each node is represented by a value of the same type, which contains a description of a given number of information fields, and the number of corresponding fields must be equal to the degree of the tree. It is logical to define the absence of descendants with nil. Then, in Pascal, the description of a binary tree might look like this:

TYPE TreeLink = ^Tree;

tree = record;

Inf : <datatype>;

Left, Right : TreeLink;

End.

3. Examples of the implementation of operations

1. Construct a tree of n nodes of minimum height, or a perfectly balanced tree (the number of nodes of the left and right subtrees of such a tree must differ by no more than one).

Recursive construction algorithm:

1) the first node is taken as the root of the tree.

2) the left subtree of nl nodes is built in the same way.

3) the right subtree of nr nodes is built in the same way;

nr = n - nl - 1. As an information field, we will take the node numbers entered from the keyboard. The recursive function that implements this construction will look like this:

Function Tree(n : Byte) : TreeLink;

Var t : TreeLink; nl,nr,x : Byte;

Begin

If n = 0 then Tree := nil

else

Begin

nl := n div 2;

nr = n - nl - 1;

writeln('Enter vertex number ');

readln (x);

new(t);

t^.inf := x;

t^.left := Tree(nl);

t^.right := Tree(nr);

Tree := t;

End;

{Tree}

End.

2. In the binary ordered tree, find the node with the given value of the key field. If there is no such element in the tree, then add it to the tree.

Search Procedure(x : Byte; var t : TreeLink);

Begin

If t = nil then

Begin

New(t);

t^inf := x;

t^.left := nil;

t ^ .right: = nil;

End

Else if x < t^.inf then

Search(x, t^.left)

Else if x > t^.inf then

Search(x, t^.right)

else

Begin

{process found element}

...

End;

End.

3. Write tree traversal procedures in forward, symmetric and reverse order, respectively.

3.1. Procedure Preorder(t : TreeLink);

Begin

If t <> nil then

Begin

WriteIn(t^.inf);

Preorder(t^.left);

Preorder(t^.right);

End;

End;

3.2. Procedure Inorder(t : TreeLink);

Begin

If t <> nil then

Begin

Inorder(t^.left);

WriteIn(t^.inf);

Inorder(t^.right);

End;

End.

3.3. Procedure Postorder(t : TreeLink);

Begin

If t <> nil then

Begin

postorder(t^.left);

postorder(t^.right);

WriteIn(t^.inf);

End;

End.

4. In the binary ordered tree, delete the node with the given value of the key field.

Let us describe a recursive procedure that will take into account the presence of the required element in the tree and the number of descendants of this node. If the node to be deleted has two children, then it will be replaced by the largest key value in its left subtree, and only then will it be permanently deleted.

Procedure Delete1(x : Byte; var t : TreeLink);

Var p : TreeLink;

Procedure Delete2(var q : TreeLink);

Begin

If q^.right <> nil then Delete2(q^.right)

else

Begin

p^.inf := q^.inf;

p := q;

q := q^.left;

End;

End;

Begin

If t = nil then

Writeln('no element found')

Else if x < t^.inf then

Delete1(x, t^.left)

Else if x > t^.inf then

Delete1(x, t^.right)

else

Begin

P := t;

If p^.left = nil then

t := p^.right

else

If p^.right = nil then

t := p^.left

else

Delete2(p^.left);

End;

End.

Author: Tsvetkova A.V.

<< Back: Tree Data Structures (Tree data structures. Operations on trees. Examples of implementation of operations)

>> Forward: Object data type (Object type in Pascal. The concept of an object, its description and use. Inheritance. Creating instances of objects. Components and scope)

We recommend interesting articles Section Lecture notes, cheat sheets:

Summary of works of Russian literature of the first half of the XNUMXth century

Criminal executive law. Lecture notes

State and municipal finance. Lecture notes

See other articles Section Lecture notes, cheat sheets.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

The existence of an entropy rule for quantum entanglement has been proven 09.05.2024

Quantum mechanics continues to amaze us with its mysterious phenomena and unexpected discoveries. Recently, Bartosz Regula from the RIKEN Center for Quantum Computing and Ludovico Lamy from the University of Amsterdam presented a new discovery that concerns quantum entanglement and its relation to entropy. Quantum entanglement plays an important role in modern quantum information science and technology. However, the complexity of its structure makes understanding and managing it challenging. Regulus and Lamy's discovery shows that quantum entanglement follows an entropy rule similar to that for classical systems. This discovery opens new perspectives in the field of quantum information science and technology, deepening our understanding of quantum entanglement and its connection to thermodynamics. The results of the study indicate the possibility of reversibility of entanglement transformations, which could greatly simplify their use in various quantum technologies. Opening a new rule ... >>

Mini air conditioner Sony Reon Pocket 5 09.05.2024

Summer is a time for relaxation and travel, but often the heat can turn this time into an unbearable torment. Meet a new product from Sony - the Reon Pocket 5 mini-air conditioner, which promises to make summer more comfortable for its users. Sony has introduced a unique device - the Reon Pocket 5 mini-conditioner, which provides body cooling on hot days. With it, users can enjoy coolness anytime, anywhere by simply wearing it around their neck. This mini air conditioner is equipped with automatic adjustment of operating modes, as well as temperature and humidity sensors. Thanks to innovative technologies, Reon Pocket 5 adjusts its operation depending on the user's activity and environmental conditions. Users can easily adjust the temperature using a dedicated mobile app connected via Bluetooth. Additionally, specially designed T-shirts and shorts are available for convenience, to which a mini air conditioner can be attached. The device can oh ... >>

Energy from space for Starship 08.05.2024

Producing solar energy in space is becoming more feasible with the advent of new technologies and the development of space programs. The head of the startup Virtus Solis shared his vision of using SpaceX's Starship to create orbital power plants capable of powering the Earth. Startup Virtus Solis has unveiled an ambitious project to create orbital power plants using SpaceX's Starship. This idea could significantly change the field of solar energy production, making it more accessible and cheaper. The core of the startup's plan is to reduce the cost of launching satellites into space using Starship. This technological breakthrough is expected to make solar energy production in space more competitive with traditional energy sources. Virtual Solis plans to build large photovoltaic panels in orbit, using Starship to deliver the necessary equipment. However, one of the key challenges ... >>

Random news from the Archive

Turning light into matter 14.10.2021

In the American government laboratory located on Long Island, scientists create matter from light alone with the help of a sophisticated particle accelerator. This phenomenon on our planet occurs for the first time.

This experimental breakthrough confirmed the predictions made by influential physicists almost a century ago, and also shed new light on the mysterious processes that occur on both quantum and cosmic scales.

The transformation of photons, massless particles of light, into electrons, elementary particles of matter, was carried out by a team of researchers at the RHIC relativistic heavy ion collider. The theoretical prerequisites for this work appeared at the beginning of the XNUMXth century, but in order to test them experimentally, it was necessary to seriously equip the experimental equipment at RHIC - the solenoid tracker-detector (STAR).

“This is an interesting effect, since the photon has no charge, so from a classical point of view, the magnetic field should not affect it,” the scientists explained. “Therefore, this clearly proves the fundamental aspects of quantum mechanics. A photon can experience constant fluctuations, turning into an electron-positron pair , which in turn interacts with the magnetic field, and that's what we measured."

The new measurements could help astrophysicists and cosmologists model the creation of electron-positron pairs from the light surrounding the universe's most energetic objects and events, from supernovae to black holes. The STAR collaboration plans to continue experimenting by attempting to capture the first two-dimensional images of the atomic nucleus in unprecedented detail.

Other interesting news:

▪ Beosound Balance wireless speaker

▪ Artificial eyelashes controlled by magnet and light

▪ Gravitational waves could help predict tsunamis

▪ E Ink Mobius screens for smartwatches

▪ Quiet New Fujitsu Celsius Workstations

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ section of the site Mobile communications. Article selection

▪ article Long box. Popular expression

▪ article Is there a lot of water in a cactus? Detailed answer

▪ article Machinist of dust pumps. Standard instruction on labor protection

▪ article Fluorescent lamps for lighting aquariums. Encyclopedia of radio electronics and electrical engineering

▪ article Radio microphone with a loop antenna. Encyclopedia of radio electronics and electrical engineering

Leave your comment on this article:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024