Lecture 5 Trees.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
Lecture 5 Trees. Family Trees Please draw from the board Tree: Section 4.1 (Weiss)
Lecture 5 Trees. Tree: Section 4.1 (Weiss) Formal Definition Tree is a sequence of nodes. There is a starting node known as root node. Every node other.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
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.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Tree Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Binary Search Trees Chapter 7 Objectives
CSE 373 Data Structures Lecture 7
CC 215 Data Structures Trees
BCA-II Data Structure Using C
Search Trees.
Binary Search Trees A binary search tree is a binary tree
BST Trees
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
UNIT III TREES.
CSIT 402 Data Structures II
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
CS 201 Data Structures and Algorithms
AVL Tree.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
AVL Tree 27th Mar 2007.
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
Trees CSE 373 Data Structures.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Lecture 5 Trees

Family Trees Please draw from the board Tree: Section 4.1 (Weiss)

Formal Definition Tree is a sequence of nodes. There is a starting node known as root node. Every node other than the root has a parent node. Nodes may have any number of children. A B C D E A has 3 children, B, C, D A is parent of B

Path to a node p is a sequence of nodes root,n1, n2, … Path to a node p is a sequence of nodes root,n1, n2, …..p such that n1 is a child of root, n2 is a child of n1 …… Path to E is ? How many paths are there to one node? Depth of a node is the length of the unique path from the root to the node (not counting the node). Root is at depth 0 Depth of E is ?

Leaves are nodes without any children. D and E are a leaf node. Height of a nonleaf node is the length of the LONGEST path from the node to a leaf(not counting the leaf). Height of a leaf is 0 Height of A is ?

Application Organization of a file system. Note down the example from the board Every node has one or more elements: Directory example: element of a node is the name of the corresponding directory

Implementation Using pointers A node has a pointer to all its children Since a node may have many children, the child pointers have a linked list. A has a pointer to B, C, D each. B has pointers to E and its other child. E does not have any pointers.

Addition of a child: Create the new child node Add a pointer to this child in the link list of its parent. Deletion of a child B: Children of B are first made children of the parent of B Node B is deleted. Deletion of the root: One of the children becomes the new root: Other children of old root become the children of the new root

Tree Traversal Preorder Postorder Preorder: First do the required operation with a node, then with its children Postorder: First do the required operation with the children, then return to the node.

Preorder Example Need to find the depth of each node in the tree representing the unix file system. Listdepth(node,depth) { print(node->name, depth) for each child C of node Listdepth(C,depth+1) }

Postorder Example Need to find the size of each directory in the unix file system Dirsize(node) { size = 0 for each child C of node size size + Dirsize( C ); print(node->name, size); }

Binary Trees A node can have at most 2 children, leftchild and rightchild A complete binary tree is one where a node can have 0 or 2 children and all leaves are at the same depth. A complete binary tree of N nodes has depth O(log N)

Proof Prove by induction that number of nodes at depth d is 2d Total number of nodes of a complete binary tree of depth d is 1 + 2 + 4 +…… 2d = 2d+1 - 1 Thus 2d+1 - 1 = N d = log(N + 1) - 1 What is the largest depth of a binary tree of N nodes?

Inorder Travel First operate with the left subtree Then operate with the node Then operate with the right subtree Printtree(node) { printtree(node->leftchild); print(node->element); printtree(node->rightchild); }

Search Tree A binary search tree is useful for searching, Section 4.3 We assume that al elements are distinct All elements in the left subtree of a node are smaller than the element of the node, and all elements in the right subtree of a node are larger. Example: Note down from the board

Searching an element in the Tree Start from the root. Each time we encounter a node, see if the key in the node equals the element. If yes stop. If the element is less, go to the left subtree. If it is more, go to the right subtree. Conclude that the element is not in the list if we reach a leaf node and the key in the node does not equal the element.

Search(node, element) { If (node = NULL) conclude NOT FOUND; Else If (node.key = element) conclude FOUND; Else If (element < node.key) Search(node.leftchild, element); Else If (element > node.key) Search(node.rightchild, element); } Complexity: O(d), d is the depth

Find Min Node = root; For (node = root; node=node.leftchild; node!=NULL) prevnode = node; Return(prevnode); Complexity: O(d)

Insert an element Try to find the element; If the element exists, do nothing. If it does not, insert it at the position of the returned null pointer;

Insert(node, element) { If (node.key = element) conclude FOUND and RETURN; Else If (element < node.key) if (node.leftchild =NULL) insert the new node at node.leftchild; else Insert(node.leftchild, element); } Else If (element > node.key) ?????????? }

Complexity: O(d) DELETION When we delete a node, we need to consider how we take care of the children of the deleted nodes. This has to be done such that the property of the search tree is maintained. Any problem if the node has no child? Any problem if the node has only one child?

Suppose, the node has two children. Look at the right subtree of the node (subtree rooted at the right child of he node). Find the Minimum there. Replace the key of the node to be deleted by the minimum element. Delete the minimum element. Any problem deleting it? For deletion convenience, always have a pointer from a node to its parent.

Pseudo Code If a node is childless, then { node->parent->ptr_to_node = NULL free node; } If a node has one child { node->parent->child = node->child; free node; }

If a node has 2 children, { minnode = findmin(rightsubtree)->key; node->key = minnode->key; delete(minnode); } Complexity?

Lazy Deletion Don’t physically delete the node, but mark it as ``junk.’’ Any problem? Increases the depth of the tree. However, if the number of deleted nodes is of the same order as the number of existing nodes, then lazy deletion does not alter the order of the depth significantly.

AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with large height This can be attained if both subtrees of each node have roughly the same height. AVL tree is a binary search tree where the height of the two subtrees of a node differs by at most one Height of a null tree is -1

Section 4.4, Weiss Suppose an AVL tree of height h contains contains at most S(h) nodes: S(h) = L(h) + R(h) + 1 L(h) is the number of nodes in left subtree R(h) is the number of nodes in right subtree You have larger number of nodes if there is larger imbalance between the subtrees This happens if one subtree has height h-1, another h-2 Thus, S(h) = S(h-1) + S(h-2) + 1 Using this you can show that h = O(log N)

Operations in AVL Tree Searching, Complexity? FindMin, Complexity? Deletion? Lazy Deletion, Complexity?

Insertion Search for the element If it is not there, insert it in its place. Any problem? Insertion may imbalance the tree. Heights of two children of a node may differ by 2 after an insertion. Tree Rotations used to restore the balance.

If an insertion cause an imbalance, which nodes can be affected? Nodes on the path of the inserted node. Let U be the node nearest to the inserted one which has an imbalance. insertion in the left subtree of the left child of U insertion in the right subtree of the left child of U insertion in the left subtree of the right child of U insertion in the right subtree of the right child of U

Insertion in left child of left subtree Single Rotation V U U X V Y Z Z Y X After Rotation Before Rotation

Example Note down from board

Double Rotation Suppose, imbalance is due to an insertion in the left subtree of right child Single Rotation does not work! W U Before Rotation After Rotation V D V U W A A B C D C B

Example Note down from board

Pseudocode Insert(X, T) { If (T = NULL) insert X at T; T->height = 0; If (XT.element) Insert(X, T ->left) If Height(T ->left) - Height(T ->right) = 2

Pseudocode Singlerotate routine in Fig 4.41 (Weiss) Separate for left and right nodes Doublerotate routine in Fig 4.43 (Weiss)

{ If (X < T.leftchild.element) T =singleroratewithleft(T); else T =doubleroratewithleft(T); } } Else If (X>T.element) Insert(X, T ->right) If Height(T ->right) - Height(T ->leftt) = 2 If (X > T.righchild.element) T =singleroratewithright(T); else T =doubleroratewithright(T); } }

T->height = max(height(T->left), height(T->right)) + 1; Return(T); EXTENDED EXAMPLE Note down from board Deletions can be done with similar rotations