![Page 1: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/1.jpg)
Tree Data Structures
![Page 2: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/2.jpg)
REQUIRED READING
Before reading these slides, go through the following pages:
1. https://www.tutorialspoint.com/data_structures_algorithms/tree_data_structure.htm
2. https://www.tutorialspoint.com/data_structures_algorithms/tree_traversal.htm
3. https://www.tutorialspoint.com/data_structures_algorithms/binary_search_tree.htm
4. https://www.tutorialspoint.com/data_structures_algorithms/avl_tree_algorithm.htm
![Page 3: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/3.jpg)
Trees• Productivity experts say that breakthroughs come by thinking
“nonlinearly.”
• Trees: non-linear data structures.
• They provide natural organization for data.
• E.g. file systems, graphical user interfaces, databases, Web sites, and other computer systems.
![Page 4: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/4.jpg)
Trees
• A tree is an abstract data type that stores elements hierarchically.
• With the exception of the top element, each element in a tree has aparent element and zero or more children elements.
![Page 5: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/5.jpg)
Tree Definition
• Formally, a tree T is a set of nodes storing elements in a parent-childrelationship with the following properties:• If T is nonempty, it has a special node, called the root of T, that has no parent.
• Each node v of T different from the root has a unique parent node w; everynode with parent w is a child of w.
![Page 6: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/6.jpg)
Tree Elements• Node: item in the tree
• Branch: link connecting Nodes
• Root: "first" Node, no parent
• Leaf: "Bottom" Nodes, no children
• Parent: Node above children
• Child: Node below parent
• Siblings: Children of the same parent
• Levels: Number of rows
• Null tree: 0 levels (nothing in tree)
• Sub-tree: part of the tree that is also a tree
• A tree is either a null tree or a root with several sub-trees.
![Page 7: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/7.jpg)
Typical Operations (ADT)
• insert()
• search()
• traverse()• in-order
• pre-order
• post-order
• isRoot()
• isEmpty()
• size()
• parent()
• children()
• siblings()
• root()
![Page 8: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/8.jpg)
Different Types of Trees
• Tree
• Binary Tree
• Binary Search Tree
• AVL Tree
![Page 9: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/9.jpg)
Tree Traversal Algorithms
• There are 3 standard tree traversals: pre-order, in-order, post-order
• Key:
• V visit (or look at the Node)
• L traverse the Left sub-tree
• R traverse the Right sub-tree
![Page 10: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/10.jpg)
Tree Traversal Algorithms
• Pre-Order (V L-R):
• Look at the data at the root
• Pre-Order traverse all the subtrees from left to right.
•
• In-Order (L V R):
• In-order traverse the leftmost subtree
• Look at the data at the root of the subtree
• In-Order traverse all other subtrees from left to right.
•
• Post-Order (L-R V):
• Post-Order traverse all subtrees from left to right.
• Look at the root
![Page 11: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/11.jpg)
Pre-Order Traversal: V L-R
Until all nodes are traversed:
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
A → B → D → E → C → F → G
![Page 12: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/12.jpg)
In-Order Traversal: L V R
Until all nodes are traversed:Step 1 − Recursively traverse left subtree.Step 2 − Visit root node.Step 3 − Recursively traverse right subtree.
D → B → E → A → F → C → G
![Page 13: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/13.jpg)
Post-Order TraversalUntil all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
D → E → B → F → G → C → A
![Page 14: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/14.jpg)
Pre-order traversal (VLR)
1 3 5 1
2 7
3 1 1
5
1 5 2 7
2 0
3 1 3 7
3 5
5 6 7 4
6 5
P r e -o r d e r t r a v e r sa l
The resulting sequence of numbers is as given in the diagram and the number sequence below
27 13 5 3 11 20 15 27 51 35 31 37 65 56 74
![Page 15: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/15.jpg)
In-order traversal (LVR)
1 3 5 1
2 7
3 1 1
5
1 5 2 7
2 0
3 1 3 7
3 5
5 6 7 4
6 5
I n -o r d e r t r a v e r sa l
The resulting sequence of numbers is as given in the diagram and the number sequence below
3 5 11 13 15 20 27 27 31 35 37 51 56 63 74
![Page 16: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/16.jpg)
Post-order traversal (LRV)
1 3 5 1
2 7
3 1 1
5
1 5 2 7
2 0
3 1 3 7
3 5
5 6 7 4
6 5
P o st-o r d e r tr a v e r sa l
The resulting sequence of numbers is as given in the diagram and the number sequence below
3 11 5 15 27 20 13 31 37 35 56 74 65 51 27
![Page 17: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/17.jpg)
BST Implementation
struct node
{
int n;
struct node* left;
struct node* right;
}node;
![Page 18: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/18.jpg)
Inserting into the BST
How do we insert a new value e.g. 25 into the tree?
• Root = 20, 25 is > than 20 so move right.
• Root is now = 30, 25 is < than 30 so move left.
• Root is now = 22, 25 is > than 22 so move right.
• Node does not exist to the right, so insert new Node 25 here.
![Page 19: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/19.jpg)
Insert into the BST (this is just pseudocode)
insert(int data, node * tree)node *myTree = tree;
If myTreeis NULL
create root node
return
do until insertion position is located
If data > myTree.data
myTree = myTree->right i.e go to right subtree
if right subtree root is NULL
insert data into this root and return
else
myTree = myTree->left i.e. go to left subtree
if left subtree root is NULL
insert data into this root and return
end do
![Page 20: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/20.jpg)
Searching in a BSTSearching a tree for a particular value e.g. 2:
• Root = 20, 2 is < 20 so move left
• Root = 18, 2 is < 18 so move left
• Root = 2 -> found.
For a value that does not exist e.g. 56:
• Root = 20, 56 is > 20 so move right
• Root = 30, 56 is > 30 so move right
• Root = 80, 56 is < 80 so move left
• Root does not exist -> not found
![Page 21: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/21.jpg)
Searching through the BST
search(int key, node* tree)
if(tree == NULL)
return false
else if (key < tree->n)
return search(key, tree->left)
else if (key > tree->n)
return search(key, tree->right)
else // key == tree->n
return true
![Page 22: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/22.jpg)
BST Search explanation
• We are looking for n, given tree, which is a node* to the root ofthe tree. We implement the logic fairly straightforwardly.
• If tree is NULL, then there are no nodes so we return false.
• The middle two conditions compare n, the value we are given, totree->n, which dereferences tree and accesses the n field storedwithin. If the n we are looking for is less, we search again with the leftchild of the tree by calling search(n, tree->left), whichmakes our problem smaller. Likewise, if n is greater, we search withthe right child of the tree.
• Finally, if the tree is not NULL and n is neither greater than or lessthan tree->n, then we found the value and can return true.
![Page 23: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/23.jpg)
Performance of the BST
Note that the running time of search and update operations in a BSTvaries dramatically depending on the tree’s height.
On average, a binary search tree with n keys generated from a randomseries of insertions and removal of keys has expected height O(logn)
![Page 24: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/24.jpg)
Height and Depth• Let p be a node of a tree T. The depth of p is the number of ancestors of
p, excluding p itself.
• Note that this definition implies that the depth of the root of T is 0.
• The depth of p’s node can also be recursively defined as follows:
• If p is the root, then the depth of p is 0
• Otherwise, the depth of p is one plus the depth of the parent of p
• The height of a tree T is the height of the root of T.
• The height of a tree is equal to the maximum depth of its external nodes (a.k.a leaf nodes).
![Page 25: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/25.jpg)
Depth
Algorithm to find depth of a node, p, of a tree, T
depth(T, p):
if p.isRoot() then
return 0
else
return 1+depth(T, p.parent())
![Page 26: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/26.jpg)
Balanced vs Unbalanced Trees
How many comparisons are needed to find the value 80?
![Page 27: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/27.jpg)
Balanced vs Unbalanced Trees
• Balance Factor = height(left-subtree) − height(right-subtree)
If the Balance Factor is not more than 1 for all nodes, then the tree isbalanced
• A binary search tree is perfectly balanced if for every Node in thetree, the number of nodes to the left and to the right differ by one (orzero).
• If a tree is perfectly balanced, then the number of comparisonsneeded to find any particular value is minimised.
![Page 28: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/28.jpg)
AVL Trees
• Ensuring that trees are balanced.
• height-balance property: For every internal node v of T, the heightsof the children of v differ by at most 1.
• Helps maintain a logarithmic height for the tree.
• Any binary search tree T that satisfies the height-balance property issaid to be an AVL tree, named after the initials of its inventors,Adelson, Velskii and Landis.
![Page 29: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/29.jpg)
AVL Tree
The keys of the entries areshown inside the nodes,and the heights of thenodes are shown next tothe nodes.
The height of an AVL treestoring n entries is O(logn).
![Page 30: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/30.jpg)
AVL Rotations
• What are AVL rotations and what are they useful for?
• Can you illustrate the following kinds of rotations?• Left Rotation
• Right Rotation
• Left-Right Rotation
• Right-Left Rotation
![Page 31: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/31.jpg)
![Page 32: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/32.jpg)
![Page 33: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/33.jpg)
![Page 34: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/34.jpg)
![Page 35: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/35.jpg)
![Page 36: Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all nodes, then the tree is balanced •A binary search tree is perfectly balanced if](https://reader033.vdocuments.mx/reader033/viewer/2022041417/5e1c0e75efd1cd103714387b/html5/thumbnails/36.jpg)