You are on page 1of 64

Binary Trees

CS221(A) – Data Structures &


Algorithms
Binary Trees
• Tree with no node having more than two
children.
• A finite set of nodes, one of which is
designated as the root. The root node may
have at most two sub-trees, each of which is
also a binary tree. The two sub-trees of a
given node are ordered and we refer to them
as left child and the right child. Respectively.
Binary Trees
• Nodes in a binary tree A
may have zero, one or
two children. B C

D E F

G H
Binary Trees
• The maximum number of
A
nodes for an entire
binary tree of depth k is.
2k-1 for k ≥ 1 B C

• A full binary tree of


depth k is a binary tree F
D E
with 2k-1 nodes.
• That is the maximum
number of nodes a G H
binary tree can have.
Max. No of Node = 15
Binary Tree Implementation
• typedef struct TreeNode *PtrToNode;
• typedef struct *PtrToNode Tree;
• Typedef struct *PtrToNode Root;
• struct TreeNode
• {
– ElementType Element;
– Tree Left;
– Tree Right;
• }
Binary Tree Implementation
• Element Variable will serve as our data field.
• Left and Right points to the two sub-trees.
• Value NULL indicates the absence of a sub-
tree.
• Root points at the root node of the tree.
• Root == NULL indicates an empty tree.
Binary Tree Traversal
• Visiting each node exactly once, is called
Traversal.
• When positioned at any given node a traversal
function may
– Continue down to left sub-tree or
– Continue down to right sub-tree or
– Or process the current node
Binary Tree Traversal
• To process the data item we have the
following options.
– Visit the node before moving down the left sub-
tree. (Preorder)
– Visit the node after traversing the left sub-tree but
before traversing the right sub-tree. (Inorder)
– Visit the node after traversing both sub-trees.
(PostOrder)
Binary Tree Traversal
• Inorder Traversal
1. Move down the tree as far left as possible
2. Visit the current node
3. Backup one node in the tree and visit it.
4. Move down the right sub-tree of the node
visited in step 3.
5. Repeat the step 1 to 5 until all nodes have been
processed.
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2, 10,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2, 10, 5,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2, 10, 5, 1,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2, 10, 5, 1, 6,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2, 10, 5, 1, 6, 3,
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2, 10, 5, 1, 6, 3, 7
Binary Tree Traversal
• Inorder Traversal
1

2 3

4 5 6 7

8 9 10

Result : 8, 4, 9, 2, 10, 5, 1, 6, 3, 7
Binary Tree Traversal
• void InOrder(TreeNode Root)
• {
– if (Root !=null)
–{
• InOrder(RootLeft);
• PrintNode(RootElement);
• InOrder(RootRight);
–}
• }
Binary Tree Traversal
• Preorder Traversal
– Visit the data item.
– Visit the left sub tree.
– Visit the right sub tree.
Binary Tree Traversal
• Preorder Traversal
1

2 3

4 5 6 7

8 9 10
Binary Tree Traversal
• Preorder Traversal

2 3

4 5 6 7

8 9 10

Result : 1,
Binary Tree Traversal
• Preorder Traversal

4 5 6 7

8 9 10

Result : 1, 2,
Binary Tree Traversal
• Preorder Traversal

5 6 7

8 9 10

Result : 1, 2, 4,
Binary Tree Traversal
• Preorder Traversal

5 6 7

9 10

Result : 1, 2, 4, 8,
Binary Tree Traversal
• Preorder Traversal

5 6 7

10

Result : 1, 2, 4, 8, 9,
Binary Tree Traversal
• Preorder Traversal

6 7

10

Result : 1, 2, 4, 8, 9, 5,
Binary Tree Traversal
• Preorder Traversal

6 7

Result : 1, 2, 4, 8, 9, 5, 10,
Binary Tree Traversal
• Preorder Traversal

6 7

Result : 1, 2, 4, 8, 9, 5, 10, 3,
Binary Tree Traversal
• Preorder Traversal

Result : 1, 2, 4, 8, 9, 5, 10, 3, 6
Binary Tree Traversal
• Preorder Traversal

Result : 1, 2, 4, 8, 9, 5, 10, 3, 6, 7
Binary Tree Traversal
• Preorder Traversal

Result : 1, 2, 4, 8, 9, 5, 10, 3, 6, 7
Binary Tree Traversal
• void PreOrder(TreeNode Root)
• {
– if (Root !=null)
–{
• PrintNode(RootElement);
• PreOrder(RootLeft);
• PreOrder(RootRight);
–}
• }
Binary Tree Traversal
• Postorder Traversal
– Visit the left sub tree.
– Visit the right sub tree.
– Visit the data item.
Binary Tree Traversal
• Postorder Traversal

2 3

4 5 6 7

8 9 10
Binary Tree Traversal
• Postorder Traversal

2 3

4 5 6 7

9 10

Result : 8,
Binary Tree Traversal
• Postorder Traversal

2 3

4 5 6 7

10

Result : 8, 9
Binary Tree Traversal
• Postorder Traversal

2 3

5 6 7

10

Result : 8, 9, 4,
Binary Tree Traversal
• Postorder Traversal

2 3

5 6 7

Result : 8, 9, 4, 10,
Binary Tree Traversal
• Postorder Traversal

2 3

6 7

Result : 8, 9, 4, 10, 5,
Binary Tree Traversal
• Postorder Traversal

6 7

Result : 8, 9, 4, 10, 5, 2,
Binary Tree Traversal
• Postorder Traversal

Result : 8, 9, 4, 10, 5, 2, 6,
Binary Tree Traversal
• Postorder Traversal

Result : 8, 9, 4, 10, 5, 2, 6, 7
Binary Tree Traversal
• Postorder Traversal

Result : 8, 9, 4, 10, 5, 2, 6, 7, 3
Binary Tree Traversal
• Postorder Traversal

Result : 8, 9, 4, 10, 5, 2, 6, 7, 3, 1
Binary Tree Traversal
• Postorder Traversal

Result : 8, 9, 4, 10, 5, 2, 6, 7, 3, 1
Binary Tree Traversal
• void PostOrder(TreeNode Root)
• {
– if (Root !=null)
–{
• PostOrder(RootLeft);
• PostOrder(RootRight);
• PrintNode(RootElement);
–}
• }
Binary Tree Traversal
• Breadth First Traversal
– Process nodes by level
– Left to right within the level
Binary Tree Traversal
• Breadth First Traversal

2 3

4 5 6 7

8 9 10
Binary Tree Traversal
• Breadth First Traversal

2 3

4 5 6 7

8 9 10

Result : 1,
Binary Tree Traversal
• Breadth First Traversal

4 5 6 7

8 9 10

Result : 1, 2,
Binary Tree Traversal
• Breadth First Traversal

4 5 6 7

8 9 10

Result : 1, 2, 3,
Binary Tree Traversal
• Breadth First Traversal

5 6 7

8 9 10

Result : 1, 2, 3, 4,
Binary Tree Traversal
• Breadth First Traversal

6 7

8 9 10

Result : 1, 2, 3, 4, 5
Binary Tree Traversal
• Breadth First Traversal

8 9 10

Result : 1, 2, 3, 4, 5, 6,
Binary Tree Traversal
• Breadth First Traversal

8 9 10

Result : 1, 2, 3, 4, 5, 6, 7,
Binary Tree Traversal
• Breadth First Traversal

9 10

Result : 1, 2, 3, 4, 5, 6, 7, 8,
Binary Tree Traversal
• Breadth First Traversal

10

Result : 1, 2, 3, 4, 5, 6, 7, 8, 9,
Binary Tree Traversal
• Breadth First Traversal

Result : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Binary Tree Traversal
• Breadth First Traversal

Result : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Binary Tree Traversal
• void BreadthFirst(TreeNode
Root)
• {
• // Solve It
• }

You might also like