Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine."— Presentation transcript:

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

2 12.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 12.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 12.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 munu lambda nu mu Figure: Two distinct rooted trees

6 a b c d a b cdcd Figure. Two distinct binary trees 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 left child is missing

7 Trees A tree is an acyclic, connected and undirected graph. Equivalently, a tree may be defined as an undirected graph in which there exists exactly one path between any given pair of nodes. Since a tree is a kind of graph, the same representations used to implement graphs can be used to implement trees. Figure (a) Trees with 4 nodes 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 resulting graph is no longer connected.

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

9 alpha NodeHeightDepthLevel beta deltaepsilon gamma zeta alpha202 beta111 gamma011 delta020 epsilon020 zeta020 Figure: Height, depth and level Figure: A rooted tree Figure: Height, depth and level

10 12.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 12.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 12.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 12.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. 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) Binary Tree Traversal

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

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.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 12.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 12.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 12.19 Figure 12.27 Expression tree

20 12.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 12.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 12.22 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). An inorder traversal of a BST creates a list that is sorted in ascending order. i LEFT ROOT RIGHT

23 12.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 12.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 be represented 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 binary trees. In such a tree, each node can have 0, 1, or 2 children. In fact, we almost always assume that a node has two pointers, one to its left and one to its right, either of which can be nil..

27 Trees If each node of a rooted tree can have no more than k children, 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 Binary search (x,r) {The pointer r points to the root of a search tree. The function searches for the value x in this tree and returns a pointer to the node containing x. If x is missing, 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 worst case, where n is the number of nodes in the tree.


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

Similar presentations


Ads by Google