Está en la página 1de 21

rboles

Universidad Nacional Mayor de San Marcos Facultad de Ingeniera de Sistemas e Informtica Escuela de Ingeniera de Software Asignatura: Estructuras de Datos II Preparado por: Juan Gamarra Moreno

rboles
Un rbol es una estructura no lineal cuyos elementos estn organizados en jerarqua Un rbol es descrito por un conjunto grande de trminos relacionados

Elaborado por: Juan Gamarra Moreno

rboles
Longitud de camino y Nivel La longitud de camino se determina al contar el nmero de aristas que se deben seguir para llegar desde la raz al nodo

La altura de un rbol es la longitud del camino ms largo desde la raz hasta la hoja

Elaborado por: Juan Gamarra Moreno

rboles
Clasificacin de rboles

The most important criterion is the maximum number of children any node in the tree may have. This value is sometimes referred to as the order of the tree.

Elaborado por: Juan Gamarra Moreno

rboles
Clasificacin de rboles

A tree that has no limit to the number of children a node may have is called a general tree. A tree that limits each node to no more than n children is referred to as an n-ary tree

Elaborado por: Juan Gamarra Moreno

rboles
Clasificacin de rboles

A tree in which nodes may have at most two children is called a binary tree.

Elaborado por: Juan Gamarra Moreno

rboles
Clasificacin de rboles

A tree is considered to be balanced if all of the leaves of the tree are on the same level or at least within one level of each other.

Elaborado por: Juan Gamarra Moreno

rboles
Clasificacin de rboles

A balanced n-ary tree with m elements will have a height of lognm. Thus a balanced binary tree with n nodes will have a height of log2n.

Elaborado por: Juan Gamarra Moreno

rboles
Clasificacin de rboles

A balanced n-ary tree with m elements will have a height of lognm. Thus a balanced binary tree with n nodes will have a height of log2n.

Elaborado por: Juan Gamarra Moreno

rboles
Clasificacin de rboles

A tree is considered complete if it is balanced and all of the leaves at the bottom level are on the left side of the tree.

Elaborado por: Juan Gamarra Moreno

10

rboles
Clasificacin de rboles

Another related concept is the notion of a full tree. An n-ary tree is considered full if all the leaves of the tree are at the same level and every node is either a leaf or has exactly n children.

Elaborado por: Juan Gamarra Moreno

11

Estrategias para implementar rboles


Estrategias generales

The most obvious implementation of a tree is a linked structure. Each node could be defined as a TreeNode class. Each node would contain a pointer to the element to be stored in that node as well as pointers for each of the possible children of the node.

Elaborado por: Juan Gamarra Moreno

12

Estrategias para implementar rboles


Estrategias generales

It may also be useful for each node to store a pointer to its parent. This use of pointers is similar to the concept of a doubly linked list. The strategies for array implementations of a tree may be less obvious.

Elaborado por: Juan Gamarra Moreno

13

Estrategias para implementar rboles


Estrategia Computacional para implementar rboles con arrays

One possible computational strategy places the left child of element n at position (2 * n + 1) and the right child at position (2 * (n + 1)).

Elaborado por: Juan Gamarra Moreno

14

Estrategias para implementar rboles


Estrategia de enlace simulado para implementar rboles con arrays

The simulated link strategy allows array positions to be allocated contiguously regardless of the completeness of the tree.
El orden de entrada es A, C, B, E, D, F

Elaborado por: Juan Gamarra Moreno

15

Recorrido de rboles
Recorridos Preorder traversal, which is accomplished by visiting each node, followed by its children, starting with the root Inorder traversal, which is accomplished by visiting the left child of the node, then the node, then any remaining nodes, starting with the root Postorder traversal, which is accomplished by visiting the children, then the node, starting with the root Level-order traversal, which is accomplished by visiting all of the nodes at each level, one level at a time, starting with the root

Elaborado por: Juan Gamarra Moreno

16

Recorrido de rboles
Recorrido Preorder

Preorder traversal means visit the node, then the left child, then the right child.
Visit node Traverse(left child) Traverse(right child)

Preorder traversal would produce the sequence A, B, D, E, C.

Elaborado por: Juan Gamarra Moreno

17

Recorrido de rboles
Recorrido Inorder

Inorder traversal means visit the left child, then the node, then the right child..
Traverse(left child) Visit node Traverse(right child)

Inorder traversal would produce the sequence D, B, E, A, C.

Elaborado por: Juan Gamarra Moreno

18

Recorrido de rboles
Recorrido Postorder

Postorder traversal means visit the left child, then the right child, then the node.
Traverse(left child) Traverse(right child) Visit node

A postorder traversal would produce the sequence D, E, B, C, A.

Elaborado por: Juan Gamarra Moreno

19

Recorrido de rboles
Recorrido Level-Order

Level-order traversal means visit the nodes at each level, one level at a time, starting with the root.
Traverse(left child) Traverse(right child) Visit node

A level-order traversal would produce the sequence A, B, C, D, E.

Elaborado por: Juan Gamarra Moreno

20

Muchas Gracias

Elaborado por: Juan Gamarra Moreno

21