1 / 29

Advanced Algorithms Analysis and Design

Advanced Algorithms Analysis and Design. Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine. 12-5 TREES.

trudy
Download Presentation

Advanced Algorithms Analysis and Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine

  2. 12-5 TREES A tree consists of a finite set of elements, called nodes (or vertices) and a finite set of directed lines, called arcs, that connect pairs of the nodes. Figure 12.20 Tree representation

  3. As We can divided the vertices in a tree into three categories: the root, leaves and the internal nodes. Table 12.1 shows the number of outgoing and incoming arcs allowed for each type of node.

  4. Each node in a tree may have a subtree. The subtree of each node includes one of its children and all descendents of that child. Figure 12.21 shows all subtrees for the tree in Figure 12.20. Figure 12.21 Subtrees

  5. lambda lambda mu nu mu nu Figure: Two distinct rooted trees

  6. The two binary trees as shown in figure are not the same: in the first case b is the left child of a and the right child is missing, whereas in the second case b is the right child of a and the leftchild is missing a a b b c c d d Figure. Two distinct binary trees

  7. Trees • A tree is an acyclic, connected and undirectedgraph. Equivalently, a tree may be defined as anundirected graph in which there exists exactlyone path between any given pair of nodes. • Since a tree is a kind of graph, the samerepresentations used to implement graphs canbe used to implement trees. • Trees have a number of simple properties, of which the following are perhaps the most important:- A tree with n nodes has exactly n - 1 edges. If a single edge is removed from a tree, then the resultinggraph is no longer connected. Figure (a) Trees with 4 nodes

  8. Height, Depth and Level • Height and level, for instance, are similarconcepts, but not the same. - The height of a node is the number of edges in thelongest path from the node in question to a leaf. - The depth of a node is the number of edges in thepath from the root to the node in question. - The level of a node is equal to the height of the rootof the tree minus the depth of the node concerned.

  9. alpha Node Height Depth Level alpha 2 0 2 beta 1 1 1 gamma gamma 0 1 1 beta delta 0 2 0 epsilon 0 2 0 zeta 0 2 0 delta epsilon zeta Figure: Height, depth and level Figure: A rooted tree Figure: Height, depth and level

  10. 12-6 BINARY TREES A binary tree is a tree in which no node can have more than two subtrees. In other words, a node can have zero, one or two subtrees. Figure 12.22 A binary tree

  11. Recursive definition of binary trees We can also define a structure or an ADT recursively. The following gives the recursive definition of a binary tree. Note that, based on this definition, a binary tree can have a root, but each subtree can also have a root.

  12. Figure 12.23 shows eight trees, the first of which is an empty binary tree (sometimes called a null binary tree). Figure 12.23 Examples of binary trees

  13. Operations on binary trees The six most common operations defined for a binary tree are tree (creates an empty tree), insert, delete, retrieve, empty and traversal. We discuss binary tree traversal in this section. Binary Tree Traversal A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. The two general approaches to the traversal sequence are 1) Depth-first (deeper in the tree before exploring siblings) 2) Breadth-first traversal (Trees can also be traversed in level-order)

  14. Depth First Transversal ROOT LEFT RIGHT LEFT ROOT RIGHT LEFT RIGHT ROOT Figure Depth-first traversal of a binary tree

  15. Breadth-first Traversal Trees can also be traversed in level-order, where we visit every node on a level before going to a lower level. This is also called Breadth-first traversal. Example Preorder

  16. Example Figure shows how we visit each node in a tree using preorder traversal i.e A, B, C, D, E, F.

  17. Binary tree applications Binary trees have many applications in computer science. In this section we mention only two of them: Huffman coding and expression trees. Huffman coding Huffman coding is a compression technique that uses binary trees to generate a variable length binary code from a string of symbols. We discuss Huffman coding in detail in next lectures.

  18. Expression trees An arithmetic expression can be represented in three different formats: infix, postfix and prefix. In an infix notation, the operator comes between the two operands. In postfix notation, the operator comes after its two operands, and in prefix notation it comes before the two operands. These formats are shown below for addition of two operands A and B.

  19. Figure 12.27 Expression tree

  20. 12-7 BINARY SEARCH TREES A binary search tree (BST) is a binary tree with one extra property: the key value of each node is greater than the key values of all nodes in each left subtree and smaller than the value of all nodes in each right subtree. Figure 12.28 shows the idea. Figure 12.28 Binary search tree (BST)

  21. Example 12.12 Figure 12.29 shows some binary trees that are BSTs and some that are not. Note that a tree is a BST if all its subtrees are BSTs and the whole tree is also a BST. Figure 12.29 Example 12.12

  22. i A very interesting property of a BST is that if we apply the inorder traversal of a binary tree, the elements that are visited are sorted in ascending order. For example, the three BSTs in Figure 12.29, when traversed in order, give the lists (3, 6, 17), (17, 19) and (3, 6, 14, 17, 19). LEFT ROOT RIGHT An inorder traversal of a BST creates a list that is sorted in ascending order.

  23. Binary search tree ADTs The ADT for a binary search tree is similar to the one we defined for a general linear list with the same operation. As a matter of fact, we see more BST lists than general linear lists today. The reason is that searching a BST is more efficient than searching a linear list: a general linear list uses sequential searching, but BSTs use a version of binary search.

  24. BST implementation BSTs can be implemented using either arrays or linked lists. However, linked list structures are more common and more efficient. The implementation uses nodes with two pointers, left and right. Figure 12.31 A BST implementation

  25. Trees On a computer any rooted tree may berepresented using nodes of the following type. type treenode1 = record value: information eldest - child, next - sibling: ↑ treenode1

  26. Trees • We shall often have occasion to use binarytrees. In such a tree, each node can have 0, 1, or 2 children. • In fact, we almost always assume that a nodehas two pointers, one to its left and one to itsright, either of which can be nil. .

  27. Trees • If each node of a rooted tree can have no more than kchildren, we say it is a k-ary tree. • One obvious representation uses nodes of the type k-ary-node = record value: information child: array [i. . k] of ↑k-ary-node • In the case of a binary tree we can also define type binary-node = record value: information left-child, right-child : ↑binary-node

  28. function Binarysearch(x,r) {The pointer r points to the root of a search tree. Thefunction searches for the value x in this tree andreturns a pointer to the node containing x. If x ismissing, the function returns nil.} if r = nil then {x is not in the tree.} return nil else if x = r↑. Value then return r else if x < r ↑. Value then return search (x,r ↑.left-child)else return search(x,r ↑.right-child)

  29. Trees • For a tree operations as searches or the addition and deletion of nodes take a O(log n) in the worstcase, where n is the number of nodes in the tree.

More Related