0% found this document useful (0 votes)
132 views

Trees PPT Computer Club

This document defines and explains trees and binary trees. It defines key tree concepts like nodes, edges, root, parent and child nodes. It then explains properties of binary trees like their structure, different types of binary trees (full, complete, perfect), tree traversals, calculating height, depth and diameter. It provides examples and references for further reading.

Uploaded by

Sayan Chandra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

Trees PPT Computer Club

This document defines and explains trees and binary trees. It defines key tree concepts like nodes, edges, root, parent and child nodes. It then explains properties of binary trees like their structure, different types of binary trees (full, complete, perfect), tree traversals, calculating height, depth and diameter. It provides examples and references for further reading.

Uploaded by

Sayan Chandra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Trees

Presented by:
Murtaza Ali
Computer Club
What is a Tree?
Define Tree!!

• A tree is a hierarchical data structure defined as a collection of nodes.


Nodes represent value and nodes are connected by edges.
• A tree has the following properties:
• The tree has one node called root. The tree originates from this, and hence it does
not have any parent.
• Each node has one parent only but can have multiple children.
• Each node is connected to its children via edge.
Basic Terms
Basic Terms
• Root: Root is a special node in a tree. The entire tree originates from it. It
does not have a parent.
• Parent Node: Parent node is an immediate predecessor of a node.
• Child Node: All immediate successors of a node are its children.
• Leaf: Node which does not have any child is called as leaf.
• Edge: Edge is a connection between one node to another. It is a line
between two nodes or a node and a leaf.
Basic Terms
• Siblings: Nodes with the same parent are called Siblings.
• Path/Traversing: Path is a number of successive edges from source node to
destination node.
• Height of Node: Height of a node represents the number of edges on the
longest path between that node and a leaf.
• Levels of node: Level of a node represents the generation of a node. If the
root node is at level 0, then its next child node is at level 1, its grandchild is at
level 2, and so on.
Basic Terms
• Degree of Node: Degree of a node represents the number of children of a node.
• Sub tree: Descendants of a node represent subtree.
• Internal nodes: A node has at-least one child node.
• Ancestor node: An ancestor of a node is any predecessor node on a path from
the root to that node. The root node doesn't have any ancestors.
• Descendant: The successor of the given node is known as a descendant of a
node.
Depth and Height
• The depth of a node is the number of edges from the root to the node.
• The height of a node is the number of edges from the node to the deepest
leaf in the subtree.
Binary Trees
• A tree whose elements have at most 2 children is called a binary tree.
Since each element in a binary tree can have only 2 children, we typically
name them the left and right child.
• A binary tree is made of nodes, where each node contains a "left"
reference, a "right" reference, and a data element. The topmost node in the
tree is called the root.
Structure of a Node
class Node {
int data;
Node left, right;
}
Properties of a Binary Tree
• The maximum number of nodes at level ‘l’ of a binary tree is 2l. 
• The maximum number of nodes in a binary tree of height ‘h’ is 2h+1 – 1. 
• In a Binary Tree with N nodes, minimum possible height or the minimum
number of levels is Log2(N+1).
• A Binary Tree with L leaves has at least | Log2L |+ 1   levels.
• In Binary tree where every node has 0 or 2 children, the number of leaf
nodes is always one more than nodes with two children.
Full Binary Tree
• A Binary Tree is a full binary tree if every node has 0 or 2 children.
Complete Binary Tree
• A Binary Tree is a Complete Binary Tree if all the levels are completely
filled except possibly the last level and the last level has all keys as left as
possible.
Perfect Binary Tree
• A Binary tree is a Perfect Binary Tree in which all the internal nodes have
two children and all leaf nodes are at the same level.
• n = 1 + 2 + 4 + ... + 2h-1 + 2h = 2h+1 - 1
Tree Traversals
• Depth-First Search
• Pre-Order Traversal
• In-Order Traversal
• Post-Order Traversal
• Breadth-First Search
Depth-First Search
Depth-First Search
Depth-First Search
Level-Order Traversal
Level-Order Traversal
Height of Binary Tree
Depth of a Node in Binary Tree

class Node {
int data, depth;
Node left, right;
}
Diameter of a Binary Tree
• The diameter of a tree is the number of nodes on the longest path between
two leaves in the tree.
Diameter of a Binary Tree
• The diameter of a tree T is the largest of the following quantities:
• the diameter of T’s left subtree.
• the diameter of T’s right subtree.
• the longest path between leaves that goes through the root of T (this can be
computed from the heights of the subtrees of T).
Diameter of a Binary Tree
• The algorithm for a recursive method looks like:
• Find the diameter of the left sub-tree.
• Find the diameter of the right sub-tree.
• The diameter may pass through the root, so their combined height.
• The greatest value among these 3 values would be the diameter of the binary tree.
Diameter of a Binary Tree
References
• https://www.geeksforgeeks.org/binary-tree-data-structure/
• https://www.mygreatlearning.com/blog/understanding-trees-in-data-struct
ures/
• https://www.javatpoint.com/tree
• https://www.andrew.cmu.edu/course/15-121/lectures/Trees/trees.html
• https://www.baeldung.com/cs/binary-tree-height
• https://studyalgorithms.com/tree/diameter-of-a-binary-tree/
Thank You
LinkedIn: linkedin.com/in/murtaza-ali-62494118b
Computer Club: linktr.ee/computerclubgs

©Computer Club. All Rights Reserved

You might also like