Trees 1 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

What are trees?

It is a non-linear ADT, which stores the information naturally in the form of hierarchy style. It
represents the nodes connected by edges.

Key Terms Description


Root Root is a special node in a tree. The entire tree is referenced through 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.
Siblings Nodes with the same parent are called Siblings.
Path 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.
Height of Tree Height of tree represents the height of its root node.
Depth of Node Depth of a node represents the number of edges from the tree's root
node to the node.
Degree of Node Degree of a node represents a number of children of a node.
Edge Edge is a connection between one node to another. It is a line between
two nodes or a node and a leaf.
keys Key represents a value of a node based on which a search operation is to
be carried out for a node.

Levels of a node

Levels of a node represents the number of connections between the node and the root. It
represents generation of a node. If the root node is at level 0, its next node is at level 1, its grand
child is at level 2 and so on. Levels of a node can be shown as follows:

If node has no children, it is called Leaves or External Nodes.

Nodes which are not leaves, are called Internal Nodes. Internal nodes have at least one child.
A tree can be empty with no nodes or a tree consists of one node called the Root.

Height of a Node

Height of a node is a number of edges on the longest path between that node and a leaf. Each node
has height. In the below figure, A, B, C, D can have height. Leaf cannot have height as there will be
no path starting from a leaf. Node A's height is the number of edges of the path to K not to D. And its
height is 3.

Height of a node defines the longest path from the node to a leaf.

Path can only be downward.

Depth of a Node

While talking about the height, it locates a node at bottom where for depth, it is located at top
which is root level and therefore we call it depth of a node.

Types of Trees

Types of trees depend on the number of children a node has. There are two major tree types:

General Tree: A tree in which there is no restriction on the number of children a node has, is called a
General tree. Examples are Family tree, Folder Structure.

Binary Tree: In a Binary tree, every node can have at most 2 children, left and right. In diagram
below, B & D are left children and C, E & F are right children.
Full Binary Tree: It is a special kind of a binary tree that has either zero children or two children. It
means that all the nodes in that binary tree should either have two child nodes of its parent node or
the parent node is itself the leaf node or the external node. In other words, a full binary tree is a
unique binary tree where every node except the external node has two children. When it holds a
single child, such a binary tree will not be a full binary tree. Here, the quantity of leaf nodes is equal
to the number of internal nodes plus one. The equation is like L=I+1, where L is the number of leaf
nodes, and I is the number of internal nodes.

Figure 1: Full Binary Tree

Complete Binary Tree: A complete binary tree is another specific type of binary tree where all the
tree levels are filled entirely with nodes, except the lowest level of the tree. Also, in the last or the
lowest level of this binary tree, every node should possibly reside on the left side.

Figure 2: Complete Binary Tree

You might also like