Professional Documents
Culture Documents
Trees and Binary Trees
Trees and Binary Trees
Trees and Binary Trees
What is a Tree?
A tree is a finite nonempty set of elements.
A tree is a collection of nodes connected by directed (or undirected) edges. A tree is
a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear
data structures.
It is an abstract model of a hierarchical structure.
consists of nodes with a parent-child relation.
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.
Tree Terminologies
Root: node without parent (A)
Siblings: nodes share the same parent
Internal node: node with at least one child (A, B, C, A
F)
External node (leaf ): node without children (E, I, J,
K, G, H, D)
Parent: the node which has a branch from it to any
other node B C D
Descendant of a node: child, grandchild, grand-
grandchild, etc.
Depth of a node: number of ancestors
Height of a tree: maximum depth of any node (3) E F G H
Degree of a node: the number of its children
Degree of a tree: the maximum number of its node.
Subtree: tree consisting of a node and its
descendants
Edge: the connecting link between any two nodes I J K
note: in a tree with ‘n’ number of nodes, there will be n-1 number of edges
Child : the node in which is descendant of any node, so all nodes except root node are child nodes
Leaf nodes: the nodes which do not have a child, or also called non-terminal nodes.
Level: in tree, each step from top to bottom is called level count starts with ‘0’ and incremental by 1 at
each step
Forest: it is a set of disjoint trees
BINARY TREE
Binary tree is a special type of data structure. In binary tree, every node can have a maximum of
2 children, which are known as Left child and Right Child. It is a method of placing and locating the
records in a database, especially when all the data is known to be in random access memory (RAM).
Definition:
"A tree in which every node can have maximum of two children is called as Binary Tree."
The first index of an array that is '0‘ -stores the total number of nodes. All
nodes are numbered from left to right level by level from top to bottom. In a
tree, each node having an index i is put into the array as its i the element.
The above figure shows how a binary tree is represented as an array. Value
'7' is the total number of nodes. If any node does not have any of its child,
null value is stored at the corresponding index of the array.
BST is considered a data structure made up of nodes, like Linked Lists. These nodes are either null or
have references (links) to other nodes. These ‘other’ nodes are child nodes, called a left node and right
node. Nodes have values. These values determine where they are placed within the BST.
Note: Every binary search tree is a binary tree, but all the binary trees need not to be binary search
trees.
BASIC OPERATIONS
Insert − Inserts an element in a tree.
Search − Searches an element in a tree.
Transversal- is a process of accessing every node of the tree and exactly once. A tree is defined
in a recursive manner.
1.Insertion Operation
-Insert operation is performed with O(log n) time complexity in a binary search tree.
-nsert operation starts from the root node. It is used whenever an element is to be inserted
2. Search Operation
Search operation is performed with O(log n) time complexity in a binary search tree.
This operation starts from the root node. It is used whenever an element is to be searched.
The following algorithm shows the search operation in binary search tree:
Preorder Traversal : A B C D E F G H
3. 2. Postorder Traversal
Algorithm for postorder traversal
3. 3. Inorder Traversal
Algorithm for inorder traversal
Step 1 : Start from the Left Subtree.
Step 2 : Then, visit the Root.
Step 3 : Then, go to the Right Subtree.