Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms for Information Processing

Similar presentations


Presentation on theme: "Data Structures and Algorithms for Information Processing"— Presentation transcript:

1 Data Structures and Algorithms for Information Processing
Lecture 5: Trees Lecture 5: Trees

2 Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. Each node is either a leaf, having no children, or an internal node, with one or two children In many ways, a tree is like the other structures you have seen: A tree consists of nodes, and each node can contain data of one sort or another. Lecture 5: Trees

3 Binary Trees Lecture 5: Trees

4 Some Terminology Root is the unique node with no parent
Leaves or terminals are nodes with no children A subtree is a node together with all its descendents Level of a node is number of nodes on path from the node to root Lecture 5: Trees

5 Arithmetic Expressions A*(((B+C)*(D*E))+F)
Example use of Trees Arithmetic Expressions A*(((B+C)*(D*E))+F) Each internal node labeled by an operator Each leaf labeled by a variable or numeric value Tree determines a unique value Lecture 5: Trees

6 Example Use of Trees Representing Chinese Characters
Characters composed hierarchically into boxes Boxes can be oriented left-right, top-down, or outside-inside Each leaf labeled by one of radicals Lecture 5: Trees

7 Binary vs. Binary-Unary
Important distinction : Sometimes binary trees are defined to allow internal nodes with one or two children. There is a big difference... Lecture 5: Trees

8 Counting Trees The number of binary trees with internal nodes is
There is no such formula if we allow unary internal nodes! Lecture 5: Trees

9 Exercise Write efficient Java functions int numBTrees(int n)
int numBUTrees(int n) that return the number of binary trees and binary/unary trees, respectively. Lecture 5: Trees

10 Properties of Trees There is exactly one path connecting any two nodes
Any two nodes have a least common ancestor A tree with N internal nodes has N+1 external nodes (easy to prove by induction) Lecture 5: Trees

11 The Best Trees A binary tree is full if internal nodes fill every level, except possibly the last. The height of a full binary tree with internal nodes is Lecture 5: Trees

12 Complete Binary Trees A complete tree is one having levels and leaves
Complete trees have a simple implementation using arrays (How?) Lecture 5: Trees

13 Class for Binary Nodes public class BTNode { private Object data;
private BTNode left; private BTNode right; ... Lecture 5: Trees

14 Class for Binary Nodes public BTNode(Object obj, BTNode l, BTNode r) {
data = obj; left = l; right= r; } public boolean isLeaf() return (left == null) && (right == null); ... Lecture 5: Trees

15 Copying Trees public static BTNode treeCopy(BTNode t) {
if (t == null) return null; else BTNode leftCopy = treeCopy(t.left); BTNode rightCopy = treeCopy(t.right); return new BTNode(t.data, leftCopy, rightCopy); } Lecture 5: Trees

16 Tree Traversals Preorder traversal P M L E S R A A T E E
Lecture 5: Trees

17 Tree Traversals Inorder traversal P M L E S R A A T E E
Lecture 5: Trees

18 Tree Traversals Postorder traversal P M L E S R A A T E E
Lecture 5: Trees

19 Tree Traversals Level order traversal P M L E S R A A T E E
Lecture 5: Trees

20 Preorder Traversal Process the root
Process the nodes in the left subtree Process the nodes in the right subtree Lecture 5: Trees

21 Preorder Print public void preorderPrint() { System.out.println(data);
if (left != null) left.preorderPrint(); if (right != null) right.preorderPrint(); } Lecture 5: Trees

22 Inorder Traversal Profess the nodes in the left subtree
Process the root Process the nodes in the right subtree Lecture 5: Trees

23 Inorder Print public void preorderPrint() { if (left != null)
left.preorderPrint(); System.out.println(data); if (right != null) right.preorderPrint(); } Lecture 5: Trees


Download ppt "Data Structures and Algorithms for Information Processing"

Similar presentations


Ads by Google