You are on page 1of 64

# CSC258/438 DATA STRUCTURES

Zulaile Mabni

## TOPIC 7 BINARY TREE

1

CHAPTER OBJECTIVES
To learn how to use a tree to represent a hierarchical organization of information. To learn how to use recursion to process trees. To understand the different ways of traversing a tree. To understand the difference between binary trees, and binary search trees. To learn how to implement binary trees, and binary search trees using linked data structures and arrays.

Malik D.S.

TREE TERMINOLOGY
A tree consists of a collection of elements or nodes, with each node linked to its successors. The node at the top of a tree is called its root. The links from a node to its successors are called branches. The successors of a node are called its children. The predecessor of a node is called its parent.

Malik D.S.

## TREE TERMINOLOGY (CONTINUED)

Each node in a tree has exactly one parent except for the root node, which has no parent. Nodes that have the same parent are siblings. A node that has no children is called a leaf node. A generalization of the parent-child relationship is the ancestor-descendent relationship.

Malik D.S.

BINARY TREES
A binary tree is a hierarchical structure. Definition: A binary tree, T, is either empty or such that:

Malik D.S.

T has a special node called the root node; T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively; LT and RT are binary trees

BINARY TREE

Malik D.S.

Malik D.S.

## The root node of the binary tree = A

LA = empty RA = empty
7

Malik D.S.

Malik D.S.

## VARIOUS BINARY TREES WITH THREE NODES

10

Malik D.S.

BINARY TREES
Following class defines the node of a binary tree:
protected class BinaryTreeNode { Object info; BinaryTreeNode llink; BinaryTreeNode rlink; }
Malik D.S.

11

NODES

## For each node:

Data is stored in info The reference to the left child is stored in llink The reference to the right child is stored in rlink

Malik D.S.

12

13

Malik D.S.

## BINARY TREE DEFINITIONS

Leaf:

node that has no left and right children. Parent: node with at least one child node. Level of a node: number of branches on the path from root to node.
The level of the root node is 0 The level of the children of the root node is 1

Malik D.S.

Height

of a binary tree: The length of the path from the root to the deepest node in the tree. A (rooted) tree with only a node (the root) has a height of zero.

14

## HEIGHT OF A BINARY TREE

Recursive algorithm to find height of binary tree: (height(p) denotes height of binary tree with root p):

Malik D.S.

## If the binary tree is empty, height is 0 If the binary tree is nonempty:

Find the height of left subtree & right subtree Find the maximum of these two heights and add 1

if(p is NULL)

height(p) = 0
else height(p) = 1 + max(height(p.llink),height(p.rlink))

15

## HEIGHT OF A BINARY TREE

Method to implement above algorithm:
Malik D.S.

private int height(BinaryTreeNode p) { if(p == NULL) return 0; else return 1 + max(height(p.llink), height(p.rlink)); }

16

Malik D.S.

Height = 3
17

Expression tree

## Each node contains an operator or an operand

Malik D.S.

Huffman tree
Represents Huffman codes for characters that might appear in a text file Huffman code uses different numbers of bits to encode letters as opposed to ASCII or Unicode

## Binary search trees

All elements in the left subtree precede those in the right subtree
18

19

Malik D.S.

## FULL AND COMPLETE BINARY TREE

A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node has zero or two children except for the leaves. A perfect binary tree (sometimes complete binary tree) is a full binary tree in which all leaves are at the same depth.

20

Malik D.S.

Malik D.S.

or

## Three different traversals

Inorder Preorder Postorder

21

TRAVERSALS

Inorder
Traverse the left subtree Visit the node Traverse the right subtree

Malik D.S.

Preorder
Visit the node Traverse the left subtree Traverse the right subtree

22

TRAVERSALS

Postorder
Traverse the left subtree Traverse the right subtree Visit the node

Malik D.S.

23

## BINARY TREE: TRAVERSAL

Malik D.S.

Inorder

:BDAC
24

Preorder : A B D C Postorder: D B C A

## BINARY TREE: INORDER TRAVERSAL

private void inorder(BinaryTreeNode p) { if(p != NULL) { inorder(p.llink); System.out.println(p.info + ); inorder(p.rlink); } }

25

Malik D.S.

## BINARY TREE: PREORDER TRAVERSAL

private void preorder(BinaryTreeNode p) { if(p != NULL) { System.out.println(p.info + ); preorder(p.llink); preorder(p.rlink); } }

26

Malik D.S.

## BINARY TREE: POSTORDER TRAVERSAL

private void postorder(BinaryTreeNode p) { if(p != NULL) { postorder(p.llink); postorder(p.rlink); System.out.println(p.info + ); } }

27

Malik D.S.

28

Malik D.S.

## Data in each node

Larger than the data in its left child Smaller than the data in its right child

Malik D.S.

## A binary search tree, t, is either empty or:

T has a special node called the root node T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively Key in root node larger than every key in left subtree and smaller than every key in right subtree LT and RT are binary search trees

29

30

Malik D.S.

## OPERATIONS PERFORMED ON BINARY SEARCH TREES

Determine whether the binary search tree is empty Search the binary search tree for a particular item Insert an item in the binary search tree Delete an item from the binary search tree

31

Malik D.S.

## OPERATIONS PERFORMED ON BINARY SEARCH TREES

Find the height of the binary search tree Find the number of nodes in the binary search tree Find the number of leaves in the binary search tree Traverse the binary search tree

32

Malik D.S.

Exercises:

50

30

90
Malik D.S.

12

40

86

100

Determine the order in which the elements would be accessed during an in-order traversal.
33

1. inOrder(t) { if (t is not empty) { inOrder(leftTree(t)); access the root element of t; inOrder(rightTree(t)); } // if } // inOrder traversal Left Root Right

34

Malik D.S.

50
30 12 90
Malik D.S.

40 86

100

## Answer: 12, 30, 40, 50, 86, 90, 100

35

2.

postOrder (t) { if (t is not empty) { postOrder(leftTree(t)); postOrder(rightTree(t)); access the root element of t; } // if } // postOrder traversal Left Right Root

36

Malik D.S.

Malik D.S.

## Post-order traversal ( Left- Right- Node): Answer: 12 40 30 86 100 90 50

37

3.

preOrder (t) { if (t is not empty) { access the root element of t; preOrder (leftTree (t)); preOrder (rightTree (t)); } // if } // preOrder traversal

Malik D.S.

38

Malik D.S.

39

## TRAVERSALS OF EXPRESSION TREES

An inorder traversal of an expression tree inserts parenthesis where they belong (infix form) A postorder traversal of an expression tree results in postfix form A preorder traversal of an expression tree results in prefix form

40

Malik D.S.

## TRAVERSALS OF EXPRESSION TREES

An inorder traversal of an expression tree inserts parenthesis where they belong. An infix expression is obtained.

41

Malik D.S.

Determine the order in which the elements would be accessed during a post-order traversal. Hint: An operator immediately follows its operands.
+

Malik D.S.

*
42

Answer: X, Y, -, Z, A, B, *, / +

Malik D.S.

Postfix!

43

Determine the order in which the elements would be accessed during a pre-order traversal. Hint: An operator immediately precedes its operands.
+

Malik D.S.

*
44

Malik D.S.

Answer: +, -, X, Y, /, Z, *, A, B

Prefix!

45

46

Malik D.S.

47

Malik D.S.

add (73); 80

40

90

Malik D.S.

60

48

50

75

## After inserting (73)

80 40 90
Malik D.S.

60

50

75

73
49

Given the following data: HP, Compaq, Apple, Sony, Samsung, Compact, Toshiba, Acer, Dell Draw the Binary Search Tree

Malik D.S.

50

## BINARY SEARCH TREE DELETION

After deleting the desired item, the resulting tree must be a binary search tree. Four cases (Refer to binary search tree on the next slide):

1) The node to be deleted has no left and right subtrees. Example: Node with data 46

Malik D.S.

2) The node to deleted has no left subtree but has a nonempty right subtree. Example: Node with data 30
3) The node to be deleted has no right subtree but has a nonempty left subtree Example: Node with data 80 4) The node to be deleted has nonempty left and right subtrees. Example: Node with data 50

51

52

Malik D.S.

53

Malik D.S.

Exercises:

remove (50);

80 40 90
Malik D.S.

60

50

75
54

73

## After removing 50:

remove (40); 80 40 90
Malik D.S.

60

75
55

73

## After removing 40:

Malik D.S.

80 60 90

75

73
56

remove (80);

80 60 110
Malik D.S.

75

100

150

73

85

105
57

95

The element 80 has two children, so we cannot simply unlink 80 from the tree: that would create a hole.

Of the elements already in the tree, two could replace 80 (and then have the original deleted) without destroying the binary search tree properties. Which two?

58

Malik D.S.

We can replace 80 with either its predecessor, 75, or its successor, 85. The successor of an element is the leftmost element in the right subtree. The predecessor of an element is the rightmost element in the left subtree. Well choose its successor. Replace 80 with 85, and then remove 85.
59
Malik D.S.

85 60 110

Malik D.S.

75

100

150

73

95

105
60

## CLASS TREESET AND INTERFACE SEARCH TREE

61

Malik D.S.

BINARYSEARCHTREE CLASS

62

Malik D.S.

CHAPTER REVIEW

A tree is a recursive, nonlinear data structure that is used to represent data that is organized as a hierarchy. A binary tree is a collection of nodes with three components: a reference to a data object, a reference to a left subtree, and a reference to a right subtree. In a binary tree used for arithmetic expressions, the root node should store the operator that is evaluated last. A binary search tree is a tree in which the data stored in the left subtree of every node is less than the data stored in the root node, and the data stored in the right subtree is greater than the data stored in the root node.

63

Malik D.S.

REFERENCES

Collin, Williams, Data Structures and The Java Collections Framework, 2nd Edition, McGraw Hill , 2005. Koffman E., Wolfgang P., Objects, Abstraction, Data Structures And Design Using Java, John Wiley & Sons, 2005. Malik D.S, Nair P.S., Data Structures Using Java, Course Technology, 2003.

Malik D.S.

64