Trees Implementation in Python
Trees Implementation in Python
TREES IN PYTHON
LIMA, 2020
1
INDEX
INTRODUCTION.3
CHAPTER I: FUNDAMENTAL DEFINITIONS................................................................4
1. DEFINITIONS AND PROPERTIES OF TREES..................................................6
2. TREES BINARIES............................................................................................................8
3. TREES SEARCH BINARIES11
CHAPTER II: TREES IN PYTHON
1. IMPLEMENTATION FROM A BINARY SEARCH TREE...................................13
2. OPERATIONS IN A BINARY SEARCH TREE.........................................13
2.1 Insert...........................................................................................................................13
2.2 Journey and printing15
A. Inorder traversal (IRD)15
B. Preorder traversal (RID).......................................................................................16
C. In post-order (IDR).................................................................................................16
2.3 Search............................................................................................................................17
2.4 Suppress..........................................................................................................................17
3. CODE FOR BINARY SEARCH TREES IN PYTHON........................19
CHAPTER III: EXERCISES
CHAPTER IV: APPLICATIONS...........................................................................................25
CONCLUSIONS. 27
BIBLIOGRAPHY. 28
2
INTRODUCTION
3
CHAPTER I: FUNDAMENTAL DEFINITIONS
Data structures are classified into static and dynamic, based on their capacity.
to change shape and size. They can also be classified as linear and non-linear.
linear, according to the distribution of their elements.
Trees are nonlinear and dynamic structures of vital importance within the
computation. They are hierarchical structures, applied to elements called
nodes.
TITLE
1. CHAPTER1
a. Section1
i. Subsection
b. Section2
i. Subsection1
ii. Subsection2
4
iii. Subsection3
2. CHAPTER2
a. Section1
i. Subsection
b. Section2
i. Subsection1
ii. Subsection2
c. Section3
i. Subsection
( A ( B ( D ( I ), E ( J, K, L )), C ( F ( O ), G ( M, N ), H ( P ))))
Nesting of parentheses
5
1. DEFINITIONS AND PROPERTIES OF TREES
Height of the tree is the maximum number of levels of all the nodes of the tree.
The descendants of a node (c in the diagram) are those accessible nodes.
through a path that starts at the node.
m) The ancestors of a node are the nodes on the path from the root to it.
6
Illustration 3: Height of the tree. Illustration 4: Depth of the tree.
It is the sum of the path lengths of all the nodes in the tree. It is calculated
by means of the formula:
h
LCI=¿ ∑
i=1
∗I
I
Where:
i: tree level
altura
ni: number of nodes at level i
Average length of internal path: for this, the LCI is divided by the
number of nodes of the tree (n).
LCI
LCIM=
n
Extended tree: it is a tree where the number of children of each node is equal to the
grade of the tree. If any of the trees do not comply, as many must be incorporated.
special nodes as required.
7
Then the length of the external path is the sum of the lengths of the path of
all the special nodes of the tree. It is calculated by the formula.
h +1
LCE=¿ = ∑
I=2
n∗I
hey
Where:
tree level
h: height
nei: number of special nodes at level i
Average length of external path (LCEM): divide the LCE by the number of
special nodes of the tree () n e
LCE
LCEM=
ne
Where:
2. BINARY TREES
It is a tree where each node can have at most two subtrees, not
left and another right. An empty tree is a special type of binary tree. The
The height of an empty tree is -1.
8
Illustration 5: Examples of binary trees
a) b)
a and b are distinct trees
B. Similar trees: when their structures are identical, but the information
of its nodes is different from each other.
9
a) b)
a and b are similar trees
C. Equivalent trees: they are similar trees and their nodes have the same
information.
a) b)
a and b are equivalent trees
D. Complete binary trees: All its nodes, except those of the last level,
they have 2 children: left and right subtrees.
a)
10
b)
a and b are complete trees
h
Number of nodes (ABC=2−1
)
For every node T of the tree, it must be fulfilled that all the values of the nodes of the
The left subtree of T must contain values smaller than or equal to the value of node T. In this way
similar, all the values of the nodes of the right subtree of T must be
greater than or equal to the value of node T.
11
Illustration 7: Distribution of elements in a binary search tree.
12
CHAPTER II: TREES IN PYTHON
For this case, we will use the Node class, as we will see below.
class Node:
def __init__(self, data):
[Link] = None
[Link] = None
[Link] = data
Once the Node class is created, we need to define the basic functions for it.
Insert, traverse and print, search and delete.
2.1 Insert
Build the nodes and insert them into the binary tree of
Function
search.
Exit No exit.
13
Conditions of The tree contains a node with information, located at
exit agreement with its key value and the rule defined in the function.
The insert function introduces the node's information and places it in the tree according to its
value and the conditions established in the function. To insert an element you
the following steps are taken:
1.- The key to be inserted must be compared with the root of the tree. If it is greater, it must
move towards the right subtree. If it is less, it should move towards the
left subtree.
The right subtree is equal to empty, or the left subtree is equal to empty;
in which case the item will be inserted in the place that corresponds to it
corresponds.
The key to be inserted is equal to the root of the tree; in which case it does not
perform the insertion.
14
Traverse and print all the elements of the binary tree
Function
search in the indicated order.
A very important operation when working with trees is their traversal. This consists of
by visiting the nodes in an orderly manner; so that all nodes are
visited only once. There are three types of tours:
Inorder traversal
Preorder traversal
Post-order traversal
def inorder(root):
if root is not None:
inorder([Link])
print([Link], end=" ")
inorder([Link])
15
B. Pre-order traversal (RID)
In pre-order traversal, the current node is visited before traversing the subtree.
left.
def preorder(root):
if root is not None:
print([Link], end=" ")
preorder([Link])
preorder([Link])
C. In postorder (IDR)
The postorder traversal visits the node after traversing the left and right subtrees.
law respectively. The procedure that implements this type of journey
is the following:
def postorder(root):
if root is not None:
postorder([Link])
postorder([Link])
print([Link], end=" ")
16
2.3 Search
With this operation, it explores within the tree, according to the requested key; it returns
None if not found. Below, we show an implementation of this
function follows below:
2.4 Suppress
Function Delete the node that contains the specified key value.
Exit None.
Conditions of
The node that contained the key value is not in the tree.
output
17
To remove a value in a binary search tree, the following cases can arise:
following cases:
18
Before closing the chapter, we will present the code to implement a tree.
binary search, where we will find the operations we have described
previously.
def preorder(root):
if root is not None:
print([Link], end=" ")
preorder([Link])
19
preorder([Link])
def postorder(root):
if root is not None:
postorder([Link])
postorder([Link])
print([Link], end=" ")
20
CHAPTER III: EXERCISES
To insert the elements into the tree, the function is called and the
parameters for each case.
2. Print the elements of the tree, starting from the three types of traversal.
Based on the data entered in the previous exercise, we can show the three
types of routes studied.
C. Postorder Traversal:
postorder(root)
21
Illustration 9: Program output.
4. Search the tree for the following elements: 5, 10, 20, 100.
22
Illustration 11: The output of the program is shown.
The elements to be deleted are entered into the program. The 6 is a leaf node, so
that the suppression is direct. The 12 has a son on the left, in this case it is carried out
the replacement.
23
Illustration 12: The program output is shown.
24
CHAPTER IV: APPLICATIONS
Next, we will provide synthetic information about the areas where there are
they take advantage of the functions of binary search trees:
Binary Space Partition (BSP): is a very efficient method for calculating the
visibility relationships between a static group of polygons in 3D, viewed
from an arbitrary point of view. It is used in almost all games of
3D video to determine which objects should be lent.
Binary Handling: it is used in almost all high bandwidth routers to
store tables-routers.
Hash Trees: also known as Merkle tree, is a data structure
stratified which aims to relate each node to a root
only associated with this one. They are used in p2p programs.
25
Syntax tree - Built by compilers and (implicitly)
calculators to analyze expressions.
T-trees: are often used by databases that store the majority of
your data in memory.
Binary trees are used more frequently than n-ary trees
because these are generally more complex and do not offer an advantage in
As for speed.
26
CONCLUSIONS
27
BIBLIOGRAPHY
Cairo, O. and Guadarti, S. (2006). Data Structures. (3rd ed.). Mexico: MacGrawHill.
VALIENTE, G. (2002). Algorithms on Trees and Graphs (1st ed.). Berlin: Springer.
Marzal, A. and Gracia, I. (2014). Introduction to Programming with Python (1st ed.)
Spain: Jaume University.
28