Trees Eric McCreath
Overview In this lecture we will explore: general trees, binary trees, binary search trees, and AVL and B-Trees. 2
Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such as xml documents, mathematical expressions, natural language, programming languages), operating systems structures (file directory structure, disk allocation, processes), representing tables, search a space of possible states (used in planning, machine learning, or game playing), representing sets, and some algorithms. 3
Trees A tree is either : a leaf node with no children, or an inner node with one or more children, these children are also trees. An empty tree is a tree with no nodes. (as it is sometimes useful to be able to represent a tree that is completely empty) Nodes of a tree will normally contain some data. A B D E C F G 4
Trees - Terimnology An edge of a tree connects a parent node to a child node. These are directed in edges. Nodes will have exactly 1 parent (except the root node which has none). The root node of a tree is the node from which all other nodes descend from. Sibling nodes share the same parent. The size of a tree is the number of nodes that make up the tree (both leaf and inner nodes). The path from the root node to node X is an ordered list of nodes that follows the edges from the root node to X. The the depth of node X is the number of edges in the path from the root node to node X. The height of a tree is the maximum depth over all the nodes of the tree. 5
Trees - Terimnology A, B, C, D, E, F, G are the nodes of the tree. C, D, F and G are leaf nodes. A, B, and E are inner nodes. A is the root node of the tree. E is the parent of F and G. F and G and children of E. B, D and E are siblings. In this case B is the eldest and E is the youngest sibling. C and F are not siblings. The tree has size 7 and height 2. The path from A to C is A->B->C and C has depth 2. A B D E C F G 6
Traversal Algorithms on trees often involve traversing (visiting) the nodes of the tree. Examples include: operating on every data element in the nodes of the tree, transforming the tree into another data structure, search for a node, counting the nodes, and finding the height of a tree. Nodes may be traversed using different approaches or orders. Two common approaches are: Depth First, or Breadth First. 7
Depth-first Traversal (BFT) Depth-first traversal can be done using simple recursion. Basically this involves recursively calling depth-first traversal on each of the children. The below tree would be traversed with the following order: A, B, C, (B), (A), D, (A), E, F, (E), G, (E), (A) A B D E C F G 8
Breadth-first Traversal (DFT) Breadth-first traversal involves visiting all the nodes at depth 0, then all the nodes at depth 1, all the nodes at depth 2, etc. In the below example the order would be A, B, D, E, C, F, and G. A B D E C F G 9
Breadth-first Traversal (DFT) Implementing a BFT is a little more complex than DFT, and will generally use more memory. An implementation will normally involve maintaining a queue of nodes to visit. When a node is visited the node's children are added to the end of the queue. The next node to visit is taken from the front of the queue. The is continued until the queue is empty. A B D E C F G 10
Binary Tree A binary tree is a tree with each node having at most 2 children. We often call the children of an inner node the 'left' or 'right' child. A B E C F G 11
DFT of a Binary Tree DFT of a binary tree can be done recursively. A tree can be traversed by: visit the root, traverse the left subtree, traverse the right subtree, This is pre-order traversal. The order traversed is : A, B, C, E, F, G. A B E C F G 12
DFT of a Binary Tree There is also in-order traversal: traverse the left subtree, visit the root, traverse the right subtree. The order traversed in the below tree is : C, B, A, F, E, G. A B E C F G 13
DFT of a Binary Tree There is also post-order traversal. traverse the left subtree, traverse the right subtree, visit the root, The order traversed in the below tree is : C, B, F, G, E, A. A B E C F G 14
Binary Search Tree If the data stored in the nodes (or a key which forms part of the data) can be ordered then the nodes of the binary tree can be organized as a binary search tree. A Binary Search Tree can be used to store a set of elements. e.g. the set can be store via the below binary search tree: 5 3 13 6 14 15
Binary Search Tree A Binary Search Tree can be an efficient way of storing a table. So the table: could be stored using the binary search tree: Hugh,10 Eric,44 John,12 Isaac, 6 Matt,8 16
Binary Search Tree A Binary Search Tree is a binary tree where either: the root has no children, or the following three conditions hold: all the nodes in the left subtree are less than the root node, all the nodes in the right subtree are greater than the root node, both children are binary search trees. 5 5 13 3 13 3 14 4 6 14 17
Binary Search Tree - Search Elements can be searched for recursively: if the element we are searching for is at the root then we have found it, if the element we are searching for is less than the root then search the left subtree, else (the element must be greater than the root element) search the right subtree. If the tree is balanced than this is O(lg n) where n is the number of elements in the tree. 5 3 13 14 6 18
Binary Search Tree - Add Elements can be added to the binary search tree by recurring down the tree (basically the same as the search method) and then added as a leaf node at the appropriate position. This is also O(lg n) where n is the number of elements in the tree. (assuming the tree is balanced) 5 3 13 14 6 19
Binary Search Tree - Delete If the element to be removed is a leaf node then it can just be removed. 10 delete 13 10 18 4 18 4 13 21 1 6 6 21 1 8 5 5 8 20
Binary Search Tree - Delete If the element to be removed is an inner node with only one child than it can be deleted and the child subtree of the deleted element can be moved into the place of the deleted element's subtree. 10 10 Delete 18 18 4 21 4 1 6 6 21 1 8 5 5 8 21
Binary Search Tree - Delete If the element to be removed is an inner node and it has two children then the minimum element from the right sub-tree can be moved into the deleted elements place. (or the maximum of the left sub-tree) Delete is O(lg n) where n is the number of nodes in the tree. (assumes a balanced tree) 10 13 18 4 Delete 10 18 4 13 21 1 6 6 21 1 5 8 5 8 22
Binary Search Tree - Balanced The shape of the tree will depend on the order elements are added. If elements are added in ascending (or descending) order than the binary search tree will look (and perform) like a linked list. A tree with elements can be placed in a tree with height . If a binary search tree is within some constant range away from this optimum height then it would be considered balanced (note different algorithms will define "balanced" in different ways). There is a number of approaches to efficiently maintain a "balanced" tree as elements are added. (AVL Tree's, Red-Black Tree's) 23
AVL Trees AVL Trees are balanced binary search trees. They maintain the property that for each node the height of the node's children differ by at most one. This is achieve by checking that the tree remains balances after insertion. And if the tree become unbalanced then nodes are "rotated" to both maintain the binary search tree properties and restore the tree to a balanced state. This helps maintain good performance for searching the tree (even in the worst case), without degrading the performance of insertion or deletion operations. 24
B-Trees B-Tree are a generalization of binary search trees. However, they are not binary trees. So rather than having a single key in each node more than one key can be placed in each node. These keys are ordered within the nodes and partition the key values of the subtrees. So if there are 4 keys in a node then there would be 5 subtrees. The number of keys within an inner node will be maintained in the range to , so the number of subtrees a node will have will range between and . All the leaf node are kept at the same depth. This makes for good balance. B-trees (and their variants) are used extensively for storing tables on hard disks as, in comparison to a binary search tree, they reduce the depth to lookup an element. This in tern reduces the number of disk blocks needed to be read. 25
B-Trees The below tree is a b-tree whcih stores the set {1,3,8,9,12,15,21}. 8 15 1 3 9 12 21 Looking up elements within a b-tree can be implemented using using a recursive approach that is similar to that of a binary search tree. However, insertion and deletion are a little more tricky as one needs to maintain the constrains to help keep the tree balanced. 26
Recommend
More recommend