0% found this document useful (0 votes)
2 views

binary tree lect 1 and 2nd

The document provides an introduction to tree data structures, explaining key concepts such as nodes, edges, root, parent, child, siblings, leaf nodes, internal nodes, degree, level, height, depth, path, and sub-trees. It also covers binary trees, their types, and various traversal methods including pre-order, in-order, post-order, and level-order traversal. Additionally, it discusses how to create binary trees from data values in an array.

Uploaded by

qurexhimohib71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

binary tree lect 1 and 2nd

The document provides an introduction to tree data structures, explaining key concepts such as nodes, edges, root, parent, child, siblings, leaf nodes, internal nodes, degree, level, height, depth, path, and sub-trees. It also covers binary trees, their types, and various traversal methods including pre-order, in-order, post-order, and level-order traversal. Additionally, it discusses how to create binary trees from data values in an array.

Uploaded by

qurexhimohib71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Introduction to Tree

■ Tree is a non-linear data structure which is a collection of


data (Node) organized in hierarchical structure.
■ In tree data structure, every individual element is called
as Node. Node stores
– the actual data of that particular element and
– link to next element in hierarchical structure.

Tree with 11 nodes and 10 edges

asfandyar khan tareen 2


Tree Terminology
■ Root
■ In a tree data structure, the first node is called as Root Node.
Every tree must have root node. In any tree, there must be
only one root node. Root node does not have any parent.
(same as head in a LinkedList).

Here, A is the Root node

3
Tree Terminology
■ Edge
■ The connecting link between any two nodes is called an Edge.
In a tree with 'N' number of nodes there will be a maximum of
'N-1' number of edges.

Edge is the connecting link


between the two nodes

4
Tree Terminology
■ Parent
■ The node which is predecessor of any node is called
as Parent Node. The node which has branch from it to any
other node is called as parent node. Parent node can also be
defined as "The node which has child / children".

Here,
A is Parent of B and C
B is Parent of D, E and F
C is the Parent of G and H

5
Tree Terminology
■ Child
■ The node which is descendant of any node is called as CHILD
Node. In a tree, any parent node can have any number of
child nodes. In a tree, all the nodes except root are child
nodes.

Here,
B and C are Children of A
G and H are Children of C
K is a Child of G

6
Tree Terminology
■ Siblings
■ In a tree data structure, nodes which belong to same Parent
are called as Siblings. In simple words, the nodes with same
parent are called as Sibling nodes.

Here,
B and C are siblings
D, E and F are siblings
G and H are siblings

7
Tree Terminology
■ Leaf
■ The node which does not have a child is called as Leaf Node.
■ leaf node is also called as 'Terminal' node.

Here,
D, I, J, F. K and H are leaf
nodes

8
Tree Terminology
■ Internal Nodes
■ In a tree data structure, the node which has at least one child
is called as Internal Node.

Here,
A, B, E, C, G are Internal
Nodes

9
Tree Terminology
■ Degree
■ the total number of children of a node is called as Degree of
that Node. The highest degree of a node among all the nodes
in a tree is called as 'Degree of Tree'

Here,
Degree of A is 2
Degree of B is 3
Degree of F is 0

10
Tree Terminology
■ Level
■ In a tree data structure, the root node is said to be at Level 0
and the children of root node are at Level 1 and the children
of the nodes which are at Level 1 will be at Level 2 and so on.
In simple words, in a tree each step from top to bottom is
called as a Level and the Level count starts with '0' and
incremented by one at each level (Step).

asfandyar khan tareen 11


Tree Terminology
■ Height
■ the total number of edges from leaf node to a particular node
in the longest path is called the Height of that Node. In a tree,
height of the root node is said to be height of the tree. In a
tree, height of all leaf nodes is '0'.

Here,
Height of the tree is 3

12
Tree Terminology
■ Depth
■ The total number of edges from root node to a particular node
is called as Depth of that Node. In a tree, the total number of
edges from root node to a leaf node in the longest path is said
to be Depth of the tree.

Here,
Depth of the tree is 3

13
Tree Terminology
■ Path
■ The sequence of Nodes and Edges from one node to another
node is called a Path between that two Nodes. Length of a
Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.

Here,
Path between A and J:
A-B-E-J
Path between C and K:
C-G-K

14
Tree Terminology
■ Sub-tree
■ Each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.

15
Binary Tree
■ Binary tree is a special type of tree data structure in which
every node can have a maximum of 2 children.
– One is known as left child and
– the other is known as right child.
■ In a binary tree, every node can have either 0 children or 1
child or 2 children but not more than 2 children.

16
Types of Binary Tree
■ A full binary tree is a binary tree in which each node has
exactly zero or two children.
■ A complete binary tree is a binary tree, which is completely
filled, with the possible exception of the bottom level, which is
filled from left to right.

Full Tree Complete Tree

17
Binary Tree (tree.h)

18
Binary Tree Traversal
■ A traversal is a process that visits all the nodes in the tree.
Since a tree is a nonlinear data structure, there is no unique
traversal. We will consider several traversal algorithms with
we group in the following two kinds
– depth-first traversal
– breadth-first traversal
■ There are three different types of depth-first traversals:
– Pre Order traversal
– In Order traversal
– Post Order traversal

19
Binary Tree Traversal
■ Pre Order Traversal:
– Visit the root.
– Perform a preorder traversal of the left subtree.
– Perform a preorder traversal of the right subtree.

Pre Order - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3

20
Binary Tree Traversal
■ Preorder traversal

21
Binary Tree Traversal
■ In Order Traversal:
– Perform an in order traversal of the left subtree.
– Visit the root.
– Perform an in order traversal of the right subtree.

In Order - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11

22
Binary Tree Traversal
■ Inorder traversal

23
Binary Tree Traversal
■ Post Order Traversal:
– Perform a postorder traversal of the left subtree.
– Perform a postorder traversal of the right subtree.
– Visit the root.

PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8

24
Binary Tree Traversal
■ Postorder traversal

25
Binary Tree Traversal
■ There is only one kind of breadth-first traversal--the level order
traversal. This traversal visits nodes by levels from top to
bottom and from left to right.

LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2

26
Binary Tree Traversal
■ Level order traversal

Shebuti Rayana (CS, Stony Brook University) 27


Binary Tree Traversal
■ These common traversals can be represented as a single
algorithm by assuming that we visit each node three times.
■ An Euler tour is a walk around the binary tree where each
edge is treated as a wall, which you cannot cross. In this walk
each node will be visited either on the left, or from the below,
or on the right.
■ The Euler tour in which
– When we visit nodes on the left produces a preorder traversal
– When we visit nodes from the below, we get an inorder
traversal. And
– when we visit nodes on the right, we get a postorder traversal.

28
Binary Tree Traversal

29
Binary Tree Traversal: Example

Pre Order

In Order

Post Order

30
Binary Tree Traversal: Example

Pre Order
ABDFGCEHI
In Order
BFDGAEIHC
Post Order
FGDBIHECA

31
Creating Binary Trees

32
Creating Binary Trees
■ We will use these routines as primitives to create a binary tree
from data values in an array.
– There is a very nice mapping from the indices of a linear
array into nodes of a binary tree. We do this by taking the
value a[i] and letting it have as child a[2*i+1] and a[2*i+2].
– Then we map a[0] into the unique root node of the resulting
binary tree. Its left child will be a [1], and its right child will be
a [2].
– The function create_tree() embodies this mapping.
– The formal parameter size is the number of nodes in the
binary tree.

33
Creating Binary Trees

for query contact bachakhantareen16@gmail.com 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy