Tree Data Structures - intuitionke.weebly.com · If the Balance Factor is not more than 1 for all...

Preview:

Citation preview

Tree Data Structures

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

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.

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.

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.

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.

Typical Operations (ADT)

• insert()

• search()

• traverse()• in-order

• pre-order

• post-order

• isRoot()

• isEmpty()

• size()

• parent()

• children()

• siblings()

• root()

Different Types of Trees

• Tree

• Binary Tree

• Binary Search Tree

• AVL Tree

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

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

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

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

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

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

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

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

BST Implementation

struct node

{

int n;

struct node* left;

struct node* right;

}node;

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.

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

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

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

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.

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)

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).

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())

Balanced vs Unbalanced Trees

How many comparisons are needed to find the value 80?

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.

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.

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).

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

Recommended