Topic 5 Trees
Topic 5 Trees
Trees
Trees
Trees vs 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.
https://towardsdatascience.com/8-useful-tree-data-structures-worth-knowing-8532c7231e8c
Trees
Node Root
Edge
Parent
Internal nodes
Leaves
Child of
Eddard
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.
https://www.programiz.com/dsa/binary-tree
https://towardsdatascience.com/5-types-of-binary-tree-with-cool-illustrations-9b335c430254
Trees
Syntactic tree Search tree
Class hierarchy or "inheritance tree" showing the relationships among classes in object-
oriented programming
Search trees store data in a way that makes an efficient search algorithm possible
via tree traversal
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
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.
+ /
3 9 5 2
Trees
Search techniques (tree traversal):
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
• Pre-order
• root node, left node and then right node
+
Prefix notation:
* 7
+ * + 3 9 / 5 2 7
+ /
3 9 5 2
Trees
• In-order
• left node, root node and then right node
Common notation: * 7
3 + 9 * 5 / 2 + 7
+ /
3 9 5 2
Trees
• 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;
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
How to do backtracking?
Stacks :-)
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