110 likes | 132 Views
This class overview covers the basics of trees, binary trees, general trees, and their traversal algorithms. It also includes examples and properties of trees.
E N D
Trees Neil Tang01/26/2010 CS223 Advanced Data Structures and Algorithms
Class Overview • Basics • Binary Tree • Binary Tree Traversal • General Tree 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
An Example 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
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
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) if t null then print(t.element) Preorder-Tree-Traversal(t.left) Preorder-Tree-Traversal(t.right) How about Postorder-Tree-Traversal? 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
General Tree • Implementation CS223 Advanced Data Structures and Algorithms
General Tree Traversal • Preorder • What is the first node visted? Last? CS223 Advanced Data Structures and Algorithms
General Tree Traversal • Postorder • What is the first node visited? Last? CS223 Advanced Data Structures and Algorithms