二叉tree

#ifndef BST_H_INCLUDED
#define BST_H_INCLUDED
template <class T>
class treenode
{
public:
    treenode():lson(NULL),rson(NULL),freq(1){};
    T data;
    int freq;
    treenode *lson;
    treenode *rson;
};
template <class T>
class bst
{
private:
    treenode<T>*root;
    void insertpri(treenode<T>*&node,T x);
    treenode <T>*findpri(treenode<T>*node,T x);
    void insubtree(treenode<T>*node);
    void deletepri(treenode<T>*&node,T x);
public:
    bst():root(NULL){}
    void insert_(T x);
    treenode<T>*find_(T x);
    void delete_(T x);
    void traversal();
};

#endif // BST_H_INCLUDED

#include "bst.h"

template <class T>
void bst<T>::insertpri(treenode<T>*&node,T x)
{
    if (node==NULL)
    {
        node=new treenode<T>();
        node->data=x;
        return;
    }
    if (node->data>x)
    {
     insertpri(node->lson,x);
    }
    else if (node->data<x)
    {
        insertpri(node->rson,x);
    }
    else (node->freq)++;
}

template <class T>
void bst<T>::insert_(T x)
{
    insertpri(root,x);
}

template <class T>
treenode<T>* bst<T>::findpri(treenode<T>*node,T x)
{
    if (node==NULL)
    {
        return NULL;
    }
    if (node->data>x)
    {
        findpri(node->lson,x);
    }
    else if (node->data<x)
    {
        findpri(node->rson,x);
    }
    else return node;
}

template <class T>
treenode<T>*bst<T>::find_(T x)
{
    findpri(root,x);
}

template <class T>
void bst<T>::deletepri(treenode<T>*&node,T x)
{
   if (node==NULL)
        return;
   if (x<node->data)
   {
       deletepri(node->lson,x);
   }
   else if (x>node->data)
   {
       deletepri(node->rson,x);
   }
   else
   {
       if (node->lson&&node->rson)
       {
           treenode<T>*temp=node->rson;
           while(temp->lson!=NULL)
           {
               temp=temp->lson;
           }

}
       else
       {
           treenode<T>*temp=node->rson;
           if (node->lson==NULL)
           {
               node=node->rson;
           }
           else if (node->rson==NULL)
           {
            node=node->lson;
           }
           delete (temp);

}
   }
   return;
}

template <class T>
void bst<T>::delete_(T x)
{
    deletepri(root,x);
}

template <class T>
void bst<T>::insubtree(treenode<T>*node)
{
    if (node==NULL)return;
    insubtree(node->lson);
    std::cout<<node->data<<" ";
    insubtree(node->rson);
}

template <class T>
void bst<T>::traversal()
{
    insubtree(root);
}

#include <iostream>
#include "bst.cpp"
using namespace std;
int main()
{
  bst<int> a;
  a.insert_(22);
  a.insert_(34);
  a.insert_(1);
  a.insert_(12);
  a.delete_(34);
  a.traversal();

}

时间: 2024-10-14 22:37:49

二叉tree的相关文章

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 讲一个排序好的数组转换成二叉搜索树,这题没想出来,基本上是参考别人的,边界条件应该注意一下: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * T

编程算法 - 二叉搜索树(binary search tree) 代码(C)

二叉搜索树(binary search tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉搜索树(binary search tree)能够高效的进行插入, 查询, 删除某个元素, 时间复杂度O(logn). 简单的实现方法例如以下. 代码: /* * main.cpp * * Created on: 2014.7.20 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <s

235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

Binary Search Tree (二叉搜索树)

一直在看Data Structure and Algorithm Analysis 的原版,英文水平有限看的比较慢.代码功力就更不用说了,所以代码打的还没有看书快……已经在看优先队列了,AVL树还没有打完也是棒棒哒.这会儿就先从二叉树更新开始吧. 二叉树的结构什么的基本都知道,二叉搜索树就是比就简单的二叉树多了一个特性(property)——每个节点的左子叶内的key比节点的key小,而其右子叶的key比节点的key大.这个特性不是唯一的(比如左右子叶相对于其父节点的key值大小顺序可以颠倒),

[LeetCode] Validate Binary Search Tree 验证二叉搜索树

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

[LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ 3 2 1 1

LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

[Leetcode] Convert sorted list to binary search tree 将排好的链表转成二叉搜索树

---恢复内容开始--- Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题目要求:转成高度平衡的二叉搜索树. 高度平衡的二叉搜索树:i)左子树和右子树的高度之差的绝对值不超过1; ii)树中的每个左子树和右子树都是AVL树; iii)每个节点都有一个平衡因子(balance factor bf),任一节点的平衡因子是1,0,