6. What is a Tree
A tree is a finite nonempty set
of elements.
It is an abstract model of a
hierarchical structure.
consists of nodes with a
parent-child relation.
Applications:
Organization charts
File systems
Programming environments
Computers”R”Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
8. Cont…
Root-The top most Node.
Children
Parents
Siblings- Have same parents.
Leaf- Has no Child.
RELATION OF TREE
1
3
2
4
6
5
8
7
11109
8
9. Trees Data Structures
Tree
Nodes
Each node can have 0 or more children
A node can have at most one parent
Binary tree
Tree with 0–2 children per node
Tree Binary Tree 9
10. Trees
Terminology
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
Root node
Leaf nodes
Interior nodes Height
10
12. Tree Terminology
Path: Traversal from node to node along the edges results
in a sequence called path.
Root: Node at the top of the tree.
Parent: Any node, except root has exactly one edge running
upward to another node. The node above it is called parent.
Child: Any node may have one or more lines running
downward to other nodes. Nodes below are children.
Leaf: A node that has no children.
Subtree: Any node can be considered to be the root of a
subtree, which consists of its children and its children’s
children and so on.
13. subtree
Tree Terminology Root: node without parent (A)
Siblings: nodes share the same parent
Internal node: node with at least one
child (A, B, C, F)
External node (leaf ): node without
children (E, I, J, K, G, H, D)
Ancestors of a node: parent, grandparent,
grand-grandparent, etc.
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)
Degree of a node: the number of its
children
Degree of a tree: the maximum number of
its node.
A
B DC
G HE F
I J K
Subtree: tree consisting of a
node and its descendants
14. General Trees
Trees store data in a hierarchical manner
Root node is A
A's children are B, C, and D
E, F, and D are leaves
Length of path from A to E is 2 edges
A
DB C
FE
Trees have layers of nodes,
where some are higher,
others are lower.
15. Logic of Tree
Used to represent hierarchical data.
BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A) Section (B)
CR 1 CR2
group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4
15
16. Logic of Tree
Used to represent hierarchical data.
BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A) Section (B)
CR 1 CR2
group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4
16
17. Cont…
A Collection of entities called Nodes.
Tree is a Non-Linear Data Structure.
It’s a hierarchica Structure.
TREE
Nodes
17
18. Tree Traversal
Unlike linear data structures (Array, Linked List,
Queues, Stacks, etc) which have only one logical way
to traverse them, trees can be traversed in different
ways.
Traversal is a process to visit all the nodes of a tree and
may print their values too. Because, all nodes are
connected via edges (links) we always start from the
root (head) node.
19. Cont…
That is, we cannot randomly access a node in a tree. There are
three ways which we use to traverse a tree
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
20. In-order Traversal
In this traversal method, the left subtree is visited first,
then the root and later the right sub-tree. We should
always remember that every node may represent a
subtree itself.
22. Inorder (Left, Root, Right)
We start from A, and following in-order traversal, we
move to its left subtree B. B is also traversed in-order.
The process goes on until all the nodes are visited. The
output of inorder traversal of this tree will be −
D → B → E → A → F → C → G
23. Inorder Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
24. Pre-order Traversal
In this traversal method, the root node is visited first,
then the left subtree and finally the right subtree.
26. Preorder (Root, Left, Right)
We start from A, and following pre-order traversal, we first visit A itself and
then move to its left subtree B. B is also traversed pre-order. The process goes
on until all the nodes are visited. The output of pre-order traversal of this tree
will be.
A → B → D → E → C → F → G
27. Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
28. Post-order Traversal
In this traversal method, the root node is visited last,
hence the name. First we traverse the left subtree, then
the right subtree and finally the root node.
30. Postorder (Left, Right, Root)
We start from A, and following pre-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all
the nodes are visited. The output of post-order traversal of this tree will
be.
D → E → B → F → G → C → A
31. Algorithm
Until all nodes are traversed
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
33. • If root is NULL
• then create root node
• return
• If root exists then
• compare the data with node.data
• while until insertion position is located
• If data is greater than node.data
• goto right subtree
• else
• goto left subtree
• endwhile
• insert data
• end If
34. If root.data is equal to search.data
return root
else
while data not found
If data is greater than node.data
goto right subtree
else
goto left subtree
If data found
return node
endwhile
return data not found
end if
37. There are a large number of tree types, however three types
specifically are used the majority of the time (both in real world
development and in Computer Science courses). The types are:
Binary trees
B-Trees
Heaps
38. Binary trees
Binary tree follows the rule that each parent node has
no more than 2 child nodes. This means that a binary
tree can look like this:
OR
39. Strict Binary Tree
Strict Binary Tree is a Binary tree in which root can
have exactly two children or no children at all. That
means, there can be 0 or 2 children of any node.
40. Complete Binary Tree
Complete Binary Tree is a Strict Binary tree in which
every leaf node is at same level. That means, there are
equal number of children in right and left subtree for
every node.
41. Extended Binary Tree
An extended binary tree is a transformation of any
binary tree into a complete binary tree.
This transformation consists of replacing every null
subtree of the original tree with “special nodes.”
The nodes from the original tree are then called as
internal nodes, while the “special nodes” are called as
external nodes.
42. Extended Binary Tree
A binary tree with special nodes replacing every null subtree. Every regular node
has two children, and every special node has no children.
43. Binary Search Tree
The value at any node, • Greater than every value in left subtree. Smaller than
every value in right subtree – Example
Y > X
Y < Z
Values in left sub tree less than parent
Values in right sub tree greater than parent
Fast searches in a Binary Search tree, maximum of log n comparisons.
45. B - Trees
B – Tree is another type of search tree called B-Tree in
which a node can store more than one value (key) and
it can have more than two children.
B-Tree can be defined as follows…
B-Tree is a self-balanced search tree with multiple keys in every node and more than two
children for every node.
46. Operations on a B-Tree
The following operations are performed on a B-Tree...
Search
Insertion
Deletion
47. Heap Tree
Heap is a special case of balanced binary tree data
structure where the root-node key is compared with its
children and arranged accordingly. If α has child
node β then
key(α) ≥ key(β)
A heap can be of two types
Min-Heap
Max-Heap
48. Max-Heap
Max-Heap − Where the value of the root node is
greater than or equal to either of its children.
51. Max Heap Construction Algorithm
Step 1 − Create a new node.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its
parent.
Step 4 − If value of parent is less than child, then swap
them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
52. Max Heap Deletion Algorithm
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its
parent.
Step 4 − If value of parent is less than child, then swap
them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
53. Min-Heap
Min-Heap − Where the value of the root node is less
than or equal to either of its children.