meljun cortes data structures types of binary search tree
TRANSCRIPT
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
1/24
Data Structures
Balanced / unbalanced tree
Balanced tree - equal items on each subtree ofnode to minimize the maximum path from the
root to any leaf node
Height-balanced - left and right subtree
height of each node is within 1
Types of Binary Search Tree *Property of STI Page 1 of 24
Unbalanced tree - root has more left
descendants than its right descendants or vice-versa
Self-balancing tree - attempt to keep its height,the number of levels of nodes underneath theroot, as small as possible
Tree rotation - changes the structure of the treewithout intervening any element in the tree
especially its order
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
2/24
Data Structures
AVL Tree
named after G.M. Adelso-Velsky and E.M.Landis
can also be called as height balanced
Balance factor - done by right subtree less the
height of its left subtree Insertion - as it is inserting in an unbalanced
BST and retraces the one’s steps toward theroot
–
Types of Binary Search Tree *Property of STI Page 2 of 24
Search - no provisions will be taken and will notchange the structure of the tree
an AVL tree because itshows that each leftsubtree has a height of 1
greater than each rightsubtree
not an AVL tree for thesubtree of E has theheight of 4 and thesubtree of I has theheight of 2
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
3/24
Data Structures
Red-Black Tree
a special type of BST that organizes pieces ofcomparable data such as numbers
two characteristics that this tree comprises
The nodes are colored
During insertion and deletion, rules arefollowed that preserve variousarrangements of these colors
Red-Black Rules
Types of Binary Search Tree *Property of STI Page 3 of 24
The root is always black.
If a node is red, its children must beblack.
Both children of every red node areblack.
Every path from the root to a leaf, or toa null child, must contain the samenumber of black nodes.
things should be done when tree is notbalanced or the color rules are violated
Change the colors of the nodes.
Perform rotations.
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
4/24
Data Structures
Red-Black Tree
Rotations are designed to:
Raise some nodes and lower others to helpbalance the tree
Ensure that the characteristics of a binary
search tree are not violated To maintain the properties of red-black trees
when inserting a node
Enter a new element on a tree as a red leaf
Types of Binary Search Tree *Property of STI Page 4 of 24
Maintain the root as a black node
When moving down the path to look onwhere to insert, update the colors of thenodes when you find a black parent with twored children
Inserting a node at the bottom may result intwo successive red nodes; employ rotationto reorder the coloring of the tree
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
5/24
Data Structures
Splay Trees
elements in the tree that have been accessedrecently can be accessed very quickly
invented by Daniel Sleator and Robert Tarjan
it performs an operation called “splaying” also
know as rotation
Advantages
Faster performance
Types of Binary Search Tree *Property of STI Page 5 of 24
Easier implementation
Efficient with identical keys
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
6/24
Data Structures
Splay Trees
Three splay steps:
Zig step
Zig-zig step
Types of Binary Search Tree *Property of STI Page 6 of 24
Zig-zag step
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
7/24
Data Structures
2-3-4 Trees
Multiway tree - allowing having more data itemsand children per node
Types of Binary Search Tree *Property of STI Page 7 of 24
2-3-4 tree – a multiway tree that can have up to4 children and 3 data items per node
Possible arrangement for non-leaf nodes A node with one data item always has two
children
A node with two data items always has
three children
A node with three data items always hasfour children
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
8/24
Data Structures
2-3-4 Trees
Types of Binary Search Tree *Property of STI Page 8 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
9/24
Data Structures
2-3-4 Trees
Principle
All children in the subtree rooted at child 0have key values less than key 0
All children in the subtree rooted at child 1
have key values greater than 0 but less thankey 2
All children in the subtree rooted at child 2have key values greater than key 1 but lessthan ke 2
Types of Binary Search Tree *Property of STI Page 9 of 24
All children in the subtree rooted at child 3have key values greater than key 2
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
10/24
Data Structures
Find key value 64
2-3-4 Trees
Types of Binary Search Tree *Property of STI Page 10 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
11/24
Data Structures
Insert operations
2-3-4 Trees
Types of Binary Search Tree *Property of STI Page 11 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
12/24
Data Structures
Splitting a node
A new, empty node is created. It’s a siblingof the node being split, and is placed to its
right.
Data item C is moved into a new node. Data item B is moved into the parent of the
node being split.
Data item A remains where it is.
2-3-4 Trees
Types of Binary Search Tree *Property of STI Page 12 of 24
from the node being split and connected tothe new node
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
13/24
Data Structures
2-3-4 Trees
Types of Binary Search Tree *Property of STI Page 13 of 24
Split a root A new node is created that becomes the new
root and the parent of the node being split.
A second new node is created that becomes asibling of the node being split.
Data item C is moved into the new sibling.
Data item B is moved into the new root.
Data item A remains where it is.
The two rightmost children of the node beingsplit are disconnected from it and connected tothe new right—hand node.
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
14/24
Data Structures
2-3-4 trees
Types of Binary Search Tree *Property of STI Page 14 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
15/24
Data Structures
“B” – balanced, Boieng, Bayer
keeps data in a sorted order
Database and filesystems – applications thatuses this data structure
require that all leaf nodes are at the same depth Search, insert, delete are operations that can be
performed
overflowed - adding on a full leaf node
B-trees
Types of Binary Search Tree *Property of STI Page 15 of 24
To make nodes in order
Half the leaf node making it the left child
Pick the middle and place it on the root Create a new node placing the remaining
values
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
16/24
Data Structures
B-trees
Insert 18
simply move the values of the middle leaf nodeto insert 18
Types of Binary Search Tree *Property of STI Page 16 of 24
Insert 46
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
17/24
Data Structures
From the tree below, insert the following values:
67
54
39
Exercise
Types of Binary Search Tree *Property of STI Page 17 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
18/24
Data Structures
Inserting 67
Inserting 54
Answers
Types of Binary Search Tree *Property of STI Page 18 of 24
Inserting 39
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
19/24
Data Structures
a partial sorted binary tree
Heap
Complete
Not complete
Types of Binary Search Tree *Property of STI Page 19 of 24
Implemented as array
satisfies the heap condition
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
20/24
Data Structures
Removal of node in heap means to remove themaximum key which this node is always the root
Remove the root
Move the last node into the root
Trickle (bubble) the last node down until it’sbelow a larger node and above a smallerone
Heap
Types of Binary Search Tree *Property of STI Page 20 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
21/24
Data Structures
Heap
Types of Binary Search Tree *Property of STI Page 21 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
22/24
Data Structures
Heap
Types of Binary Search Tree *Property of STI Page 22 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
23/24
Data Structures
Heap
Insertion – use trickle up
Types of Binary Search Tree *Property of STI Page 23 of 24
-
8/21/2019 Meljun Cortes Data Structures Types of Binary Search Tree
24/24
Data Structures
Heap
Types of Binary Search Tree *Property of STI