Top Banner
1 Data Structures – Binary Trees 4 Binary Trees
36

MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

Apr 06, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 1/36

1Data Structures – Binary Trees

4 Binary Trees

Page 2: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 2/36

2Data Structures – Binary Trees

ObjectivesAt the end of the lesson, the student should be able to:

● Explain the basic concepts and definitions related to binarytrees

Identify the properties of a binary tree● Enumerate the different types of binary trees

● Discuss how binary trees are represented in computer memory

Traverse binary trees using the three traversal algorithms:preorder, inorder, postorder 

● Discuss binary tree traversal applications

● Use heaps and the heapsort algorithm to sort a set of elements

Page 3: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 3/36

3Data Structures – Binary Trees

Binary Tree

● An ADT that is hierarchical in nature

● Collection of nodes which are either empty or consists of aroot and two disjoint binary trees called the left and the right

subtrees

● Level of a node - distance of the node from the root

Page 4: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 4/36

4Data Structures – Binary Trees

Binary Tree

● Height or depth of a tree - level of the bottommost nodes;length of the longest path from the root to any leaf 

● External node - node w/o children; internal otherwise

● Proper binary tree - binary tree that has either zero or twochildren

Page 5: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 5/36

5Data Structures – Binary Trees

Binary Tree

●  

Page 6: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 6/36

6Data Structures – Binary Trees

Properties

● For a (proper) binary tree of depth k,

 – The maximum number of nodes at level i is 2i , i ≥ 0

 – The number of nodes is at least 2k + 1 and at most 2k+1 – 1

 – The number of external nodes is at least h+1 and at most 2k

 – The number of internal nodes is at least h and at most 2k – 1

 – If no is the number of terminal nodes and n2 is the number of nodes

of degree 2 in a binary tree, then no

= n2

+ 1

Page 7: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 7/36

7Data Structures – Binary Trees

Types

● right (left) skewed binary tree - tree in which every nodehas no left (right) subtrees; tree with the greatest depth

Page 8: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 8/36

8Data Structures – Binary Trees

Types

● strictly binary tree - tree in which every node has either two subtrees or none at all

Page 9: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 9/36

9Data Structures – Binary Trees

Types

● full binary tree - strictly binary tree in which all terminalnodes lie at the bottom-most level; has the maximumnumber of nodes for a given depth

Page 10: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 10/36

10Data Structures – Binary Trees

Types

● complete binary tree - tree which results when zero or more nodes are deleted from a full binary tree in reverse-level order 

Page 11: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 11/36

11Data Structures – Binary Trees

Representation

● Link Representationclass BTNode {

Object info;BTNode left, right;

public BTNode(){}

public BTNode(Object i) {info = i;

}

public BTNode(Object i, BTNode l, BTNode r) {

info = i;left = l;right = r;

}} 

Page 12: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 12/36

12Data Structures – Binary Trees

Representation

●  

Page 13: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 13/36

13Data Structures – Binary Trees

Traversals

● A procedure that visits the nodes of the binary tree in alinear fashion such that each node is visited exactly once,visit a node means performing computations local to thenode

● Preorder 

● Inorder 

● Postorder 

Page 14: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 14/36

14Data Structures – Binary Trees

Traversals

● Preorder traversal – NLR traversal

If the binary tree is empty, do nothing (traversal done)Otherwise:

Visit the root.Traverse the left subtree in preorder.Traverse the right subtree in preorder.

Page 15: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 15/36

15Data Structures – Binary Trees

Traversals

● Preorder traversal – NLR traversal

/* Outputs the preorder listing of elements in this tree */

void preorder(){

if (root != null) {System.out.println(root.info.toString());

new BinaryTree(root.left).preorder();

new BinaryTree(root.right).preorder();

}

}

Page 16: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 16/36

16Data Structures – Binary Trees

Traversals

● Inorder traversal – LNR traversal

If the binary tree is empty, do nothing (traversal done)Otherwise:

Traverse the left subtree in inorder.Visit the root.Traverse the right subtree in inorder.

Page 17: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 17/36

17Data Structures – Binary Trees

Traversals

● Inorder traversal – LNR traversal

/* Outputs the inorder listing of elements in this tree */

void inorder(){

if (root != null) {

new BinaryTree(root.left).inorder();

System.out.println(root.info.toString());

new BinaryTree(root.right).inorder();

}

}

Page 18: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 18/36

18Data Structures – Binary Trees

Traversals

● Postorder traversal – LRN traversal

If the binary tree is empty, do nothing (traversal done)Otherwise:

Traverse the left subtree in postorder.Traverse the right subtree in postorder.Visit the root.

Page 19: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 19/36

19Data Structures – Binary Trees

Traversals

● Postorder traversal – LRN traversal

/* Outputs the postorder listing of elements in this tree */

void postorder(){

if (root != null) {

new BinaryTree(root.left).postorder();

new BinaryTree(root.right).postorder();

System.out.println(root.info.toString());

}

}

Page 20: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 20/36

20Data Structures – Binary Trees

Traversals

OCCURENCES OF VISITS

● Preorder and Inorder:

 – On going down leftward as its left subtree is traversed

 – On going up from the left after its left subtree is traversed

● Postorder:

 – On going down leftward as its left subtree is traversed

 – On going up from the left after its left subtree has been traversed

 – On going up from the right after its right subtree has been traversed

Page 21: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 21/36

21Data Structures – Binary Trees

Traversal Examples

Page 22: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 22/36

22Data Structures – Binary Trees

Traversal Examples

Page 23: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 23/36

23Data Structures – Binary Trees

Traversal ApplicationDuplicating a Binary Tree

● The Algorithm

● Traverse the left subtree of node a in postorder and make a copy of it● Traverse the right subtree of node a in postorder and make a copy of it

● Make a copy of node a and attach copies of its left and right subtrees

Page 24: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 24/36

24Data Structures – Binary Trees

Traversal ApplicationDuplicating a Binary Tree

/* Copies this tree and returns the root of the duplicate */

BTNode copy(){BTNode newRoot;

BTNode newLeft;

BTNode newRight;if (root != null){

newLeft = new BinaryTree(root.left).copy();newRight = new BinaryTree(root.right).copy();newRoot = new BTNode(root.info, newLeft, newRight);

return newRoot;

}

return null;}

Page 25: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 25/36

25Data Structures – Binary Trees

Traversal ApplicationEquivalence of Two Binary Trees

● The Algorithm

● Check whether node a and node b contain the same data● Traverse the left subtrees of node a and node b in preorder and check

whether they are equivalent

● Traverse the right subtrees of node a and node b in preorder andcheck whether they are equivalent

Page 26: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 26/36

26Data Structures – Binary Trees

Traversal ApplicationEquivalence of Two Binary Trees

boolean equivalent(BinaryTree t2){

boolean answer = false;

if ((root == null) && (t2.root == null))

answer = true;

else {

answer = (root.info.equals(t2.root.info));if (answer) {

answer = new BinaryTree(root.left);

equivalent(new BinaryTree(t2.root.left));

}

equivalent(new BinaryTree(t2.root.right));

}

return answer;

}

Page 27: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 27/36

27Data Structures – Binary Trees

Exercises

Tree 1

Tree 2

Tree 3

Page 28: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 28/36

28Data Structures – Binary Trees

Binary Tree Application:

Heaps and Heapsort● Heaps

 – complete binary tree that has elements stored at its nodes

 – satisfies the heap-order property: for every node u  except the root,the key stored at u  is less than or equal to the key stored at its

parent

 – In this application, elements stored in a heap satisfy the total order :For any objects x, y and z in set S:

● Transitivity: if x < y and y < z then x < z● Trichotomy: for any two objects x and y in S, exactly one of these

relations holds: x > y, x = y or x < y

Page 29: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 29/36

29Data Structures – Binary Trees

Heaps

●  

Page 30: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 30/36

30Data Structures – Binary Trees

Sequential Representation

● Complete binary tree

Page 31: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 31/36

31Data Structures – Binary Trees

Sequential Representation

Properties of a heap represented as a complete binary tree:

● If 2i ≤ n, the left son of node i is 2i; otherwise, node i has

no left son

● If 2i < n (2i + 1 ≤ n in Java), the right son of node i is 2i + 1;otherwise, node i has no right son

● If 1 < i ≤ n, the father of node i is (i)/2

Page 32: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 32/36

32Data Structures – Binary Trees

Heaps and Sift-Up

 – Sift-Up - bottom-up, right-to-left process of converting a completebinary tree into a heap

 – Example

Page 33: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 33/36

33Data Structures – Binary Trees

Sift-UpJava Implementation

● /* Converts an almost-heap on n nodes rooted at i into a heap */

● public void siftUp (int i, int n) {

 – int k = key[i]; /* keep key at root of almost-heap */

 – int child = 2 * i; /* left child */

 –

while (child <= n) {● if (child < n && key[child+1]> key[child]) child++ ;

● /* if heap property is not satisfied */

● if (key[child] > k){

 – key[i] = key[child] ; /* Move the child up */

 – i = child ;

 – child = 2 * i; /*Consider the left child again*/ 

 – } else break;

 – }

 – key[i] = k ; /* this is where the root belongs */

 – }

Page 34: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 34/36

34Data Structures – Binary Trees

Heapsort

● Heapsort algorithm put forth in 1964 by R. W. Floyd and J.W. J. Williams

1. Assign the keys to be sorted to the nodes of a complete binary tree.

2. Convert this binary tree into a heap by applying sift-up to its nodes in

reverse level order.

3. Repeatedly do the following until the heap is empty:

a)Remove the key at the root of the heap (the smallest in the heap) andplace it in the output.

b)Detach from the heap the rightmost leaf node at the bottommost level,

extract its key, and store this key at the root of the heap.c)Apply sift-up to the root to convert the binary tree into a heap once

again.

Page 35: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 35/36

35Data Structures – Binary Trees

HeapsortJava Implementation

 – /* Performs an in-place sort of the keys in O(nlog2n) time */public void sort(){

int n = key.length-1;

● /* Convert key into almost-heap */

● for (int i=n/2; i>1; i--){

 – siftUp(i, n);

● }

● /* Exchange the current largest in key[1] with key[i] */

● for (int i=n; i>1; i--){

 – siftUp(1, i); – int temp = key[i];

 – key[i] = key[1];

 – key[1] = temp;

● }

 – }

Page 36: MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter04 Binary Trees

http://slidepdf.com/reader/full/meljuncortesjedi-slides-data-structures-chapter04-binary-trees 36/36

36Data Structures – Binary Trees

Summary

● A binary tree is an abstract data type that is hierarchical instructure

● A binary tree could be classified as skewed, strict, full or complete

● Link representation is the most natural way to represent a binary

tree● Three ways to traverse a tree are preorder, inorder, and postorder 

● Binary tree traversals could be used in applications such asduplication of a binary tree and checking the equivalence of twobinary trees

● The heapsort algorithm utilizes the heap ADT and runs inO(n lg n) time