Professional Documents
Culture Documents
Trees 1 3
Trees 1 3
Trees 1 3
It is a non-linear ADT, which stores the information naturally in the form of hierarchy style. It
represents the nodes connected by edges.
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:
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.
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.
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.