lecture objectives
DESCRIPTION
Lecture Objectives. To learn how to use a tree to represent a hierarchical organization of information To learn how to use recursion to process trees To understand the different ways of traversing a tree To understand the difference between binary trees, binary search trees, and heaps. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/1.jpg)
CS340
1
Lecture Objectives To learn how to use a tree to represent a
hierarchical organization of information To learn how to use recursion to process
trees To understand the different ways of
traversing a tree To understand the difference between
binary trees, binary search trees, and heaps
![Page 2: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/2.jpg)
Trees - Introduction
CS340
Data organizations Linear: one predecessor or successor Nonlinear: multiple predecessors,
successors Accessing all elements in a linear
sequence is O(n) Trees are nonlinear and hierarchical
Tree nodes can have multiple successors (but only one predecessor)
2
![Page 3: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/3.jpg)
Trees - Introduction (cont.)
CS340
Trees can represent : class hierarchy disk directory and subdirectories family tree
Trees are recursive data structures Many methods to process trees are
written recursively
3
![Page 4: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/4.jpg)
Binary trees
CS340
Each element has two successors Binary trees can be represented by
arrays and linked data structures Searching in a binary search tree
generally more efficient than searching in an ordered list
4
![Page 5: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/5.jpg)
Tree Terminology and Applications
CS340
![Page 6: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/6.jpg)
Tree Terminology
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
6
![Page 7: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/7.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successorsThe node at the
top of a tree is called its root
7
![Page 8: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/8.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
The links from a node to its successors are called branches
8
![Page 9: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/9.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successorsThe successors of
a node are called its children
9
![Page 10: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/10.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
The predecessor of a node is called its parent
Each node in a tree has exactly one parent except for the root node, which has no parent
10
![Page 11: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/11.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
Nodes that have the same parent are siblings
11
![Page 12: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/12.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
A node that has no children is called a leaf node
Leaf nodes also are known as
external nodes, and nonleaf
nodes are known as
internal nodes
12
![Page 13: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/13.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
A generalization of the parent-child relationship is the
ancestor-descendant relationship
13
![Page 14: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/14.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
A subtree of a node is a tree whose root is a child of that node
14
![Page 15: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/15.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
The level of a node is a
measure of its distance from the
root plus 1
Level 1
Level 2
Level 3
15
![Page 16: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/16.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successors
Level 1
Level 2
Level 3
The level of a node is defined
recursively
• If node n is the root of tree T, its level is 1• If node n is not the root of tree T, its level is
1 + the level of its parent
16
![Page 17: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/17.jpg)
Tree Terminology (cont.)
CS340
wikipedia
science arts
mathematics
A tree consists of a collection of elements or nodes, with each node linked to its successorsThe height of a tree is the number of nodes in the longest path from the root node to a leaf node
The height of this tree is 3
17
![Page 18: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/18.jpg)
CS340
18
Binary Trees In a binary tree, each node has two
subtrees A set of nodes T is a binary tree if either
of the following is true T is empty Its root node has two subtrees, TL and TR,
such that TL and TR are binary trees(TL = left subtree; TR = right subtree)
![Page 19: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/19.jpg)
CS340
19
Expression Tree Each node contains an
operator or an operand Operands are stored in
leaf nodes Tree structure dictates the order of operand evaluation No parenthesis needed
(x + y) * ((a + b) / c)
![Page 20: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/20.jpg)
20
Binary Search Tree Binary search trees
All elements in the left subtree precede those in the right subtree
A formal definition:A set of nodes T is a binary search tree if either of the following is true: T is empty If T is not empty, its root node has two subtrees, TL
and TR, such that TL and TR are binary search trees the values in the root node of T is greater than all values
in TL and is less than all values in TR
encyclopedia
arts science
mathematics
![Page 21: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/21.jpg)
Binary Search Tree (cont.)
CS340
Does not need to be sorted Why?
When new elements are inserted (or removed) properly, the binary search tree maintains its order
Compare to an array
![Page 22: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/22.jpg)
Binary Search Tree (cont.)
CS340
Searching a BST can be O(log n) What is the worst case?
![Page 23: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/23.jpg)
Recursive Algorithm for Searching a Binary Tree
CS340
1. if the tree is empty2. return null (target is not found)
else if the target matches the root node's data3. return the data stored at the root node
else if the target is less than the root node's data4. return the result of searching the left subtree of the root
else
5. return the result of searching the right subtree of the root
![Page 24: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/24.jpg)
CS340
24
Full, Perfect, and Complete Binary Trees
Full binary tree: all nodes have either 2 children or 0 children (the leaf nodes)
7101
12930
52 11
64
13
![Page 25: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/25.jpg)
CS340
25
Full, Perfect, and Complete Binary Trees (cont.)
Perfect binary tree is a full binary tree of height n with exactly 2n – 1 nodes
Find n in this example
3
1
420
5
6
![Page 26: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/26.jpg)
CS340
26
Full, Perfect, and Complete Binary Trees (cont.)
Complete binary tree is a perfect binary tree through level n - 1 with some extra leaf nodes at level n (the tree height), all toward the left
3
1
420
5
![Page 27: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/27.jpg)
CS340
27
General Trees Nodes of a general tree can have any
number of subtrees
![Page 28: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/28.jpg)
CS340
28
General Trees (cont.)
A general tree can be represented using a binary tree
![Page 29: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/29.jpg)
Tree Traversals
Click icon to add picture
CS340
![Page 30: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/30.jpg)
CS340
30
Tree Traversals Walking through the tree in a prescribed
order and visiting the nodes as they are encountered
Three common kinds of tree traversal Inorder Preorder Postorder
![Page 31: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/31.jpg)
CS340
31
Tree Traversals (cont.) Preorder: visit root node, traverse TL,
traverse TR Inorder: traverse TL, visit root node,
traverse TR Postorder: traverse TL, traverse TR, visit
root node
![Page 32: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/32.jpg)
CS340
32
Visualizing Tree Traversals
If we keep following the tree to the left, it will trace a route known as the Euler tour
![Page 33: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/33.jpg)
CS340
33
Visualizing Tree Traversals (cont.)
A Euler tour (blue path) is a preorder traversal
The sequence in this example isa b d g e h c f i j
![Page 34: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/34.jpg)
CS340
34
Visualizing Tree Traversals (cont.)
If we record a node as we return from traversing its left subtree we get an
inorder traversal The sequence is
d g b h e a i f j c
![Page 35: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/35.jpg)
CS340
35
Visualizing Tree Traversals (cont.)
If we record each node as we last encounter it, we get a postorder traversal
The sequence isg d h e b i j f c a
![Page 36: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/36.jpg)
CS340
36
Traversals of Binary Search Trees and Expression Trees
With inorder traversal nodes are visited in sequence
Bob, Helen, Peter, Stacy
Peter
Helen
Stacy
Bob
![Page 37: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/37.jpg)
CS340
37
Traversals of Binary Search Trees and Expression Trees (cont.)
An inorder traversal of an expression tree results in the sequencex + y * a + b / c
Or with parentheses (x + y) * ((a + b)) / c)
*
/+
c+yx
ba
![Page 38: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/38.jpg)
CS340
38
Traversals of Binary Search Trees and Expression Trees (cont.)
A postorder traversal x y + a b + c / *
Postfix form of the expression
Operators follow operands
*
/+
c+yx
ba
![Page 39: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/39.jpg)
CS340
39
Traversals of Binary Search Trees and Expression Trees (cont.)
A preorder traversal * + x y / + a b c Prefix form of the
expression Operators precede
operands
*
/+
c+yx
ba
![Page 40: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/40.jpg)
Implementing a BinaryTree Class
CS340
![Page 41: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/41.jpg)
CS340
41
Node<E> Class The data part is a
reference to type E A binary tree node must
have links to both its left and right subtrees
![Page 42: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/42.jpg)
CS340
42
Node<E> Class (cont.)protected static class Node<E> implements Serializable {
protected E data;protected Node<E> left;protected Node<E> right;
public Node(E data) { this.data = data; left = null; right = null;}
public String toString() {return data.toString();
}}
Node<E> is declared as an inner class within
BinaryTree<E>
![Page 43: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/43.jpg)
CS340
43
Node<E> Class (cont.)protected static class Node<E> implements Serializable {
protected E data;protected Node<E> left;protected Node<E> right;
public Node(E data) { this.data = data; left = null; right = null;}
public String toString() {return data.toString();
}}
Node<E> is declared protected. This way we
can use it as a superclass.
![Page 44: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/44.jpg)
44
BinaryTree<E> Class (cont.)
![Page 45: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/45.jpg)
CS340
45
BinaryTree<E> Class (cont.)
Assuming the tree is referenced by variable bT (type BinaryTree)
then . . .
![Page 46: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/46.jpg)
CS340
46
BinaryTree<E> Class (cont.)
bT.root.data references the
Character object storing '*'
![Page 47: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/47.jpg)
CS340
47
BinaryTree<E> Class (cont.)bT.root.left
references the left subtree of the root
![Page 48: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/48.jpg)
CS340
48
BinaryTree<E> Class (cont.)bT.root.right
references the right subtree of the root
![Page 49: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/49.jpg)
CS340
49
BinaryTree<E> Class (cont.)bT.root.right.data
references the Character object storing
'/'
![Page 50: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/50.jpg)
50
BinaryTree<E> Class (cont.)
![Page 51: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/51.jpg)
BinaryTree<E> Class (cont.)
CS340
Class heading and data field declarations:
import java.io.*;
public class BinaryTree<E> implements Serializable {// Insert inner class Node<E> here
protected Node<E> root;
. . .}
![Page 52: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/52.jpg)
BinaryTree<E> Class (cont.)
CS340
The Serializable interface defines no methods
It provides a marker for ObjectOutputStream: classes can be
written to a binary file ObjectInputStream: classes can be read
from a binary file
![Page 53: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/53.jpg)
Constructors
CS340
The no-parameter constructor:
public BinaryTree() {
root = null;}
The constructor that creates a tree with a given node at the root:
protected BinaryTree(Node<E> root) {
this.root = root;}
![Page 54: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/54.jpg)
Constructors (cont.)
The constructor that builds a tree from a data value and two trees:
public BinaryTree(E data, BinaryTree<E> leftTree, BinaryTree<E> rightTree) {
root = new Node<E>(data);if (leftTree != null) {
root.left = leftTree.root;} else {
root.left = null;}if (rightTree != null) {
root.right = rightTree.root;} else {
root.right = null;}
}
![Page 55: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/55.jpg)
getLeftSubtree and getRightSubtree Methods
CS340
public BinaryTree<E> getLeftSubtree() { if (root != null && root.left != null) {
return new BinaryTree<E>(root.left);} else {
return null}
}
getRightSubtree method is symmetric
![Page 56: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/56.jpg)
isLeaf Method
CS340
public boolean isLeaf() { return (root.left == null && root.right == null);
}
![Page 57: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/57.jpg)
toString Method
CS340
Generates a string representing a preorder traversal
public String toString() { StringBuilder sb = new StringBuilder(); preOrderTraverse(root, 1, sb); return sb.toString();
}
![Page 58: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/58.jpg)
preOrderTraverse Method
private void preOrderTraverse(Node<E> node, int depth, StringBuilder sb) {
for (int i = 1; i < depth; i++) { sb.append(" ");
} if (node == null) {
sb.append("null\n"); } else {
sb.append(node.toString()); sb.append("\n"); preOrderTraverse(node.left, depth + 1, sb); preOrderTraverse(node.right, depth + 1, sb);
}}
![Page 59: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/59.jpg)
preOrderTraverse Method (cont.)
CS340
* + x null null y null null / a null null b null null
*
+
ayx
/
b
(x + y) * (a / b)
![Page 60: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/60.jpg)
Reading a Binary Tree
If we use a Scanner to read the individual lines created by the toString and preOrderTraverse methods, we can reconstruct the tree
1. Read a line that represents information at the root2. Remove the leading and trailing spaces using String.trim3. if it is "null"4. return null
else5. recursively read the left child6. recursively read the right child7. return a tree consisting of the root and the two
children
![Page 61: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/61.jpg)
Reading a Binary Tree (cont.)
CS340
public static BinaryTree<String> readBinaryTree(Scanner scan) {
String data = scan.next();if (data.equals("null")) {
return null;} else {
BinaryTree<String> leftTree = readBinaryTree(scan); BinaryTree<String> rightTree = readBinaryTree(scan); return new BinaryTree<String>(data, leftTree, rightTree);
}}
![Page 62: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/62.jpg)
Using ObjectOutputStream and ObjectInputStream
CS340
The Java API includes the class ObjectOutputStream that will write to an external file any object that is declared to be Serializable
To declare an object Serializable, addimplements Serializable
to the class declaration
![Page 63: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/63.jpg)
Using ObjectOutputStream and ObjectInputStream (cont.)
CS340
To write a Serializable object to a file:
try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename)); out.writeObject(nameOfObject);
} catch (Exception ex) { ex.printStackTrace(); System.exit(1);
}
A deep copy of all the nodes of the binary tree will be written to the file
![Page 64: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/64.jpg)
Using ObjectOutputStream and ObjectInputStream (cont.)
CS340
To read a Serializable object from a file:
try {ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
objectName = (objectClass)in.readObject();} catch (Exception ex) {
ex.printStackTrace(); System.exit(1);
}
![Page 65: Lecture Objectives](https://reader035.vdocuments.site/reader035/viewer/2022081514/568162a1550346895dd31903/html5/thumbnails/65.jpg)
Using ObjectOutputStream and ObjectInputStream (cont.)
CS340
Do not recompile the Java source file for a class after an object of that class has been serialized
Even if you didn't make any changes to the class, the resulting .class file associated with the serialized object will have a different class signature
When you attempt to read the object, the class signatures will not match, and you will get an exception