Trees, BT, BST
Trees, BT, BST
Trees
• A tree is a nonlinear data structure, compared to
arrays, linked lists, stacks and queues which are
linear data structures.
• A tree can be empty with no nodes or a tree is
a structure consisting of one node called the root
and zero or one or more subtrees.
• Trees are hierarchical data structure.
Tree Terminology
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root per tree
and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called
parent.
• Child − The node below a given node connected by its edge downward is called its
child node.
• Leaf − The node which does not have any child node is called the leaf node.
• Subtree − Subtree represents the descendants of a node.
• 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.
• Levels − 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.
• keys − Key represents a value of a node based on which a search operation is to be
carried out for a node.
• 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.
• 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).
• Height of the leaf node will be always zero.
• Depth of root node will be always zero.
• Depth of a Node: The depth of a node is the number of edges
from the root to the node.
Height of a tree