650 likes | 796 Vues
This lecture explores the fundamental concepts of tree data structures, highlighting their role in representing hierarchical organizations of information. Topics include the use of recursion in tree processing, various tree traversal methods, and differences among binary trees, binary search trees, and heaps. Learn how trees serve as recursive data structures with applications in class hierarchies, family trees, and directory structures. By the end, students will understand key terminology, the relationship between nodes, and the significance of tree height in computer science applications.
E N D
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 CS340
Trees - Introduction 2 • 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) CS340
Trees - Introduction (cont.) 3 • Trees can represent : • class hierarchy • disk directory and subdirectories • family tree • Trees are recursive data structures • Many methods to process trees are written recursively CS340
Binary trees 4 • 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 CS340
Tree Terminology 6 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia science arts mathematics CS340
Tree Terminology (cont.) 7 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia The node at the top of a tree is called its root science arts mathematics CS340
Tree Terminology (cont.) 8 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia The links from a node to its successors are called branches science arts mathematics CS340
Tree Terminology (cont.) 9 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia The successors of a node are called its children science arts mathematics CS340
Tree Terminology (cont.) 10 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia Each node in a tree has exactly one parent except for the root node, which has no parent science arts The predecessor of a node is called its parent mathematics CS340
Tree Terminology (cont.) 11 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia Nodes that have the same parent are siblings science arts mathematics CS340
Tree Terminology (cont.) 12 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia Leaf nodes also are known asexternal nodes, and nonleaf nodes are known as internal nodes A node that has no children is called a leaf node science arts mathematics CS340
Tree Terminology (cont.) 13 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia science arts mathematics A generalization of the parent-child relationship is the ancestor-descendant relationship CS340
Tree Terminology (cont.) 14 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia science arts mathematics A subtree of a node is a tree whose root is a child of that node CS340
Tree Terminology (cont.) 15 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia The level of a node is a measure of its distance from the root plus 1 Level 1 science arts Level 2 mathematics Level 3 CS340
Tree Terminology (cont.) 16 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia Level 1 science arts The level of a node is defined recursively Level 2 mathematics Level 3 • 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 CS340
Tree Terminology (cont.) 17 A tree consists of a collection of elements or nodes, with each node linked to its successors wikipedia The height of a tree is the number of nodes in the longest path from the root node to a leaf node science arts The height of this tree is 3 mathematics CS340
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) CS340
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) CS340
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
Binary Search Tree (cont.) • 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 CS340
Binary Search Tree (cont.) • Searching a BST can be O(log n) • What is the worst case? CS340
Recursive Algorithm for Searching a Binary Tree • if the tree is empty • return null (target is not found)else if the target matches the root node's data • return the data stored at the root nodeelse if the target is less than the root node's data • return the result of searching the left subtree of the rootelse • return the result of searching the right subtree of the root CS340
Full, Perfect, and Complete Binary Trees 7 • Full binary tree: all nodes have either 2 children or 0 children (the leaf nodes) 1 10 2 4 5 6 0 3 9 12 11 13 CS340
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 5 0 2 4 6 CS340
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 5 0 2 4 CS340
General Trees • Nodes of a general tree can have any number of subtrees CS340
General Trees (cont.) • A general tree can be represented using a binary tree CS340
Tree Traversals CS340
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 CS340
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 CS340
Visualizing Tree Traversals • If we keep following the tree to the left, it will trace a route known as the Euler tour CS340
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 CS340
Visualizing Tree Traversals (cont.) • If we record a node as we return from traversing its left subtree we get an inorder traversal • The sequence isd g b h e a i f j c CS340
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 CS340
Traversals of Binary Search Trees and Expression Trees • With inorder traversal nodes are visited in sequence Bob, Helen, Peter, Stacy Peter Helen Stacy Bob CS340
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) + / a b x y + c CS340
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 + / a b x y + c CS340
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 + / a b x y + c CS340
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 CS340
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> CS340
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. CS340
BinaryTree<E> Class (cont.) Assuming the tree is referenced by variable bT (type BinaryTree) then . . . CS340
BinaryTree<E> Class (cont.) bT.root.data references the Characterobject storing '*' CS340
BinaryTree<E> Class (cont.) bT.root.left references the left subtree of the root CS340
BinaryTree<E> Class (cont.) bT.root.right references the right subtree of the root CS340
BinaryTree<E> Class (cont.) bT.root.right.data references the Characterobject storing '/' CS340