Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a...
-
Upload
simon-horn -
Category
Documents
-
view
217 -
download
0
Transcript of Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a...
![Page 1: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/1.jpg)
Introduction to Algorithms and Data Structures
Lecture 12 - “I think that I shall never see.. a data structure lovely as a”
Binary Tree
![Page 2: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/2.jpg)
What is a Binary Tree
• A binary tree is a a collection of nodes that consists of the root and other subsets to the root points, which are called the left and right subtrees.
• Every parent node on a binary tree can have up to two child nodes (roots of the two subtrees); any more children and it becomes a general tree.
• A node that has no children is called a leaf node.
![Page 3: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/3.jpg)
A Few Terms Regarding Binary Trees
A
E F
CB
G
D
A is the rootB and C are A’s childrenA is B’s parentB & C are the root of two subtreesD, E, and G are leaves
![Page 4: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/4.jpg)
This is NOT A Binary Tree
A
E F
CB
GD H
This is a general tree because C has three child nodes
![Page 5: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/5.jpg)
This is NOT A Binary Tree
A
E F
CB
GD
This is a graph because A, B, E, H, F and C form a circuit
H
![Page 6: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/6.jpg)
Tree Traversal
• There are three common ways to traverse a tree:– Preorder: Visit the root, traverse the left subtree
(preorder) and then traverse the right subtree (preorder)
– Postorder: Traverse the left subtree (postorder), traverse the right subtree (postorder) and then visit the root.
– Inorder: Traverse the left subtree (in order), visit the root and the traverse the right subtree (in order).
![Page 7: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/7.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECFGPostorder: DEBGFCAIn Order: DBEAFGC
![Page 8: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/8.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: A(visit each node as your reach it)
![Page 9: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/9.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: AB(visit each node as your reach it)
![Page 10: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/10.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABD(visit each node as your reach it)
![Page 11: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/11.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDE(visit each node as your reach it)
![Page 12: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/12.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDEC(visit each node as your reach it)
![Page 13: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/13.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECF(visit each node as your reach it)
![Page 14: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/14.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECFG(visit each node as your reach it)
![Page 15: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/15.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder:
![Page 16: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/16.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder:
![Page 17: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/17.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: D
![Page 18: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/18.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DE
![Page 19: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/19.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEB
![Page 20: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/20.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEB
![Page 21: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/21.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEB
![Page 22: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/22.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBG
![Page 23: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/23.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBGF
![Page 24: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/24.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBGFC
![Page 25: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/25.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Postorder: DEBGFCA
![Page 26: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/26.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order:
![Page 27: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/27.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order:
![Page 28: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/28.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: D
![Page 29: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/29.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DB
![Page 30: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/30.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBE
![Page 31: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/31.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEA
![Page 32: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/32.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEA
![Page 33: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/33.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEAF
![Page 34: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/34.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEAFG
![Page 35: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/35.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
In Order: DBEAFGC
![Page 36: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/36.jpg)
Tree Traversals: An ExampleA
E F
CB
G
D
Preorder: ABDECFGPostorder: DEBGFCAIn Order: DBEAFGC
![Page 37: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/37.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIECGPostorder: HIFDEBGCAIn Order: DHFIBEACG
G
H
![Page 38: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/38.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: A
G
H
![Page 39: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/39.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: AB
G
H
![Page 40: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/40.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABD
G
H
![Page 41: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/41.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDF
G
H
![Page 42: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/42.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFH
G
H
![Page 43: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/43.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHI
G
H
![Page 44: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/44.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIE
G
H
![Page 45: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/45.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIEC
G
H
![Page 46: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/46.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIECG
G
H
![Page 47: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/47.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
![Page 48: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/48.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
![Page 49: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/49.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
![Page 50: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/50.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder:
G
H
![Page 51: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/51.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: H
G
H
![Page 52: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/52.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HI
G
H
![Page 53: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/53.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIF
G
H
![Page 54: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/54.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFD
G
H
![Page 55: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/55.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDE
G
H
![Page 56: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/56.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEB
G
H
![Page 57: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/57.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEB
G
H
![Page 58: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/58.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEBG
G
H
![Page 59: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/59.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEBGC
G
H
![Page 60: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/60.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Postorder: HIFDEBGCA
G
H
![Page 61: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/61.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order:
G
H
![Page 62: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/62.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order:
G
H
![Page 63: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/63.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: D
G
H
![Page 64: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/64.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: D
G
H
![Page 65: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/65.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DH
G
H
![Page 66: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/66.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHF
G
H
![Page 67: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/67.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFI
G
H
![Page 68: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/68.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIB
G
H
![Page 69: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/69.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBE
G
H
![Page 70: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/70.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBEA
G
H
![Page 71: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/71.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBEAC
G
H
![Page 72: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/72.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
In Order: DHFIBEACG
G
H
![Page 73: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/73.jpg)
Tree Traversals: Another ExampleA
E
F
CB
I
D
Preorder: ABDFHIECGPostorder: HIFDEBGCAIn Order: DHFIBEACG
G
H
![Page 74: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/74.jpg)
Basic Implementation of a Binary Tree
• We can implement a binary in essentially the same way as a linked list, except that there are two nodes that comes next:public class Node {
private int data;
private Node left;
private Node right;
![Page 75: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/75.jpg)
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setData(int x) {
data = x;
}
![Page 76: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/76.jpg)
public void setLeft(Node p) {
left = p;
}
public void setRight(Node p) {
right = p;
}
}
![Page 77: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/77.jpg)
The tree Class
public class Tree {
private Node root;
// tree() - The default constructor – Starts
// the tree as empty
public Tree() {
root = null;
}
// Tree() - An initializing constructor that
// creates a node and places in it
// the initial value
![Page 78: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/78.jpg)
public Tree(int x) {
root = new Node();
root.setData(x);
root.setLeft(null);
root.setRight(null);
}
public Node getRoot() {
return root;
}
![Page 79: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/79.jpg)
// newNode() - Creates a new node with a
// zero as data by default
public Node newNode() {
Node p = new Node();
p.setData(0);
p.setLeft(null);
p.setRight(null);
return(p);
}
![Page 80: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/80.jpg)
// newNode() - Creates a new node with the
// parameter x as its value
public Node newNode(int x) {
Node p = new Node();
p.setData(x);
p.setLeft(null);
p.setRight(null);
return(p);
}
![Page 81: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/81.jpg)
//travTree() – initializes recursive
// traversal of tree
public void travTree() {
if (root != null)
travSubtree(root);
System.out.println();
}
//travSubtree() – recursive method used to
// traverse a binary tree (inorder)
public void travSubtree(Node p) {
if (p != null) {
travSubtree(p.getLeft());
System.out.print(p.getData()
+ "\t");
travSubtree(p.getRight());
}
}
![Page 82: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/82.jpg)
// addLeft() - Inserts a new node containing
// x as the left child of p
public void addLeft(Node p, int x) {
Node q = newNode(x);
p.setLeft(q);
}
// addRight() - Inserts a new node containing
// x as the right child of p
public void addRight(Node p, int x) {
Node q = newNode(x);
p.setRight(q);
}
}
![Page 83: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/83.jpg)
A Basic Search Tree
• We can construct a simple search tree if we add new nodes with value x on the tree using this strategy:– Every time x is less than the value in the node
we move down to the left.– Every time x is greater than the value in the
node we move down to the right.
![Page 84: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/84.jpg)
A Sample Search Tree
15
12 18
228
20
5 23
16107
![Page 85: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/85.jpg)
// insert() - Insert value x in a new node to
// be inserted after p
public void insert(int x) {
Node p, q;
boolean found = false;
p = root;
q = null;
while (p != null && !found) {
q = p;
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
![Page 86: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/86.jpg)
if (found)
error("Duplicate entry");
if (q.getData() > x)
addLeft(q, x);
else
addRight(q, x);
//q = newNode(x);
}
![Page 87: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/87.jpg)
// isXThere() - Is there a node in the
// tree containing x?
public boolean isXThere(int x) {
Node p;
boolean found = false;
p = root;
while (p != null && !found) {
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
return(found);
}
![Page 88: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/88.jpg)
public void error(String message) {
System.out.println(message);
System.exit(0);
}
![Page 89: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/89.jpg)
// getNode() - Get the pointer for the
// node containing x
public Node getNode(int x) {
Node p, q;
boolean found = false;
p = root;
q = null;
while (p != null && !found) {
q = p;
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
![Page 90: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/90.jpg)
if (found)
return(q);
else
return(null);
}
![Page 91: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/91.jpg)
public class TestTree {
public static void main(String[] args) {
Tree mytree = new Tree(8);
mytree.addLeft(mytree.getRoot(), 6);
mytree.addRight(mytree.getRoot(), 9);
mytree.insert(4);
mytree.insert(1);
mytree.insert(12);
if (mytree.isXThere(13))
System.out.println("great");
else
System.out.println("not great, Bob");
mytree.travTree();
}
}
![Page 92: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/92.jpg)
Tracing TestTree
8
![Page 93: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/93.jpg)
Tracing TestTree
8
6
![Page 94: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/94.jpg)
Tracing TestTree
8
96
![Page 95: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/95.jpg)
Tracing TestTree
8
96
4
![Page 96: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/96.jpg)
Tracing TestTree
8
96
4
1
![Page 97: Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.](https://reader036.fdocuments.us/reader036/viewer/2022062409/5697c0101a28abf838ccb062/html5/thumbnails/97.jpg)
Tracing TestTree
8
96
4 12
1