Download presentation

Presentation is loading. Please wait.

Published byBenedict Norman Modified about 1 year ago

1
Binary Trees CS 110: Data Structures and Algorithms First Semester,

2
Binary Tree ► An ordered tree with just two children ► Distinguished between “left” and “right” child ► Has a lot of uses, like heaps, expression trees, etc.

3
Binary Tree Properties ► Let ► n – number of nodes ► e – external nodes ► i – internal nodes ► h – height ► Properties ► e = i + 1 ► n = 2e – 1 ► h ≤ i ► h ≤ (n-1) / 2 ► e ≤ 2 n ► h ≥ log 2 e ► h ≥ log 2 (n+1) - 1

4
Binary Tree Implementation ► Array Implementation ► Elements are stored in an array ► Relationships are derived using indices ► Linked List Implementation ► Elements stored in a TreeNode, similar to the Node class used in Stack and Queue ► Each TreeNode has pointers to the element it contains, and then TreeNode references to its children ► Ideas here can be extended to implement other types of trees

5
Binary Tree Implementation ► Each element is stored in an array ► Root of the array is at array[0] ► Left child of the node at index j is at array[2j+1] ► Right child of the node at index j is at array[2j+2] ► How do you compute for the parent of the node at index j?

6
Binary Tree Array Example A BC D E H GF ABCDEFGH

7
Linked List Implementation ► Each element is stored in a TreeNode object ► TreeNode has “left” and “right” TreeNode fields, which correspond to its children ► TreeNode also has a “parent” field

8
BT Linked List Implementation Ø A Ø ØØØØØØ BC DEG leftright

9
Tree Search Algorithms ► Given a root of a tree, find a particular element ► Two approaches ► Breadth First Search ► Depth First Search

10
Breadth First Search ► Search per level ► Root first ► Children of root ► Children of the children of the root ► Etc ► Search left’s children before right’s ► Can be applied to other trees

11
Breadth First Search A BC D E H GF ► Top to bottom, left to right ► A, B, C, D, E, F, G, H

12
Breadth First Search ► Implementation requires a Queue data structure ► Put the root in the queue ► Dequeue an element from the queue ► If it’s the element we’re looking for, return ► Otherwise, queue its left and right child ► Repeat until the queue is empty, or until the element is found

13
Breadth First Search public TreeNode BFS(TreeNode root, Object element) { Queue q = new NodeQueue(); q.enqueue(root); while( !q.isEmpty() ) { TreeNode n = (TreeNode) q.dequeue(); if ( n.getElement().equals(element) ) { return n; } if ( q.getLeft() != null ) q.enqueue( q.getLeft() ); if ( q.getRight() != null ) q.enqueue( q.getRight() ); }

14
Depth First Search ► Start from the root, search downward ► Explore as far as possible until the goal node is reached, or there are no more children ► Backtrack when there are no more children ► Search the left branch before the right branch

15
Depth First Search A BC D E H GF ► Drill down, then go back, then right ► A, B, D, E, H, C, F, G

16
Depth First Search ► Adapt the code from the BFS, except use a Stack instead of a Queue ► Push the right child FIRST ► Better solution is to use recursion ► Check the current node’s element ► If it is not what is being searched ► DFS subtree rooted at the left child ► DFS subtree rooted at the right child

17
Depth First Search public TreeNode DFS(TreeNode root, Object element) { if ( root.getElement().equals(element) ) { return root; } TreeNode ret = null; if ( root.getLeft() != null ) { ret = DFS( root.getLeft(), element ); } if ( ret != null ) return ret; if ( root.getRight() != null ) { ret = DFS( root.getRight(), element ); } return ret; }

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google