Tree Data Structure
By:Abdul Jalil Niazai
Tree Data Structure
A tree is an abstract data type that stores elements hierarchically. With the
exception of the top element, each element in a tree has a parent element and
zero or more children elements. A tree is usually visualized by placing
elements inside ovals or rectangles, and by drawing the connections between
parents and children With straight lines.
Formally, we define a tree T as a set of nodes storing elements such that the
nodes have parent-child relationship that satisfies the following properties:
• If T is nonempty, it has a special node, called the root of T, that has no
parent.
• Each node v of T different from the root has a unique parent node w,
every node with parent w is a child of w.
By:Abdul Jalil Niazai
Some terminology in Tree
Node: Is the fundamental part of Tree contains data.
Edge: Is straight-line connect two nodes, show relationship between them
Path: Sequence of nodes.
Root: The node at the top of the tree is called the root. There is only one root in a tree.
Parent: the node above it is called the parent of the node.
Child: The nodes below a given node are called its children.
Leaf: a node that has no children is called a leaf node or simply a leaf.
Internal node: A node with at least one child.
Siblings :Two nodes that are children of the same parent are.
Ancestor: A node reachable by repeated preceding from child to parent.
Descendent: A node reachable by repeated preceding from parent to child.
By:Abdul Jalil Niazai
Binary Tree
• In computer science, a binary tree is a tree data structure in
which each node has at most two children, which are referred
to as the left child and the right child.
• A node in binary tree doesn’t necessarily have the maximum
of two children; it may have only a left child, or on a right
child, or it can have no children at all.
• Maximum number of node at any level n is 2n
• The minimum number of level in n number of node tree is log
(n).
By:Abdul Jalil Niazai
Binary Tree
• The number of sub-trees of a node is called the degree of the node. In a
binary tree, all nodes have degree 0, 1, or 2.
• A node of degree zero is called a terminal node or leaf node.
• A non-leaf node is often called a branch node.
• The maximum degree of a node in the a binary tree is degree 2.
Binary Tree
By:Abdul Jalil Niazai
Types of binary Tree
• Complete binary tree: is a binary tree in which every level of the tree is
completely filled except the last level. Also, in the last level, nodes should be
attached starting from the left-most position.
• Full binary tree: is a binary tree in which every node in the tree has exactly two
children. In other words, every node in the tree except the leaves has exactly two
children.
By:Abdul Jalil Niazai
• A perfect binary tree is a binary tree in which all leaves have the
same depth or a binary tree where each level contains
the maximum number of nodes.
Formula for Maximum nodes height of “h” binary Tree is :
Nodes=2h+1 − 1
By:Abdul Jalil Niazai
Types of binary Tree
Binary Search Tree
• In computer science, binary search trees (BST), sometimes
called ordered or sorted binary trees. Binary Search tree is a binary tree in
which each internal node x stores an element such that the element stored in
the left sub-tree of x are less than or equal to x and elements stored in the
right sub-tree of x are greater than or equal to x. This is called binary-search-
tree property.
• In this case BST has the benefits of both sorted array and linked lists.
By:Abdul Jalil Niazai
Binary Search Tree
By:Abdul Jalil Niazai
Binary Search Tree
By:Abdul Jalil Niazai