Lec4 Trees 2.pptx
Document Details
Uploaded by HandsDownParticle
Umm Al-Qura University
Tags
Full Transcript
Mamma l Do Bea Ca g r t Data Structures Trees What is a Tree In computer science, a tree is an abstract Computers”R”U model of a hierarchical s structu...
Mamma l Do Bea Ca g r t Data Structures Trees What is a Tree In computer science, a tree is an abstract Computers”R”U model of a hierarchical s structure A tree consists of nodes Sale Manufacturin R& with a parent-child s g D relation U Internationa Laptop Desktop Applications: S l s s Organization charts File systems Europ Asi Canad In general, storing e a a and manipulating hierarchical data. © 2014 Goodrich, Tamassia, Goldwasser Trees 2 Tree Terminology Root: node without parent (A) Subtree: tree consisting of a node and its Internal node: node with at least descendants one child (A, B, C, F) External node (a.k.a. leaf ): node A without children (I, J, K, G, H, D) Siblings: nodes that are children of the same parent (example G & H) B C D Ancestors of a node: parent, grandparent, grand-grandparent, etc. (node F’s ancestors are B and F G H A) Descendant of a node: child, I J K grandchild, grand-grandchild, etc. subtree © 2014 Goodrich, Tamassia, Goldwasser Trees 3 Tree Terminology An edge of tree T is a pair of nodes (u,v) such that u is the parent of v, or vice versa. Example: (A,B), (F,I). A path of T is a sequence of nodes A such that any two consecutive nodes in the sequence form an edge. Example: (B, F, K) B C D There is exactly one path (one sequence of edges) connecting each node to the root. F G H I J K subtree © 2014 Goodrich, Tamassia, Goldwasser Trees 4 Tree Terminology The depth of a node v is the number of ancestors of v, other than v itself. Example: the node storing “F” has depth 2. This definition implies that the depth of the root of T is 0. A Equivalently, the depth of a node v is the number of edges on the path from B C D the node v to the root. The depth of v can be recursively F G H defined as follows: If v is the root, then the depth of v is 0. I J K Otherwise, the depth of v is one subtree plus the depth of the parent of v. © 2014 Goodrich, Tamassia, Goldwasser Trees 5 Tree Terminology Nodes with the same depth form a level of the tree. The height of a tree is the maximum depth of its nodes, or zero if the tree is empty. Example: the tree shown in this slide has a height of 4. Root Depth= 0, Level 0 A parent node Leaf/external node Depth= 1, Level 1 B C D child node Depth= 2, Level 2 F G H subtree Depth= 3, Level 3 I J K siblings Depth= 4, Level 4 w 6 Ordered Tree A tree is ordered if there is a meaningful linear order among the children of each node; that is, we purposefully identify the children of a node as being the first, second, third, and so on. Such an order is usually visualized by arranging siblings left to right, according to their order. Ordered tree © 2014 Goodrich, Tamassia, Goldwasser Trees 7 Ordered Tree unordered tree © 2014 Goodrich, Tamassia, Goldwasser Trees 8 Binary Trees A binary tree is a tree with the following A properties: Each internal node has at most two children i.e 0 or 1 or 2 (exactly two for proper binary trees) B C Each child node is labeled as being either a left child or a right child. A left child precedes a right child in the order of children of a node D E F G The subtree rooted at a left or right child of an internal node v is called a H I left subtree or right subtree. A binary tree is proper if each node has either zero or two children. Also called full binary trees. A binary tree that is not proper is improper. © 2014 Goodrich, Tamassia, Goldwasser Trees 9 A Recursive Binary Tree Definition We can also define a binary tree in a recursive way. In that case, a binary tree is either: An empty tree. A nonempty tree having a root node r, which stores an element, and two binary trees that are respectively the left and right subtrees of r. We note that one or both of those subtrees can be empty by this definition. © 2014 Goodrich, Tamassia, Goldwasser Trees 10 Applications of Binary Tree Arithmetic expressions Decision processes Searching © 2014 Goodrich, Tamassia, Goldwasser Recursion 11 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 (a - 1) + +(3 b)) 2 - 3 b a 1 © 2014 Goodrich, Tamassia, Goldwasser Trees 12 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? Yes No How about On expense coffee? account? Yes No Yes No Starbucks Chipotle Gracie’s Café Paragon © 2014 Goodrich, Tamassia, Goldwasser Trees 13 Binary Search Trees Search-tree properties: for each node k: all nodes in k’s left subtree are < k all nodes in k’s right subtree are > k The left and right subtree each must also be a binary search tree. There must be no duplicate nodes. 26 12 >26 4 18 38 v.element right(v) insertRecursively(rigth(v), value) return v 38 Implementing a Binary Search Tree: getsize (number of nodes in the tree) Algorithm int getSizeRecursively(node v) if v = null Return 0 else Return getSizeRecursively(left(v)) + 1 + getSizeRecursively(right(v)) 39 Advantages and Disadvantages of BST Advantages of BST Quick insertion, deletion and search Disadvantages BST work well if the data is inserted into the tree in random order, and become much slower if data is inserted already in sorted order (ascending or descending) Eventually tree become unbalanced when any item is inserted BST Java implementation Node.java Public class Node { int key; Node left, right; public Node(int item) { key = item; left = right = null; } } BST implementation BinarySearchTree.java class BinarySearchTree { Node root; // Constructor BinarySearchTree() { root = null; }... BST implementation BinarySearchTree.java.. // A utility function to insert // a new node with given key in BST Node insert(Node node, int key) { // If the tree is empty, return a new node if (node == null) { node = new Node(key); return node; } // Otherwise, recur down the tree if (key < node.key) node.left = insert(node.left, key); else if (key > node.key) node.right = insert(node.right, key); // Return the (unchanged) node pointer return node; }.. BST implementation. BinarySearchTree.java. // Utility function to search a key in a BST Node search(Node root, int key) { // Base Cases: root is null or key is present at root if (root == null || root.key == key) return root; // Key is greater than root's key if (root.key < key) return search(root.right, key); // Key is smaller than root's key return search(root.left, key); }... // Driver Code public static void main(String[] args) { BinarySearchTree tree = new BinarySearchTree(); // Inserting nodes tree.root = tree.insert(tree.root, 50); tree.insert(tree.root, 30); tree.insert(tree.root, 20); tree.insert(tree.root, 40); tree.insert(tree.root, 70); tree.insert(tree.root, 60); tree.insert(tree.root, 80); // Key to be found int key = 6; BinarySearchTree.java // Searching in a BST if (tree.search(tree.root, key) == null) System.out.println(key + " not found"); else System.out.println(key + " found"); key = 60; // Searching in a BST if (tree.search(tree.root, key) == null) System.out.println(key + " not found"); else System.out.println(key + " found"); } }