Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Tree Operations - Data Structure and Algorithms - Lecture Notes | CS 245, Study notes of Data Structures and Algorithms

Material Type: Notes; Professor: Galles; Class: Data Struct & Algorithms; Subject: Computer Science; University: University of San Francisco (CA); Term: Summer III 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-0vadwz5kni
koofers-user-0vadwz5kni 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS245-2009S-07 Tree Operations 1
07-0: Binary Tree Definition
class Node {
Node() { }
Node(Comparable elem) {
element_ = element;
}
Node(Object element, Node left, Node right) {
element_ = element;
left_ = left;
right_ = right;
}
/* Access methods on next slide */
private Node left_;
private Node right_;
private Comparable element_;
}
07-1: Binary Tree Access Methods
Node left() { void setLeft(Node left) {
return left_; left_ = left;
} }
Node right() { void setRight(Node right) {
return right_; right_ = right;
} }
Comparable element() {
return element_;
}
void setElement(Comparable element) {
element_ = element;
}
07-2: Tree Operations Height
Returns the height of the tree
(Length of the path to the deepest leaf) + 1
pf3
pf4
pf5

Partial preview of the text

Download Tree Operations - Data Structure and Algorithms - Lecture Notes | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

07-0: Binary Tree Definition

class Node { Node() { } Node(Comparable elem) { element_ = element; } Node(Object element, Node left, Node right) { element_ = element; left_ = left; right_ = right; } /* Access methods on next slide */ private Node left_; private Node right_; private Comparable element_; }

07-1: Binary Tree Access Methods

Node left() { void setLeft(Node left) { return left_; left_ = left; } }

Node right() { void setRight(Node right) { return right_; right_ = right; } }

Comparable element() { return element_; }

void setElement(Comparable element) { element_ = element; }

07-2: Tree Operations – Height

  • Returns the height of the tree
    • (Length of the path to the deepest leaf) + 1

Height = 5 Height = 6 07-3: Tree Operations – Height

int height(Node tree) { if (tree == null) return 0; return 1 + MAX(height(tree.left()), height(tree.right())); }

07-4: Tree Operations – NumNodes

  • Returns the number of nodes in a tree

Number of Nodes = 8 Number of Nodes = 6 07-5: Tree Operations – NumNodes

int numNodes(Node tree) { if (tree == null) return 0; return 1 + numNodes(tree.left(), tree.right());

07-6: Writing Tree Functions

1

2

3

4

5

1

2 3 4 5

6

7 8

9 10 11 12

13

14 15

8

3

2 4

5

Tree1 (^) Tree

Tree

07-10: PREORDER Traversal

Printing out trees (Showing the shape of the tree in the printout)

A

B C

E F

G

A B C E G H H

D

D

F

07-11: PREORDER Traversal Printing out trees (Showing the shape of the tree in the printout)

  • First print the root at current indent level
    • Print the left subtree with larger indentation
    • Print the right subtree with larger indentation

07-12: Printing Binary Trees

void print(Node tree, int indent) { if (tree != null) { for(int i=0; i<indent; i++) { System.out.print("\t"); System.out.println(tree.element().toString()); print(tree.left(), indent + 1); print(tree.right(), indent + 1); }

07-13: INORDER Traversal Printing all elements in a Binary Search Tree in order

  • (Already covered in previous slides)

07-14: POSTORDER Traversal Calculating the Value of an expression tree

07-15: POSTORDER Traversal

Calculating the Value of an expression tree

  • Base case:
    • Return value stored at leaf
  • Recursive case:
    • Calculate value of left subtree
    • Calculate value of right subtree
    • Calculate expression value

07-16: Expression Tree Value

int value(Node tree) { if (tree.left() == null && tree.right() == null) return ((Integer) tree.element()).intValue(); int left = value(tree.left()); int right = value (tree.right()); char op = ((Character) tree.element()).charValue(); switch (op) { case ’+’: return left + right; case ’*’: return left * right; ... } }