Week11 1
Week11 1
Recursive Definition:
A tree consists of a root, and zero or more subtrees T 1, T2, … , Tk such that there is
an edge from the root of the tree to the root of each subtree.
Why Tree is considered a non-linear data structure?
The data in a tree are not stored in a sequential manner i.e, they are not stored
linearly. Instead, they are arranged on multiple levels or we can say it is a
hierarchical structure. For this reason, the tree is considered to be a non-linear data
structure.
Parent Node: The node which is a predecessor of a node is called the parent
node of that node. {2} is the parent node of {6, 7}.
Child Node: The node which is the immediate successor of a node is called the
child node of that node. Examples: {6, 7} are the child nodes of {2}.
Root Node: The topmost node of a tree or the node which does not have any
parent node is called the root node. {1} is the root node of the tree. A non-empty
tree must contain exactly one root node and exactly one path from the root to all
other nodes of the tree.
Leaf Node or External Node: The nodes which do not have any child nodes are
called leaf nodes. {6, 14, 8, 9, 15, 16, 4, 11, 12, 17, 18, 19} are the leaf nodes of
the tree.
Ancestor of a Node: Any predecessor nodes on the path of the root to that node
are called Ancestors of that node. {1, 2} are the parent nodes of the node {7}
Descendant: Any successor node on the path from the leaf node to that node. {7,
14} are the descendants of the node. {2}.
Sibling: Children of the same parent node are called siblings. {8, 9, 10} are
called siblings.
Level of a node: The count of edges on the path from the root node to that node.
The root node has level 0.
Internal node: A node with at least one child is called Internal Node.
Neighbour of a Node: Parent or child nodes of that node are called neighbors of
that node.
Subtree: Any node of the tree along with its descendant.
Properties of a Tree:
Number of edges: An edge can be defined as the connection between two nodes.
If a tree has N nodes then it will have (N-1) edges. There is only one path from
each node to any other node of the tree.
Depth of a node: The depth of a node is defined as the length of the path from
the root to that node. It can also be defined as the number of edges in the path
from the root of the tree to the node.
Height of a node: The height of a node can be defined as the length of the
longest path from the node to a leaf node of the tree.
Height of the Tree: The height of a tree is the length of the longest path from the
root of the tree to a leaf node of the tree.
Degree of a Node: The total count of subtrees attached to that node is called the
degree of the node. The degree of a leaf node must be 0. The degree of a tree is
the maximum degree of a node among all the nodes in the tree.
Some more properties are:
Traversing in a tree is done by depth first search and breadth first search
algorithm.
It has no loop and no circuit
It has no self-loop
Its hierarchical model.
Here,
Node A is the root node
B is the parent of D and E
D and E are the siblings
D, E, and F are the leaf nodes
A and B are the ancestors of E
Few examples on Tree Data Structure: A code to demonstrate few of the above
terminologies has been described below:
Binary Tree: 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.
Binary Tree Representation: A tree is represented by a pointer to the topmost node
of the tree. If the tree is empty, then the value of the root is NULL.
A Tree node contains the following parts.
1. Data
2. Pointer to the left child
3. Pointer to the right child
1 //Root Node
/\
2 3
/\ /\
4 5 6 7 //Leaf Nodes
PreOrder Traversal of the above tree: 1-2-4-5-3-6-7
InOrder Traversal of the above tree: 4-2-5-1-6-3-7
PostOrder Traversal of the above tree: 4-5-2-6-7-3-1
The above properties of Binary Search Tree provides an ordering among keys so that
the operations like search, minimum and maximum can be done fast. If there is no
ordering, then we may have to compare every key to search for a given key.
Searching a key :
we want to search for the number, we start at the root, and then we compare the
value to be searched with the value of the root, if it’s equal we are done with the
search if it’s smaller we know that we need to go to the left subtree because in a
binary search tree all the elements in the left subtree are smaller and all the elements
in the right subtree are larger. Searching an element in the binary search tree is
basically this traversal, at each step we go either left or right and at each step we
discard one of the sub-trees.
we start with a search space of ‘n’ nodes and as we discard one of the sub-trees, we
discard ‘n/2’ nodes so our search space gets reduced to ‘n/2’. In the next step we
reduce the search space to ‘n/4’ and we repeat until we find the element or our
search space is reduced to only one node. The search here is also a binary search
hence the name; Binary Search Tree.
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data