Algorithms and data structures (6) Tree 2

Find and binary tree search

  1. Definition: binary search tree (binary search tree), using the idea of ​​the definition of tree dichotomy satisfy the following properties
    1. All healthy non-empty left subtree is less than its root key;
    2. All key value is not empty right subtree is greater than its root key
    3. All about sub-trees are binary search trees
//伪代码如下
public SearchTree find(int target,SearchTree st){
  if(st==null){
    return null;//查找失败
  }
  if(st.data<target){
    return find(target,st.right);
  }
  else if(st.data>target){
    return find(target,st.left);
  }else if return st;
  
}

Also known as tail recursion above recursive, tail-recursive procedure that is performed to return the recursive time;

Tail recursion can do cycle, more efficient implementation;

Binary search tree insertion

//伪代码如下
public SearchTree insert(int data , SearchTree st){
  if(st == null){
    st = new SearchTree();
    st.data =data;
  }else if (st.data >data){
    insert(data,st.left);
  }else insert(data,st.right);
  
  return st;
}

Title: Given a sequence of insertion can be uniquely determined a binary search tree. However, given a binary search tree, but can be obtained from a variety of different insert sequence. 2, respectively, according to a sequence {e.g., 1, 3} and {2, 3, 1} is inserted into an initially empty binary search tree, the same results were obtained. Thus the insertion sequence for the various input, you need to determine whether they are able to generate the same binary search tree.

Such as
3,2,4,1 to true and 3,2,1,4
3,2,4,1 3,1,4,2 false and

Achievements and achievements not divided into two strategies

Contribution strategy:
Building a tree pieces, and is determined to build the tree can form another sequence if the same tree;
tree constructed and another sequence comparison:
determining whether the sequence discovery process to find whether or not been met over the nodes; the output is false;

    public boolean compareTree(ArrayList<Integer> arr1,ArrayList<Integer> arr2){
        SearchTree st1 = new SearchTree(arr1);
        for (Integer target : arr1) {
            if (!check(st1,target)) return false;
        }
        return true;
    }

    public boolean check(SearchTree st1,int target){
        if (st1.flag==1){
            if (st1.data>target){
                check(st1.left,target);
            }else if (st1.data<target){
                check(st1.right,target);
            }else return false;
        }else if (st1.data==target) {
            st1.flag=1;
            return true;
        }
        return false;
    }

Binary search tree deletion

Binary search tree delete is divided into three cases discussed

  1. No child nodes, delete
  2. Only the left child node, or only the right child node, delete, pointing to its child nodes;
  3. Left and right child node has to find the maximum value of the left node as the root of the subtree, or to find the minimum value of the right subtree of the root of the subtree as

code show as below

//伪代码如下
public SearchTree delete(int target , SearchTree st){
	if(st == null){
    throw new Exception("无删除的数据");
  }else if(st.data>target)
    delete(target,st.left);
  else if(st.data<target)
    delete(target,st.right);
  else{
    if(st.right!=null && st.left!=null){
      SearchTree temp = findMin(st.right);
      st.data = temp.data;
      delete(temp.data,st.right);
    }else {
      if(st.right == null){
        st = st.left;
      }else st = st.right;
    }
  }
  return st;
}

public SearchTree findMin(SearchTree st){
  while (st.left!=null){
    st=st.left;
  }
  return st;
}

Balanced binary tree

Average length (ASL): i.e., average length, in a lookup operation, since the time-consuming in comparison keyword, so the number of times a keyword to find an average value to be compared and referred to the average length.
Here Insert Picture Description

Left and right subtrees more uniform of balance, the smaller the average length

Balance factor factor Balance : the BF (T) = H (L) -h®; H (L), H® refers to the height of the left and right subtrees of T;

Definition: the absolute value of an empty tree node of the tree or any BF (T) is less than 1;

Given n nodes of the tree balance maximum height log2n

Balanced binary tree adjustment

Here Insert Picture Description
Here Insert Picture Description

Published 17 original articles · won praise 0 · Views 367

Guess you like

Origin blog.csdn.net/qq_32193775/article/details/104107629