CSCS-200 Data Structure and Algorithms. Lecture

Size: px
Start display at page:

Download "CSCS-200 Data Structure and Algorithms. Lecture"

Transcription

1 CSCS-200 Data Structure and Algorithms Lecture

2 Recursion

3 What is recursion? Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first Recursion is a technique that solves a problem by solving a smaller problem of the same type

4 When you turn this into a program, you end up with functions that call themselves (recursive functions) public int f(int x) { int y; } if(x==0) else { } return 1; y = 2 * f(x-1); return y+1;

5 Problems defined recursively There are many problems whose solution can be defined recursively Example: n factorial n!= 1 if n = 0 (n-1)!*n if n > 0 (recursive solution) n!= 1 if n = 0 1*2*3* *(n-1)*n if n > 0 (closed form solution)

6 Coding the factorial function Recursive implementation public int Factorial(int n) { if (n==0) // base case return 1; else return n * Factorial(n-1); }

7

8 Coding the factorial function (cont.) Iterative implementation public int Factorial(int n) { int fact = 1; for(int count = 2; count <= n; count++) fact = fact * count; } return fact;

9 Recursion vs. iteration Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code

10 How do I write a recursive function? Determine the size factor Determine the base case(s) (the one for which you know the answer) Determine the general case(s) (the one where the problem is expressed as a smaller version of itself) Verify the algorithm (use the "Three-Question-Method")

11 Three-Question Verification Method 1. The Base-Case Question: Is there a non-recursive way out of the function, and does the routine work correctly for this "base" case? 2. The Smaller-Caller Question: Does each recursive call to the function involve a smaller case of the original problem, leading inescapably to the base case? 3. The General-Case Question: Assuming that the recursive call(s) work correctly, does the whole function work correctly?

12 Binary Search public int ispresent(int *arr, int val, int N) { int low = 0; int high = N - 1; int mid; while ( low <= high ){ mid = ( low + high )/2; if (arr[mid]== val) return 1; // found! else if (arr[mid] < val) low = mid + 1; else high = mid - 1; } } return 0; // not found

13 Recursive Binary Search What is the size factor? The number of elements in (arr[first]... arr[last]) What is the base case(s)? (1) If low > high, return false (2) If val==arr[mid], return true What is the general case? if val < arr[mid] search the first half if val > arr[mid], search the second half

14 Recursive Binary Search public int BinarySearch(int *arr, int val, int low, int high) { int mid; if(low > high) // base case 1 return false; else { mid = (low + high)/2; if(val < arr[mid]) return BinarySearch(arr, val, low, mid-1); else if (val == arr[mid]) { // base case 2 val = arr[mid]; return true; } else return BinarySearch(arr, val, mid+1, high); } }

15 How is recursion implemented? What happens when a function gets called? public int a(int w) { return w+w; } int b(int x) { int z,y; // other statements z = a(x) + y; } return z;

16 What happens when a function is called? (cont.) An activation record is stored into a stack (runtime stack) 1) The computer has to stop executing function b and starts executing function a 2) Since it needs to come back to function b later, it needs to store everything about function b that is going to need (x, y, z, and the place to start executing upon return) 3) Then, x from a is bounded to w from b 4) Control is transferred to function a

17 What happens when a function is called? (cont.) After function a is executed, the activation record is popped out of the run-time stack All the old values of the parameters and variables in function b are restored and the return value of function a replaces a(x) in the assignment statement

18 What happens when a recursive function is called? Except the fact that the calling and called functions have the same name, there is really no difference between recursive and nonrecursive calls int f(int x) { int y; } if(x==0) return 1; else { y = 2 * f(x-1); return y+1; }

19 2*f(2) 2*f(1) 2*f(1) =f(0) =f(1) =f(2) =f(3)

20 Tree Data Structure

21 Tree Data Structures There are a number of applications where linear data structures are not appropriate. Consider a genealogy tree of a family. Mohammad Aslam Khan Sohail Aslam Javed Aslam Yasmeen Aslam Haaris Saad Qasim Asim Fahd Ahmad Sara Omer

22 Tree Data Structure A linear linked list will not be able to capture the treelike relationship with ease. Shortly, we will see that for applications that require searching, linear data structures are not suitable. We will focus our attention on binary trees.

23 Binary Tree A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees called the left and right subtrees. Each element of a binary tree is called a node of the tree.

24 Binary Tree Binary tree with 9 nodes. A B C D E F G H I

25 Binary Tree root A B C D E F G H I Left subtree Right subtree

26 Binary Tree Recursive definition root A B C D E F Left subtree G H I Right subtree

27 Binary Tree Recursive definition A B root C D E F G H I Left subtree

28 Binary Tree Recursive definition A B C D E F G root H I

29 Binary Tree Recursive definition A root B C D E F G H I Right subtree

30 Binary Tree Recursive definition A B C root D E F G H I Left subtree Right subtree

31 Not a Tree Structures that are not trees. A B C D E F G H I

32 Not a Tree Structures that are not trees. A B C D E F G H I

33 Not a Tree Structures that are not trees. A B C D E F G H I

34 Binary Tree: Terminology parent A Left descendant B C Right descendant D E F G H I Leaf nodes Leaf nodes

35 Binary Tree If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is termed a strictly binary tree. A B C D E J F G K H I

36 Level of a Binary Tree Node The level of a node in a binary tree is defined as follows: Root has level 0, Level of any other node is one more than the level its parent (father). The depth of a binary tree is the maximum level of any leaf in the tree.

37 Level of a Binary Tree Node A 0 Level 0 B 1 C 1 Level 1 D E F Level 2 G H I Level 3

38 Complete Binary Tree A complete binary tree of depth d is the strictly binary all of whose leaves are at level d. A 0 B 1 C 1 D 2 E 2 F 2 G 2 H 3 I J 3 K L M 3 3 N O 3 3

39 Complete Binary Tree A Level 0: 2 0 nodes B C Level 1: 2 1 nodes D E F G Level 2: 2 2 nodes H I J K L M N O Level 3: 2 3 nodes

40 Complete Binary Tree At level k, there are 2 k nodes. Total number of nodes in the tree of depth d: d = 2 j = 2 d+1 1 d In a complete binary tree, there are 2 d leaf nodes and (2 d - 1) non-leaf (inner) nodes. j=0

41 Complete Binary Tree If the tree is built out of n nodes then n = 2 d+1 1 or log 2 (n+1) = d+1 or d = log 2 (n+1) 1 I.e., the depth of the complete binary tree built using n nodes will be log 2 (n+1) 1. For example, for n=100,000, log 2 (100001) is less than 20; the tree would be 20 levels deep. The significance of this shallowness will become evident later.

42 Operations on Binary Tree There are a number of operations that can be defined for a binary tree. If p is pointing to a node in an existing tree then left(p) returns pointer to the left subtree right(p) returns pointer to right subtree parent(p) returns the father of p brother(p) returns brother of p. info(p) returns content of the node.

43 Operations on Binary Tree In order to construct a binary tree, the following can be useful: setleft(p,x) creates the left child node of p. The child node contains the info x. setright(p,x) creates the right child node of p. The child node contains the info x.

44 Applications of Binary Trees A binary tree is a useful data structure when two-way decisions must be made at each point in a process. For example, suppose we wanted to find all duplicates in a list of numbers: 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

45 Applications of Binary Trees One way of finding duplicates is to compare each number with all those that precede it. 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

46 Searching for Duplicates If the list of numbers is large and is growing, this procedure involves a large number of comparisons. A linked list could handle the growth but the comparisons would still be large. The number of comparisons can be drastically reduced by using a binary tree. The tree grows dynamically like the linked list.

47 Searching for Duplicates The binary tree is built in a special way. The first number in the list is placed in a node that is designated as the root of a binary tree. Initially, both left and right subtrees of the root are empty. We take the next number and compare it with the number placed in the root. If it is the same then we have a duplicate.

48 Searching for Duplicates Otherwise, we create a new tree node and put the new number in it. The new node is made the left child of the root node if the second number is less than the one in the root. The new node is made the right child if the number is greater than the one in the root.

49 Searching for Duplicates 14 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

50 Searching for Duplicates , 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

51 Searching for Duplicates , 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

52 Searching for Duplicates , 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

53 Searching for Duplicates , 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

54 Searching for Duplicates , 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

55 Searching for Duplicates , 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

56 Searching for Duplicates , 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

57 Searching for Duplicates , 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

58 Searching for Duplicates , 3, 5, 16, 4, 20, 17, 9, 14, 5

59 Searching for Duplicates , 3, 5, 16, 4, 20, 17, 9, 14, 5

60 Searching for Duplicates , 5, 16, 4, 20, 17, 9, 14, 5

61 Searching for Duplicates , 5, 16, 4, 20, 17, 9, 14, 5

62 Searching for Duplicates , 16, 4, 20, 17, 9, 14, 5

63 Searching for Duplicates , 16, 4, 20, 17, 9, 14, 5

64 Searching for Duplicates , 4, 20, 17, 9, 14, 5

65 Searching for Duplicates , 4, 20, 17, 9, 14, 5

66 Searching for Duplicates , 20, 17, 9, 14, 5

67 Searching for Duplicates , 17, 9, 14, 5

68 Searching for Duplicates , 17, 9, 14, 5

69 Searching for Duplicates , 9, 14, 5

70 Searching for Duplicates , 9, 14, 5

71 Searching for Duplicates , 14, 5

72 Implementation public class TreeNode { public: // constructors TreeNode() { this.object = NULL; this.left = this.right = NULL; }; TreeNode( int object ) { }; this.object = object; this.left = this.right = NULL;

73 Implementation public int getinfo() { return this.object; }; void setinfo(int object) { this.object = object; }; public TreeNode getleft() { return left; }; void setleft(treenode left) { this.left = left; };

74 Implementation public TreeNode getright() { return right; }; void setright(treenode right) { this.right = right; }; public int isleaf( ) { if( this.left == NULL && this.right == NULL ) return 1; return 0; };

75 Implementation private: int object; TreeNode left; TreeNode right; }; // end class TreeNode

76 Implementation import "TreeNode.java" public static void main(string args []) { } int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17, 9, 14,5, -1}; TreeNode root = new TreeNode(); root.setinfo( x[0] ); for(int i=1; x[i] > 0; i++ ) { } insert(root, x[i] );

77 Implementation void insert(treenode root, int info) { TreeNode node = new TreeNode(info); TreeNode p, q; p = q = root; while( info!= (p.getinfo()) q!= NULL ) { p = q; if( info < (p.getinfo()) ) q = p.getleft(); else q = p.getright(); }

78 Implementation if( info == (p.getinfo()) ){ print "attempt to insert duplicate: ; } else if( info < (p.getinfo()) ) p.setleft( node ); else p.setright( node ); } // end of insert

79 Trace of insert 17 p q , 9, 14, 5

80 Trace of insert 17 p 14 4 q , 9, 14, 5

81 Trace of insert p q , 9, 14, 5

82 Trace of insert p q , 9, 14, 5

83 Trace of insert p q , 9, 14, 5

84 Trace of insert p 18 7 q , 9, 14, 5

85 Trace of insert p q , 9, 14, 5

86 Trace of insert p q 17, 9, 14, 5

87 Trace of insert p node 17 17, 9, 14, 5 p.setright( node );

88 Cost of Search Given that a binary tree is level d deep. How long does it take to find out whether a number is already present? Consider the insert(17) in the example tree. Each time around the while loop, we did one comparison. After the comparison, we moved a level down.

89 Cost of Search With the binary tree in place, we can write a routine find(x) that returns true if the number x is present in the tree, false otherwise. How many comparison are needed to find out if x is present in the tree? We do one comparison at each level of the tree until either x is found or q becomes NULL.

90 Cost of Search If the binary tree is built out of n numbers, how many comparisons are needed to find out if a number x is in the tree? Recall that the depth of the complete binary tree built using n nodes will be log 2 (n+1) 1. For example, for n=100,000, log 2 (100001) is less than 20; the tree would be 20 levels deep.

91 Cost of Search If the tree is complete binary or nearly complete, searching through 100,000 numbers will require a maximum of 20 comparisons. Or in general, approximately log 2 (n). Compare this with a linked list of 100,000 numbers. The comparisons required could be a maximum of n.

92 Binary Search Tree A binary tree with the property that items in the left subtree are smaller than the root and items are larger or equal in the right subtree is called a binary search tree (BST). The tree we built for searching for duplicate numbers was a binary search tree. BST and its variations play an important role in searching algorithms.

93 Traversing a Binary Tree Suppose we have a binary tree, ordered (BST) or unordered. We want to print all the values stored in the nodes of the tree. In what order should we print them?

94 Traversing a Binary Tree Ways to print a 3 node tree: (4, 14, 15), (4,15,14) (14,4,15), (14,15,4) (15,4,14), (15,14,4)

95 Traversing a Binary Tree In case of the general binary tree: N node L left subtree right subtree R (L,N,R), (L,R,N) (N,L,R), (N,R,L) (R,L,N), (R,N,L)

96 Traversing a Binary Tree Three common ways N node L left subtree right subtree R Preorder: (N,L,R) Inorder: (L,N,R) Postorder: (L,R,N)

97 Traversing a Binary Tree public void preorder(treenode treenode) { if( treenode!= NULL ) { print(treenode.getinfo()); preorder(treenode.getleft()); preorder(treenode.getright()); } }

98 Traversing a Binary Tree void inorder(treenode treenode) { if( treenode!= NULL ) { inorder(treenode.getleft()); print(treenode.getinfo()); inorder(treenode.getright()); } }

99 Traversing a Binary Tree void postorder(treenode treenode) { if( treenode!= NULL ) { postorder(treenode.getleft()); postorder(treenode.getright()); print(treenode.getinfo()); } }

100 Traversing a Binary Tree Preorder:

101 Traversing a Binary Tree Inorder:

102 Traversing a Binary Tree Postorder:

103 Recursive Call Recall that a stack is used during function calls. The caller function places the arguments on the stack and passes control to the called function. Local variables are allocated storage on the call stack. Calling a function itself makes no difference as far as the call stack is concerned.

104 Stack Layout during a call Here is stack layout when function F calls function F (recursively): Parameters(F) Parameters(F) Parameters(F) Local variables(f) Local variables(f) Local variables(f) sp Return address(f) Parameters(F) Return address(f) Parameters(F) Local variables(f) sp Return address(f) sp Return address(f) At point of call During execution of F After call

105 Recursion: preorder preorder(14) 14..preorder(4) 4...preorder(3) 3...preorder(null)...preorder(null)...preorder(9) 9...preorder(7) 7...preorder(5) 5...preorder(null)...preorder(null)...preorder(null)...preorder(null)

106 Recursion: preorder preorder(15) 15...preorder(null)...preorder(18) 18...preorder(16) 16...preorder(null)...preorder(17) 17...preorder(null)...preorder(null)...preorder(20) 20...preorder(null)...preorder(null)

107 Recursion: inorder inorder(14)..inorder(4)...inorder(3)...inorder(null) 3...inorder(null) 4...inorder(9)...inorder(7)...inorder(5)...inorder(null) 5...inorder(null) 7...inorder(null) 9...inorder(null) 14

108 Recursion: inorder inorder(15)...inorder(null) 15...inorder(18)...inorder(16)...inorder(null) 16...inorder(17)...inorder(nul l) 17...inorder(nul l) 18...inorder(20)...inorder(null) 20...inorder(null)

109 Non Recursive Traversal We can implement non-recursive versions of the preorder, inorder and postorder traversal by using an explicit stack. The stack will be used to store the tree nodes in the appropriate order. Here, for example, is the routine for inorder traversal that uses a stack.

110 Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left-to-right order.

111 Level-order Traversal Level-order:

Lecture No.07. // print the final avaerage wait time.

Lecture No.07. // print the final avaerage wait time. Lecture No.0 Code for Simulation // print the final avaerage wait time. double avgwait = (totaltime*1.0)/count; cout

More information

24-Oct-18. Lecture No.08. Trace of insert. node 17, 9, 14, 5. p->setright( node );

24-Oct-18. Lecture No.08. Trace of insert. node 17, 9, 14, 5. p->setright( node ); Lecture No.08 Trace of insert p 16 20 1,,, node 1 p->setright( node ); 1 Cost of Search Given that a binary tree is level d deep. How long does it take to find out whether a number is already present?

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

More information

a graph is a data structure made up of nodes in graph theory the links are normally called edges

a graph is a data structure made up of nodes in graph theory the links are normally called edges 1 Trees Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in

More information

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Binary Search Trees Lecture 23 October 29, 2018 Prof. Zadia Codabux 1 Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2 Administrative

More information

Recursion, Binary Trees, and Heaps February 18

Recursion, Binary Trees, and Heaps February 18 Recursion, Binary Trees, and Heaps February 18 19 20 Recursion Review BST: Begin Slides Early Dismissal Problems on board BST Problems 23 24 25 26 27 Binary Tree problems QUIZ: Drawing trees and Traversals

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every

More information

Tree Structures. A hierarchical data structure whose point of entry is the root node

Tree Structures. A hierarchical data structure whose point of entry is the root node Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and

More information

Why Do We Need Trees?

Why Do We Need Trees? CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

Data Structures - Binary Trees and Operations on Binary Trees

Data Structures - Binary Trees and Operations on Binary Trees ata Structures - inary Trees and Operations on inary Trees MS 275 anko drovic (UI) MS 275 October 15, 2018 1 / 25 inary Trees binary tree is a finite set of elements. It can be empty partitioned into three

More information

[ DATA STRUCTURES ] Fig. (1) : A Tree

[ DATA STRUCTURES ] Fig. (1) : A Tree [ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

STUDENT LESSON AB30 Binary Search Trees

STUDENT LESSON AB30 Binary Search Trees STUDENT LESSON AB30 Binary Search Trees Java Curriculum for AP Computer Science, Student Lesson AB30 1 STUDENT LESSON AB30 Binary Search Trees INTRODUCTION: A binary tree is a different kind of data structure

More information

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture The concept of binary tree, its terms, and its operations Full binary tree theorem Idea

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

Trees. Truong Tuan Anh CSE-HCMUT

Trees. Truong Tuan Anh CSE-HCMUT Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes

More information

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Recursion and Binary Trees Lecture 21 October 24, 2018 Prof. Zadia Codabux 1 Agenda ArrayQueue.java Recursion Binary Tree Terminologies Traversal 2 Administrative

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-12-01 09:30:53 1/54 Chapter-11.pdf (#13) Terminology Definition of a general tree! A general tree T is a set of one or

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-03-25 21:47:41 1/53 Chapter-11.pdf (#4) Terminology Definition of a general tree! A general tree T is a set of one or more

More information

Introduction to Algorithms and Data Structures

Introduction to Algorithms and Data Structures Introduction to Algorithms and Data Structures Lecture 12 - I think that I shall never see.. a data structure lovely as a Binary Tree What is a Binary Tree A binary tree is a a collection of nodes that

More information

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD Data Structures Trees By Dr. Mohammad Ali H. Eljinini Trees Are collections of items arranged in a tree like data structure (none linear). Items are stored inside units called nodes. However: We can use

More information

CS24 Week 8 Lecture 1

CS24 Week 8 Lecture 1 CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:

More information

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge. Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

TREES Lecture 10 CS2110 Spring2014

TREES Lecture 10 CS2110 Spring2014 TREES Lecture 10 CS2110 Spring2014 Readings and Homework 2 Textbook, Chapter 23, 24 Homework: A thought problem (draw pictures!) Suppose you use trees to represent student schedules. For each student there

More information

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F)) DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]

More information

IX. Binary Trees (Chapter 10)

IX. Binary Trees (Chapter 10) IX. Binary Trees (Chapter 10) -1- A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following: a. If Loc >= length

More information

Tree Travsersals and BST Iterators

Tree Travsersals and BST Iterators Tree Travsersals and BST Iterators PIC 10B May 25, 2016 PIC 10B Tree Travsersals and BST Iterators May 25, 2016 1 / 17 Overview of Lecture 1 Sorting a BST 2 In-Order Travsersal 3 Pre-Order Traversal 4

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false. Name: Class: Date: CSCI-401 Examlet #5 True/False Indicate whether the sentence or statement is true or false. 1. The root node of the standard binary tree can be drawn anywhere in the tree diagram. 2.

More information

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the

More information

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 3: trees and visits Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International

More information

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. UNIT III TREES A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. Tree: A tree is a finite set of one or more nodes such that, there

More information

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1. Data Structures inary Trees number of leaves:?? height leaves Depth (Maximum level of the tree) leaves or nodes Root Level = 0 Level=1 57 feet root 2 Level=2 Number of nodes: 2 (2+1) - 1 = 7 2 inary Trees

More information

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

Introduction to Binary Trees

Introduction to Binary Trees Introduction to inary Trees 1 ackground ll data structures examined so far are linear data structures. Each element in a linear data structure has a clear predecessor and a clear successor. Precessors

More information

Data Structure - Binary Tree 1 -

Data Structure - Binary Tree 1 - Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures Linear Lists

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad SCJ2013 Data Structure & Algorithms Binary Search Tree Nor Bahiah Hj Ahmad Binary Search Tree A binary search tree has the following properties: For every node n in the tree Value of n is greater than

More information

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them. Laboratory 11: Expression Trees and Binary Search Trees Introduction Trees are nonlinear objects that link nodes together in a hierarchical fashion. Each node contains a reference to the data object, a

More information

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2 Revised 21-Mar-05 About This Lecture 2 Trees In this lecture we study a non-linear container called a Tree and a special kind of Tree called a Binary Tree. CMPUT 115 - Lecture 18 Department of Computing

More information

Friday Four Square! 4:15PM, Outside Gates

Friday Four Square! 4:15PM, Outside Gates Binary Search Trees Friday Four Square! 4:15PM, Outside Gates Implementing Set On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively. Let's now turn our attention to the Set.

More information

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees Why Use Binary Trees? - Binary Trees 1 Dr. TGI Fernando 1 2 February 24, 2012 Fundamental data structure Combines the advantages of an ordered array and a linked list. You can search an ordered array quickly

More information

TREES Lecture 12 CS2110 Fall 2016

TREES Lecture 12 CS2110 Fall 2016 TREES Lecture 12 CS2110 Fall 2016 Prelim 1 tonight! 2 5:30 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal 5:30 prelim (not the quiet room) and whose last names

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

Data Structures - Binary Trees 1

Data Structures - Binary Trees 1 Data Structures - Binary Trees 1 Dr. TGI Fernando 1 2 February 24, 2012 1 Email: gishantha@dscs.sjp.ac.lk 2 URL: http://tgifernando.wordpress.com/ Dr. TGI Fernando () Data Structures - Binary Trees 1 February

More information

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in

More information

Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.

Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root. 212 lgorithms & ata Structures Spring 05/06 Lecture Notes # 14 Outline ntroduction to Trees inary Trees: asic efinitions Traversing inary Trees Node Representation of inary Trees Primitive unctions in

More information

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures. Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,

More information

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the

More information

CSC/MAT-220: Lab 6. Due: 11/26/2018

CSC/MAT-220: Lab 6. Due: 11/26/2018 CSC/MAT-220: Lab 6 Due: 11/26/2018 In Lab 2 we discussed value and type bindings. Recall, value bindings bind a value to a variable and are intended to be static for the life of a program. Type bindings

More information

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology Chapter 11 Trees Definition of a general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called

More information

Algorithms. Deleting from Red-Black Trees B-Trees

Algorithms. Deleting from Red-Black Trees B-Trees Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that

More information

CMSC 341 Lecture 10 Binary Search Trees

CMSC 341 Lecture 10 Binary Search Trees CMSC 341 Lecture 10 Binary Search Trees John Park Based on slides from previous iterations of this course Review: Tree Traversals 2 Traversal Preorder, Inorder, Postorder H X M A K B E N Y L G W UMBC CMSC

More information

Trees. A tree is a directed graph with the property

Trees. A tree is a directed graph with the property 2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees

More information

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1! //16 Prelim 1 tonight! :3 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal :3 prelim (not the quiet room) and whose last names begin with A through Da MUST go

More information

Trees. CSE 373 Data Structures

Trees. CSE 373 Data Structures Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories

More information

ITEC2620 Introduction to Data Structures

ITEC2620 Introduction to Data Structures 9//207 ITEC2620 Introduction to Data Structures Lecture b Recursion and Binary Tree Operations Divide and Conquer Break a problem into smaller subproblems that are easier to solve What happens when the

More information

CMPT 225. Binary Search Trees

CMPT 225. Binary Search Trees CMPT 225 Binary Search Trees Trees A set of nodes with a single starting point called the root Each node is connected by an edge to some other node A tree is a connected graph There is a path to every

More information

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structure. IBPS SO (IT- Officer) Exam 2017 Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data

More information

Binary Trees and Binary Search Trees

Binary Trees and Binary Search Trees Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses

More information

Cpt S 122 Data Structures. Data Structures Trees

Cpt S 122 Data Structures. Data Structures Trees Cpt S 122 Data Structures Data Structures Trees Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Motivation Trees are one of the most important and extensively

More information

TREES Lecture 12 CS2110 Spring 2018

TREES Lecture 12 CS2110 Spring 2018 TREES Lecture 12 CS2110 Spring 2018 Important Announcements 2 A4 is out now and due two weeks from today. Have fun, and start early! Data Structures 3 There are different ways of storing data, called data

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Trees: Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals

More information

B-Trees. Based on materials by D. Frey and T. Anastasio

B-Trees. Based on materials by D. Frey and T. Anastasio B-Trees Based on materials by D. Frey and T. Anastasio 1 Large Trees n Tailored toward applications where tree doesn t fit in memory q operations much faster than disk accesses q want to limit levels of

More information

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked

More information

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms

More information

Spring 2018 Mentoring 8: March 14, Binary Trees

Spring 2018 Mentoring 8: March 14, Binary Trees CSM 6B Binary Trees Spring 08 Mentoring 8: March 4, 08 Binary Trees. Define a procedure, height, which takes in a Node and outputs the height of the tree. Recall that the height of a leaf node is 0. private

More information

CS350: Data Structures Tree Traversal

CS350: Data Structures Tree Traversal Tree Traversal James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Defining Trees Recursively Trees can easily be defined recursively Definition of a binary

More information

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find IX. Binary Trees IX-1 IX. Binary Trees (Chapter 10) A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following:

More information

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1 CS61B Lecture #20: Trees Last modified: Mon Oct 8 21:21:22 2018 CS61B: Lecture #20 1 A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive

More information

Binary trees. Binary trees. Binary trees

Binary trees. Binary trees. Binary trees Binary trees March 23, 2018 1 Binary trees A binary tree is a tree in which each internal node has at most two children. In a proper binary tree, each internal node has exactly two children. Children are

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees Issued: 24. 08. 2016 Exercise

More information

CSE 230 Intermediate Programming in C and C++ Recursion

CSE 230 Intermediate Programming in C and C++ Recursion CSE 230 Intermediate Programming in C and C++ Recursion Fall 2017 Stony Brook University Instructor: Shebuti Rayana What is recursion? Sometimes, the best way to solve a problem is by solving a smaller

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

Trees: examples (Family trees)

Trees: examples (Family trees) Ch 4: Trees it s a jungle out there... I think that I will never see a linked list useful as a tree; Linked lists are used by everybody, but it takes real smarts to do a tree Trees: examples (Family trees)

More information

Final Exam Data Structure course. No. of Branches (5)

Final Exam Data Structure course. No. of Branches (5) Page ١of 5 College Of Science and Technology Khan younis - Palestine Computer Science & Inf. Tech. Information Technology Data Structure (Theoretical Part) Time: 2 Hours Name: ID: Mark: Teacher 50 Mahmoud

More information

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012 Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root

More information

Binary Trees. Examples:

Binary Trees. Examples: Binary Trees A tree is a data structure that is made of nodes and pointers, much like a linked list. The difference between them lies in how they are organized: In a linked list each node is connected

More information

Chapter 5. Binary Trees

Chapter 5. Binary Trees Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children

More information

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures Outline Chapter 7: Trees 1 Chapter 7: Trees Approaching BST Making a decision We discussed the trade-offs between linked and array-based implementations of sequences (back in Section 4.7). Linked lists

More information

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department Abstract Data Structures IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4: Computational

More information

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms... First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and

More information

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children Trees Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program, the children may or may not be ordered Like a binary tree,

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Tree Implementations Kostas Alexis Nodes in a Binary Tree Representing tree nodes Must contain both data and pointers to node s children Each node will be an object

More information

Garbage Collection: recycling unused memory

Garbage Collection: recycling unused memory Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an

More information

COMP : Trees. COMP20012 Trees 219

COMP : Trees. COMP20012 Trees 219 COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural

More information

Building Java Programs

Building Java Programs Building Java Programs Binary Trees reading: 17.1 17.3 2 Trees in computer science TreeMap and TreeSet implementations folders/files on a computer family genealogy; organizational charts AI: decision trees

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees

More information

A set of nodes (or vertices) with a single starting point

A set of nodes (or vertices) with a single starting point Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of

More information