0% found this document useful (0 votes)
30 views27 pages

Topic 5 Trees

The document discusses different types of trees including binary trees, tree traversal algorithms, and tree representations. It covers tree definitions, tree traversal techniques like preorder, inorder and postorder, and implementations of tree traversals using recursion and iteration.

Uploaded by

temporal034
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views27 pages

Topic 5 Trees

The document discusses different types of trees including binary trees, tree traversal algorithms, and tree representations. It covers tree definitions, tree traversal techniques like preorder, inorder and postorder, and implementations of tree traversals using recursion and iteration.

Uploaded by

temporal034
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Engineering School

Computer Architecture and Operating Systems Department

Degree: Artificial Intelligence


Subject: Fundamentals of Programming II

Trees
Trees

Trees vs trees

Trees – plant Trees – descrete mathematics

perennial plant with an elongated


stem, or trunk, usually supporting
branches and leaves.
Trees

Tree is the non-linear (hierarchical) data structure, that consists of nodes connected
by edges.

Each node in the tree can be connected to many children (depending on the type of
tree), but must be connected to exactly one parent, except for the root node, which
has no parent.

There are no cycles or "loops" (no node can be its own ancestor)

Each child can be treated like the root node of its own subtree, making recursion a
useful technique for tree traversal.

In contrast to linear data structures, trees cannot be represented by relationships


between neighboring nodes in a single straight line.
Trees

https://towardsdatascience.com/8-useful-tree-data-structures-worth-knowing-8532c7231e8c
Trees

Node Root
Edge

Parent

Internal nodes

Leaves

Child of
Eddard

If we remove any edge it disconnects and if we add an edge we make a cycle.


A tree is the most economical connected graph given a set of vertices.​
Trees Root

Parent

Internal nodes

Leaves

Child of
Eddard

Who is Sansa's parent? Eddard. All nodes except the root have a single parent.​
How many children does Rickard have? 4, it is the degree of the Rickard node.​
How many siblings does Sansa have? 4, nodes that have the same parent are siblings.​
How many children have they had at most in the family? 5, is the degree of the tree.​

Degree of a node: Number of children.​


Degree of a tree: The greatest degree of its nodes.
Trees
Full Binary Tree
Every parent node/internal node has either two or no children.

Perfect Binary Tree


Every internal node has exactly two child nodes and all the leaf nodes are at the same level.

Complete Binary Tree


A full binary tree, where every level must be completely filled and all the leaf elements must
lean towards the left.

Degenerate or Pathological Tree


The tree has a single child either left or right.

Skewed Binary Tree


A skewed binary tree is a pathological/degenerate tree in which the tree is either
dominated by the left nodes or the right nodes.

Balanced Binary Tree


It is a type of binary tree in which the difference between the height of the left and the right
subtree for each node is either 0 or 1.

https://www.programiz.com/dsa/binary-tree
https://towardsdatascience.com/5-types-of-binary-tree-with-cool-illustrations-9b335c430254
Trees
Syntactic tree Search tree

Trees Decision tree Genealogical tree

If we want to represent the hierarchical structure of a family?


If we want to do an optimal search?​
If we want to make successive decisions?
If we want to represent an expression to a compiler?​
Trees
File systems for:
• Directory structure used to organize subdirectories and files
• The mechanism used to allocate and link blocks of data on the storage device

Class hierarchy or "inheritance tree" showing the relationships among classes in object-
oriented programming

Abstract syntax trees for computer languages

Natural language processing:


• Parse trees
• Modeling utterances in a generative grammar
• Dialogue tree for generating conversations

Document Object Models ("DOM tree") of XML and HTML documents

Search trees store data in a way that makes an efficient search algorithm possible
via tree traversal

A binary search tree is a type of binary tree

Representing sorted lists of data


Trees

0
1 2 3
4 5 6 7 8 9 10 11
12 13 14 15 16

N-ary Binary
Trees struct TreeNode
{
int data;
struct TreeNode *left;
• Create binary tree struct TreeNode *right;
• Search into binary tree };
• Delete binary tree
• Displaying binary tree

Visiting − Visiting refers to checking the value of a node when


control is on the node.
Traversing − Traversing means passing through nodes in a
specific order.

https://www.thegeekstuff.com/2013/02/c-binary-tree/
Trees
Tree definition is recursive
Root
Left child Right child

We have the root and the two children who are also trees.​

Many of the methods we will implement will be recursive and will be


applied to the left subtree and the right subtree, respectively in the
appropriate order.
Trees
Compilers use trees to represent the syntactic structure of the
programs they are parsing.

Abstract Syntax Tree (AST).

From the tree we can generate the code of a program.​


+
Expression: (3+9) * (5/2) + 7
* 7

+ /

3 9 5 2
Trees
Search techniques (tree traversal):

• Depth-First Search (DFS) Algorithm: It starts with the root


node and first visits all nodes of one branch as deep as
possible of the chosen Node and before backtracking, it
visits all other branches in a similar fashion.

• Breadth-First Search (BFS) Algorithm: It also starts from


the root node and visits all nodes of current depth before
moving to the next depth in the tree.

https://towardsdatascience.com/4-types-of-tree-traversal-algorithms-d56328450846
Trees
Tree display (traversing) technique:
Recursive definition
• Pre-order
• root node, left node and then right node
• In-order
• left node, root node and then right node
• Post-order
• left node, right node and then root node

Route with NON-recursive definitions:​ By levels:


Nodes are labeled according to their level, they are traversed from lowest to highest level, if
there is more than one of the same level, they are traversed from left to right.​
Trees

Tree display (traversing) technique:

• Pre-order
• root node, left node and then right node

+
Prefix notation:
* 7
+ * + 3 9 / 5 2 7
+ /

3 9 5 2
Trees

Tree display (traversing) technique:

• In-order
• left node, root node and then right node

Common notation: * 7
3 + 9 * 5 / 2 + 7
+ /

3 9 5 2
Trees

Tree display (traversing) technique:

• Post-order
• left node, right node and then root node

Postfix notation: * 7
3 9 + 5 2 / * 7 +
+ /

3 9 5 2
Trees

struct TreeNode
{
int data;
struct TreeNode *left;
struct TreeNode *right;
};
Trees
/* Given a binary tree, print its nodes
in preorder*/
void printPreorder(struct node* node)
{
if (node == NULL)
return;

printf("%d ", node->data);


printPreorder(node->left);
printPreorder(node->right);
}
Trees
/* Given a binary tree, print its nodes
in inorder*/
void printInorder(struct node* node)
{
if (node == NULL)
return;

printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
Trees
/* Given a binary tree, print its nodes
according to the postorder */
void printPostorder(struct node* node)
{
if (node == NULL)
return;

printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
Trees

int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);



}
Trees

Search for an iterative solution...

How to do backtracking?

Stacks :-)

push() and pop()

Analyze, for example:


https://prepinsta.com/data-structures/preorder-tree-traversal-without-recursion/
Trees

struct TreeNode
{
int data;
struct TreeNode *left;
struct TreeNode *right;
struct TreeNode *parent;
};
Trees
35

parent:NULL
left: 15 80
right:
data:→35 10 25 47 92
parent: parent:
left: left: 85 105
right: right:

data: → 15 data: → 80
parent: parent: parent: parent:
left: NULL left:NULL left:NULL left:
right:NULL right:NULL right:NULL right:
data:→ 10 data:→25 →47
data: data:→ 92
parent: parent:
left:NULL left:NULL
right:NULL right:NULL

data:→ 85 data:→105

You might also like