Binary Trees Data Structure

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Special types of binary trees

- Full: Every node contains 0 or 2 children. - Complete: If all levels (except last) are completely full and all nodes in last level are as far left as possible. - Perfect: If all internal nodes have 2 children and all leaf are at the same level.

BST remove algorithm: Remove an internal node w/two children

1. Locates X's successor (leftmost child of X's right subtree) [Hardest Case] 2. Copies the successor to X 3. Remove successor from the right subtree.

Parent

A node with a child is said to be that child's parent. → Node's ancestors include the node's parent, the parent's parent, etc., up to the tree's root.

Internal node

A node with at least one child.

Leaf

A tree node with no children.

Tree level

All nodes with the same depth

Successors and predecessors

BST defines an ordering among nodes, from smallest to largest

BST search algorithm

Checks current node (tree's root), returning that node as a match - Returns node immediately; no add search needed Else: Assign current node to → left (if key is less) → right (if key is greater) If child is null: Returns null (upon existing loop)

Binary Tree

Each node has up to two children/successors → Left child and Right child

Special types of binary trees: Full

Every node contains 0 or 2 children.

BST remove algorithm: Remove internal node w/ single child

If X is not root/no parent: - Parent's of X is assigned to X's single child. Else, if X was root/parent - Root pointer assigned w/X's single child.

Special types of binary trees: Perfect

If all internal nodes have 2 children and all leaf are at the same level.

Special types of binary trees: Complete

If all levels (except last) are completely full and all nodes in last level are as far left as possible.

BST Insert Algorithm: Search for insert location

If child not null → current node to child and continues searching

BST Insert Algorithm: Insert as right child

If new node's key is greater than the current node, and current node's right child is null

BST Insert Algorithm

Inserts new node in proper location obeying BST ordering property - Compares the new node with the current node (initially the root) → Insert as left or right child

Tree height

Largest depth of any node → Tree with just one node has height 0. Minimize height → by keeping all levels fulls Calculate (floor(log2N))

Edge

Link from a node to a child

Binary Tree: Usefulness

May require fewer comparisons → enables fast searching N-node binary tree's height may be as small as O(logN), yielding extremely fast searches Ex: A 10,000 node list may require 10,000 comparisons, but a 10,000 node BST may require only 14 comparisons.

Searching: To search node

Means to find node w/desired key, if such a node exists - BST may yield faster searches than a list.

BST Insert Algorithm: Insert as left child

New node's key is < current node, and the current node's left child is null

BST remove algorithm: Remove a leaf node

Node is assigned w/null If X is not root/no parent: - Parent's left or right child assigned w/null. Else, if X was root/parent: Root pointer assigned w/null → BST is now empty.

Successor

Node that comes after in the BST ordering Node w/right subtree: → Successor: that right subtree's leftmost child (left w/o other left) Node w/o right subtree: → Successor: first ancestor having this node in a left subtree

Depth

Number of edges on the path from the root to the node → Root node thus has depth 0.

Binary Search Tree (BST)

Ordering property that any node's - Left subtree keys ≤ the node's key - Right subtree's keys ≥ the node's key → enables fast searching

BST Remove Algorithm

Removes the first-found matching node, restructuring the tree to preserve the BST ordering property 1. Search for matching node (like search alg) 2. If found (call this node X), performs one of the following sub-algorithms • Remove a leaf node: • Remove internal node w/single child • Remove an internal node w/ two children Removing a node from N-node nearly-fullBST computational complexity: O(logN)

Pseudocode: Binary Search Tree (BST) (Searching

Starts by visiting the root node (first currentNode) → If a child to be visited doesn't exist, the desired node does not exist

Root

The one tree node with no parent → "top" node

BST Insert Algorithm Complexity

Traverses the tree from the root to a leaf node to find the insertion location. One node is visited per level BST w/N nodes has - At Least log2N levels: O(logN) - At Most N levels: O(N) Space complexity: O(1) → bc only single pointer used to traverse tree to find the insertion location.

BST remove algorithm complexity

Traverses tree from root to find node to remove. - Worst case scenario: tree traversed twice from root to a leaf Best case: O(logN) Worse case: O(N) Space complexity: O(1)

BST inorder traversal

Tree traversal algorithm: visits all nodes in the tree once → operation on each o From smallest to largest→ useful to print nodes sorted o root → left subtree → current node → right subtree. From largest to smallest → use: right/ current/ left sequence

BST search Runtime

Worst case: O(H) - Full Binary Height = log2N (round down) → H+1 is the tree height ▪ N-node binary tree's height may be as small as O(log N) → extremely fast

Predecessor

node that comes before in the BST ordering.

Searching a nearly-full BST with N nodes requires

only O(logN) comparisons.

In a list, each node has up to one successor, binary tree has up to

two successors


Kaugnay na mga set ng pag-aaral

Cumulative QC Questions/ Possible Panel Questions

View Set

Texas Government Chapter 1: The Political Culture, People, and Economy of Texas

View Set

Comm. The Essentials of Human Communication

View Set