Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS223 Advanced Data Structures and Algorithms

Similar presentations


Presentation on theme: "CS223 Advanced Data Structures and Algorithms"— Presentation transcript:

1 CS223 Advanced Data Structures and Algorithms
Trees Neil Tang 01/26/2010 CS223 Advanced Data Structures and Algorithms

2 CS223 Advanced Data Structures and Algorithms
Class Overview Basics Binary Tree Binary Tree Traversal General Tree CS223 Advanced Data Structures and Algorithms

3 CS223 Advanced Data Structures and Algorithms
Basics Tree: A tree consists of a distinguished node r (root) and zero or more nonempty subtrees T1, T2, …Tk, each of whose roots are connected by a direct edge from r. Child, parent, leaf, sibling Path and path length Depth of a node, depth of a tree Height of a node, height of a tree Ancestor, descendant CS223 Advanced Data Structures and Algorithms

4 CS223 Advanced Data Structures and Algorithms
An Example CS223 Advanced Data Structures and Algorithms

5 CS223 Advanced Data Structures and Algorithms
Properties No cycle. There only exists a unique path between a pair of nodes on a tree. The height of a tree = the depth of that tree. CS223 Advanced Data Structures and Algorithms

6 CS223 Advanced Data Structures and Algorithms
Binary Tree Binary tree: A binary tree is a tree in which no node can have more than two children. Implementation The depth of a worst-case binary tree: N-1 CS223 Advanced Data Structures and Algorithms

7 CS223 Advanced Data Structures and Algorithms
Binary Tree Traversal Inorder-Tree-Traversal(t) if t  null then Inorder-Tree-Traversal(t.left) print(t.element) Inorder-Tree-Traversal(t.right) Preorder-Tree-Traversal(t) Preorder-Tree-Traversal(t.left) Preorder-Tree-Traversal(t.right) How about Postorder-Tree-Traversal? CS223 Advanced Data Structures and Algorithms

8 CS223 Advanced Data Structures and Algorithms
An Example Inorder: x*a*b+c Preorder: *x+*abc Postorder: xab*c+* CS223 Advanced Data Structures and Algorithms

9 CS223 Advanced Data Structures and Algorithms
General Tree Implementation CS223 Advanced Data Structures and Algorithms

10 General Tree Traversal
Preorder What is the first node visted? Last? CS223 Advanced Data Structures and Algorithms

11 General Tree Traversal
Postorder What is the first node visited? Last? CS223 Advanced Data Structures and Algorithms


Download ppt "CS223 Advanced Data Structures and Algorithms"

Similar presentations


Ads by Google