Stacks, Queues, Linked Lists, and Trees

Ace your homework & exams now with Quizwiz!

When duplicates are present

1. Just don't allow duplicates 2. Modify insertion process so as to insert a duplicate node as the right hand child of the already existing node

Maximum total number of nodes in a tree that has N levels

2^(n) -1

Maximum number of nodes in the Nth level of a binary tree?

2^(n-1)

Minimum number of levels that a binary search tree with x nodes can have

2^levels = x

Leaf node

A node that has no children

Subtree

A tree within a tree. Any node, except the root node, can be considered to be the root of a subtree, consisting of the node's descendants.

Linked List

ADT, sequential and dynamic, constructed from a series of nodes that each have data and a pointer to the next node of the list.

Queue

ADTs, First in First Out(FIFO) structures. Can only add elements to the back and remove elements from the front.

Stacks

Abstract data structure, Last in First Out(LIFO) structure. An element can be removed from the top of the stack, and only added on the top of the stack.

Trees

Abstract, hierarchical, dynamic data structure that stores data in a hierarchical, level manner.

Code snippets

Adding elements to the head of a stack/queue: list.push(5); - where push is addHead(item); list.enqueue(5); -where enqueue is addTail(item); Removing: list.pop(); -where pop is list.remove(0); list.dequeue(); - where dequeue is list.remove(0); Size: list.size(); - for both

Binary search tree special quality

All nodes' left hand child is smaller than their own value. All nodes' right hand child is bigger than their own value. Allows for binary search to be conducted.

Evaluation of linked list

Bad because takes up more memory since it has to have pointer and node. Good because useful in some cases(PP).

Disadvantages of static data structures

Can be very inefficient because of predefinition Even if array is partially full/empty, will take up the RAM space it was allocated at compile time Difficult to predict what array size is needed Insertion or deletion requires shifting of other elements.

Advantage of using libraries

Code can be used by programmers to maximize their efficiency so they need not "reinvent the wheel". Reduces size of application files, calling library code instead of writing it as application code. Code in libraries is guaranteed to be working, if a bug is present, it is in the application and not in the library, reducing debugging time if one were to code the library code themselves.

Node

Contains both data and a pointer

Node in a binary tree

Contains data and a left and right pointer

Accessing an element of a linked list

Done by following the pointers of all the previous nodes

When sketching linked lists

Each node should have data and a pointer, linked list should start with a header node.

Pointer

Field of the node whose value points to another object stored in some other memory location. last pointer pointer value is null

Adding an element to a linked list

Find the node you want to insert it after, create new node, change pointer of node that is before new node to new node, change pointer of new node to node after the first node's pointer you changed.

Specialty with a queue

First element that goes into a queue is the first element to come out. Like at a supermarket. Head and tail pointers.

Inserting element into a BST

Follow the tree based on the value of the element you are putting in and the values in the BST and put where appropriate.

Parent

Has at least 1 child node, found by going up the line that connects to a specific node

Static arrays

Have a specific maximum length that cannot be altered at run time.

Front and end of queue

Head and tail

Characteristics of the beginning/end of a linked list

Header node in the beginning only containing a pointer to the first element of the LL. Last node contains a null pointer.

Removing nodes

If it has no children, delete it. If it has one child, link the previous node to the only child node of that node you want to delete. If it has two children, find the minimum element from the right subtree(or maximum element from left subtree) and replace it with node that has to be deleted, and then delete the duplicate leaf node.

Adding elements to back of a queue

Inserting or enqueuing

Deleting an element from a linked list

Just change the pointer in the previous node to the one in the next node. Garbage collector will clean up unused node. When deleting last node, change the pointer of the previous node to null.

Postorder

LEFT, RIGHT, ROOT Gives root node last Point EAST

Level

Level of a node refers to how many generations the node is from the root, when root is level 1.

To search a linked list

Linear search must be done, this is a negative of it, if it is sorted then binary search can be done, increases search efficiency.

Circular linked list

Linked list where last link points back to the first link

Finding minimum and maximum value of BST

Look all the way left Look all the way right

Advantages of dynamic data structures

Make most efficient use of RAM as it only uses as much memory as it needs. One does not need to decide upon the size of data structure in advance.

Binary tree

May only have 0, 1, or 2 children.

Queue example

Mouse clicks/keyboard strokes so the computer system knows which clicks/strokes happened in which order

Double linked list

Nodes contain data, pointer to its successor, and pointer to its predecessor. Header node points to both first and last node of the list. Additional space is used though.

Height

Number of levels from top node to the deepest leaf(one that is furthest away)

Front end fixed queue

Only things at position 0 will be removed, everything has to move up to that position as position 0 is deleted.

Specialties of stack

Only top element may be inserted, deleted, or observed. Pointer variable must be implemented that will keep the index of the last element added to the stack.

Tail pointer

Points to a spot where enqueueing is possible

Head pointer

Points to first node

Removing elements on top of a stack

Popping

Adding elements on top of a stack

Pushing

Circular queue

Queue where last position is connected back to first position. The head/front pointer moves as things are deleted from the front, and therefore will not cause data crawling(in front end fixed the data would all shift up one, or in crawling queue data would crawl). Tail pointer moves as things are added to the back as well. Pointers are being moved to determine the front and back, no set positions. More efficient, won't crawl in memory, but careful tracking of the two pointers is necessary, hard to implement, increased chances for errors, hard to code.

Preorder traversal

ROOT, LEFT, RIGHT Starts with root node Point WEST

Recursive vs iterative algorithms

Recursive method calls itself. Recursive algorithms are heavier in RAM use, stack fill up. They are more efficient in code length and easier to see. Can be hard to understand concept of recursion. Iterative code is longer, can be more confusing, adds to chance of errors/bugs.

Inorder traversal

Returns all nodes in ascending order. LEFT, ROOT, RIGHT Point SOUTH

Top of a tree

Root node is first node of a tree.

Advantages of static data structures

Size is predefined, so no problems will occur when adding or removing items. Easier to program, no need to check size Space reserved in RAM will always be available to the data structure to use

Disadvantages of dynamic data structures

Structure might overflow if allowed limit is exceeded Algorithms with dynamic data are slower Random access is not allowed and elements should be visited sequentially More complicated to program

Child node

The node(s) that a previous node points to are its children nodes

Unbalanced/balanced tree

Tree whose left or right hand subtree has more nodes than the other subtree. Balanced tree is one that is symmetrical in nature, each node to the right and left of the initial root node has an equal number of child nodes, so this symmetrical form is achieved.

2D array

Two dimensions of arrays, forms a data table.

Example/advantage of stack

Undo button on Word. Advantage comes with its LIFO structure, only being able to access the top element makes undoing keystrokes on word much easier, as you don't need to find the latest keystroke because it is added to the top of the stack and can be popped.

Traversing a tree

Visit all nodes of a tree in some specified order Draw an outline around a tree and then use directions.

Recursion

When a method calls itself until a base case, or terminating condition, is met. With each method call the parameters/local variables of the method change and move closer to the base case.

Queue is empty

When head and tail pointer point to same node

Removing elements from the front of a queue

deleting or dequeuing

LL in memory

does not have to be continuous in the memory, can be

Main methods of a queue

enqueue(); adds an element to the queue as a last element, added according to a tail pointer variable for index dequeue(); removes first element, done using head pointer variable for index, pointer is moved down one. size(); returns size of the queue.

In order tree code

if root != null inOrder(root.leftChild) output(root.value) in order(root.RightChild)

Post order tree code

if root !=null postOrder(root.leftChild) postOrder(root.RightChild) output root.value

Pre order tree code

if root !=null output root.value preOrder(root.leftChild) preOrder(root.RightChild)

Adding nodes to a linked list

look at pic and know

Main methods of stack

push(); adds element at the top of a stack, based on pointer variable pop(); removes topmost element on the stack. Uses pointer to remove topmost element, pointer moves down one. size(); returns the size of the stack, returns index of pointer+1

Disadvantages of stacks/queues

slow access to other elements


Related study sets

Module 9 TTC (Hormonal/Glucose Regulation B)00

View Set

Social Studies Test November 31st

View Set