3. Trees
• Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear
data structures, trees are hierarchical data structures.
• Tree Vocabulary: The topmost node is called root of the tree.
• The elements that are directly under an element are called its children.
• A tree is a nonlinear hierarchical data structure that consists of nodes
connected by edges.
• The element directly above something is called its parent.
• For example, ‘a’ is a child of ‘f’, and ‘f’ is the parent of ‘a’. Finally,
elements with no children are called leaves.
6. Tree Terminologies
• Node
• A node is an entity that contains a key or value and pointers to its
child nodes.
• The last nodes of each path are called leaf nodes or external
nodes that do not contain a link/pointer to child nodes.
• The node having at least a child node is called an internal node.
• Edge
• It is the link between any two nodes.
7. Cont…
• Root
• It is the topmost node of a tree.
• Height of a Node
• The height of a node is the number of edges from the node to the
deepest leaf (ie. the longest path from the node to a leaf node).
• Depth of a Node
• The depth of a node is the number of edges from the root to the node.
• Height of a Tree
• The height of a Tree is the height of the root node or the depth of the
deepest node.
8. Cont….
• Degree of a Node
• The degree of a node is the total number of branches of that node.
• Forest
• A collection of disjoint trees is called a forest.
• Types of Tree
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
9. Binary search trees
• Binary Search Tree is a node-based binary tree data structure
which has the following properties:
• The left subtree of a node contains only nodes with keys lesser
than the node’s key.
• The right subtree of a node contains only nodes with keys greater
than the node’s key.
• The left and right subtree each must also be a binary search tree.
11. Cont…..
• A Binary Search Tree (BST) is a tree in which all the nodes follow the below-
mentioned properties −
• The value of the key of the left sub-tree is less than the value of its parent (root)
node's key.
• The value of the key of the right sub-tree is greater than or equal to the value of its
parent (root) node's key.
• Following are the basic operations of a tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order manner.
• In-order Traversal − Traverses a tree in an in-order manner.
• Post-order Traversal − Traverses a tree in a post-order manner.
12. Node
• Define a node having some data, references to its left and right
child nodes.
• struct node {
• int data;
• struct node *leftChild;
• struct node *rightChild;
• };
13. Multidimensional arrays
• A multi-dimensional array is an array of arrays. 2-dimensional
arrays are the most commonly used. They are used to store data in
a tabular manner.
• Consider following 2D array, which is of the size 3×5. For an array
of size N×M, the rows and columns are numbered
from 0 to N−1 and columns are numbered from 0 to M−1,
respectively. Any element of the array can be accessed
by arr[i][j] where 0≤i<N and 0≤j<M. For example, in the following
array, the value stored at arr[1][3] is 14.
14. Cont…
• 2D array declaration:
• To declare a 2D array, you must specify the following:
• Row-size: Defines the number of rows
• Column-size: Defines the number of columns
• Type of array: Defines the type of elements to be stored in the
array i.e. either a number, character, or other such datatype. A
sample form of declaration is as follows:
• type arr[row_size][column_size]